-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{.push raises: [Defect].} | ||
|
||
import | ||
std/[tables, sequtils], | ||
stew/results, | ||
chronicles, | ||
chronos, | ||
libp2p/protocols/pubsub | ||
import | ||
../protocol/waku_message | ||
|
||
logScope: topics = "message_cache" | ||
|
||
const DefaultMessageCacheCapacity*: uint = 30 # Max number of messages cached per topic @TODO make this configurable | ||
|
||
|
||
type MessageCacheResult*[T] = Result[T, cstring] | ||
|
||
type MessageCache*[K] = ref object | ||
capacity: uint | ||
table: Table[K, seq[WakuMessage]] | ||
|
||
func init*[K](T: type MessageCache[K], capacity=DefaultMessageCacheCapacity): T = | ||
MessageCache[K]( | ||
capacity: capacity, | ||
table: initTable[K, seq[WakuMessage]]() | ||
) | ||
|
||
|
||
proc isSubscribed*[K](t: MessageCache[K], topic: K): bool = | ||
t.table.hasKey(topic) | ||
|
||
proc subscribe*[K](t: MessageCache[K], topic: K) = | ||
if t.isSubscribed(topic): | ||
return | ||
t.table[topic] = @[] | ||
|
||
proc unsubscribe*[K](t: MessageCache[K], topic: K) = | ||
if not t.isSubscribed(topic): | ||
return | ||
t.table.del(topic) | ||
|
||
|
||
proc addMessage*[K](t: MessageCache, topic: K, msg: WakuMessage) = | ||
if not t.isSubscribed(topic): | ||
return | ||
|
||
# Make a copy of msgs for this topic to modify | ||
var messages = t.table.getOrDefault(topic, @[]) | ||
|
||
if messages.len >= t.capacity.int: | ||
debug "Topic cache capacity reached", topic=topic | ||
# Message cache on this topic exceeds maximum. Delete oldest. | ||
# TODO: this may become a bottle neck if called as the norm rather than | ||
# exception when adding messages. Performance profile needed. | ||
messages.delete(0,0) | ||
|
||
messages.add(msg) | ||
|
||
# Replace indexed entry with copy | ||
t.table[topic] = messages | ||
|
||
proc clearMessages*[K](t: MessageCache[K], topic: K) = | ||
if not t.isSubscribed(topic): | ||
return | ||
t.table[topic] = @[] | ||
|
||
proc getMessages*[K](t: MessageCache[K], topic: K, clear=false): MessageCacheResult[seq[WakuMessage]] = | ||
if not t.isSubscribed(topic): | ||
return err("Not subscribed to topic") | ||
|
||
let messages = t.table.getOrDefault(topic, @[]) | ||
if clear: | ||
t.clearMessages(topic) | ||
|
||
ok(messages) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
sometimesstring
. 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
withResult
: https://status-im.github.io/nim-style-guide/libraries.results.htmlThere 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 renamingTopicCache
toPubsubTopicCache
. Don't think it's necessary to be even more generic now, at least until we have identified a need for a very genericMessageCache
.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.
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
withResult
after a closer reading of the style guide :) )