-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(message-cache): make the topic cache generic #1097
Conversation
e883997
to
a825155
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
I have two remarks:
-
In case the cache should only be generic over "topic kinds" like pubsub topic and content topic,
I'd add a comment in the code explaining that.
Otherwise, imo, a signature like this is confusing:
proc isSubscribed*[K](t: MessageCache[K], topic: K)
It might raise the question: why is thekey
name "topic"? -
Since the generic naming for a topic kind like pubsub and content topic has been introduced using the name
topic
,
naming the specific pubsubTopic version of the cacheTopicCache
is confusing.
(Generally, in other parts like the RFCs and so on, I'd always explicitly use pubsub topic and content topic;
if we did this consistently, simply calling this generic version topic
would be OK, imo.)
We could also make the message cache generic over any type of key and rename
proc isSubscribed*[K](t: MessageCache[K], topic: K): bool
proc subscribe*[K](t: MessageCache[K], topic: K)
proc unsubscribe*[K](t: MessageCache[K], topic: K)
to
proc hasKey*[K](t: MessageCache[K], key: K): bool
proc addKey*[K](t: MessageCache[K], key: K)
proc deleteKey*[K](t: MessageCache[K], key: K)
Would make the names less expressive though.... and introducing a hierarchy that overrides such methods would be overkill 😅
const DefaultMessageCacheCapacity*: uint = 30 # Max number of messages cached per topic @TODO make this configurable | ||
|
||
|
||
type MessageCacheResult*[T] = Result[T, cstring] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Typically, cstring
is only used when interfacing via FFI.
Sometimes, our code uses cstring
sometimes string
. Imo, we should be consistent here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth noting that the Nim style guide recommends using cstring
with Result
: https://status-im.github.io/nim-style-guide/libraries.results.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding @kaiserd suggestions above: No strong opinions, but I agree with adding a comment to indicate that the MessageCache
is designed to be a cache of messages against subscribed topics (pubsub- or contentTopic) and perhaps renaming TopicCache
to PubsubTopicCache
. Don't think it's necessary to be even more generic now, at least until we have identified a need for a very generic MessageCache
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth noting that the Nim style guide recommends using cstring with Result: https://status-im.github.io/nim-style-guide/libraries.results.html
Good to know :). Thanks. (In one of my first PRs, I was asked why I don't use string
as the error type because the context was Nim-only. I just copied and did not know this is in the style guide 😅 .)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah! Makes sense. You're absolutely right that we haven't been consistent with this (and it's only been recently that I started using cstring
with Result
after a closer reading of the style guide :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
a825155
to
ed90534
Compare
Yes, that's the idea.
That's the reason why I made it generic like that. I am not looking to make it super generic. Just to be used in the REST API HTTP request handlers. |
Sorry, but I'd rather not change the name of those methods. Those names are very expressive and make a lot of sense in the context this message cache is used. The suggested method names are typically associated with dictionaries (a.k.a. tables) and this is not exactly a table. We should understand this as an addressable (by pubsub/pubsub+content topic) list of message queues. |
Yes. That's fine, too. Making it more generic and changing method names was just one possible solution.
whereas "topic" in the generic type's procs' signatures names something generic over (pubsubTopic, contentTopic, a tuple of those, ...). Maybe:
|
Ok, thanks. I am taking notes about that. I am planning to do a Relay REST API code reorganization. I will rename it under that PR to |
The current topic cache implementation can be made generic to support also the filter use case. Now it allows different types of keys:
string
(the type of PubSub topics)PubsubTopicString
(akastring
) andContentTopic
.This work is necessary for #1076 (filter REST API)