-
Notifications
You must be signed in to change notification settings - Fork 56
feat(api): XIP-83 d14n binding — bidirectional Subscribe on QueryApi #2020
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ed2d5a0
feat(api): add mutableSubscription handle to subscribeWorker for in-p…
tylerhawkes 2965820
feat(api): XIP-83 bidirectional Subscribe handler on QueryApi + xmtpv…
tylerhawkes c44972f
test(api): Subscribe handler tests + configurable keepalive interval …
tylerhawkes 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 hidden or 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 hidden or 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,80 @@ | ||
| package message | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/xmtp/xmtpd/pkg/envelopes" | ||
| ) | ||
|
|
||
| // mutableSubscription is an in-place-mutable topic subscription on the subscribeWorker, used by | ||
| // the XIP-83 bidirectional Subscribe RPC. Unlike subscribeWorker.listen (a fixed query), its | ||
| // topic set is grown and shrunk over the life of the stream via addTopics / removeTopics. It is | ||
| // never global: an empty topic set delivers nothing (a fresh stream that has subscribed to | ||
| // nothing yet), in contrast to newListener, which treats an empty query as "all envelopes". | ||
| // | ||
| // All mutation methods are intended to be called from the single owning goroutine (the Subscribe | ||
| // handler's writer loop). The worker's reap path (closeListener) may also touch the underlying | ||
| // listener's topic set concurrently; both sides take listener.topicsMu, so that is safe. | ||
| type mutableSubscription struct { | ||
| worker *subscribeWorker | ||
| l *listener | ||
| // ch is the receive end of the listener channel. The worker pushes envelope batches here; | ||
| // it CLOSES the channel when it reaps the listener (ctx done or the consumer fell behind), | ||
| // which the writer loop observes as "torn down, reconnect from cursors". | ||
| ch <-chan []*envelopes.OriginatorEnvelope | ||
| } | ||
|
|
||
| // newMutableSubscription registers an empty, non-global topic subscription on the worker and | ||
| // returns a handle the caller mutates over the stream's lifetime. Call close when done. | ||
| func (s *subscribeWorker) newMutableSubscription(ctx context.Context) *mutableSubscription { | ||
| ch := make(chan []*envelopes.OriginatorEnvelope, subscriptionBufferSize) | ||
| l := &listener{ | ||
| ctx: ctx, | ||
| ch: ch, | ||
| topics: make(map[string]struct{}), | ||
| originators: make(map[uint32]struct{}), | ||
| isGlobal: false, // empty topic set => delivers nothing, NOT all-envelopes | ||
| } | ||
| return &mutableSubscription{worker: s, l: l, ch: ch} | ||
| } | ||
|
|
||
| // addTopics begins delivering the given topic keys to this subscription. Idempotent: a key | ||
| // already subscribed is a no-op. A no-op once the worker has reaped the listener. | ||
| func (m *mutableSubscription) addTopics(keys map[string]struct{}) { | ||
| if len(keys) == 0 { | ||
| return | ||
| } | ||
| m.l.topicsMu.Lock() | ||
| defer m.l.topicsMu.Unlock() | ||
| if m.l.closed { | ||
| return | ||
| } | ||
| m.worker.topicListeners.addListener(keys, m.l) | ||
| for k := range keys { | ||
| m.l.topics[k] = struct{}{} | ||
| } | ||
| } | ||
|
|
||
| // removeTopics stops delivering the given topic keys. Idempotent. | ||
| func (m *mutableSubscription) removeTopics(keys map[string]struct{}) { | ||
| if len(keys) == 0 { | ||
| return | ||
| } | ||
| m.l.topicsMu.Lock() | ||
| defer m.l.topicsMu.Unlock() | ||
| m.worker.topicListeners.removeListener(keys, m.l) | ||
| for k := range keys { | ||
| delete(m.l.topics, k) | ||
| } | ||
| } | ||
|
|
||
| // close unregisters the subscription from every topic it still holds. Safe to call even after | ||
| // the worker has already reaped the listener (removeListener is idempotent). | ||
| func (m *mutableSubscription) close() { | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| m.l.topicsMu.Lock() | ||
| defer m.l.topicsMu.Unlock() | ||
| if len(m.l.topics) > 0 { | ||
| m.worker.topicListeners.removeListener(m.l.topics, m.l) | ||
| m.l.topics = make(map[string]struct{}) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
🔴 Critical
message/mutable_subscription.go:47mutableSubscription.addTopicschecksm.l.closedwhile holdingtopicsMu, butsubscribeWorker.closeListenersetsl.closed = truewithout taking that lock. A concurrent mutate can pass the staleclosedcheck and re-add the listener totopicListenersafter its channel is closed, causingdispatchToListenersto panic on send.func (m *mutableSubscription) addTopics(keys map[string]struct{}) { if len(keys) == 0 { return } - m.l.topicsMu.Lock() - defer m.l.topicsMu.Unlock() - if m.l.closed { + m.worker.topicListeners.mu.RLock() + defer m.worker.topicListeners.mu.RUnlock() + m.l.topicsMu.Lock() + defer m.l.topicsMu.Unlock() + if m.l.closed { return } m.worker.topicListeners.addListener(keys, m.l)🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
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.
Fixed.
closeListenernow setsl.closedunderl.topicsMu, so a concurrentmutableSubscription.addTopicsobserves it atomically with itsaddListenercall and can no longer re-register a listener after its channel is closed. Added a-raceregression test (TestMutableSubscriptionAddRaceWithClose) that hammers addTopics against the reap.