-
Notifications
You must be signed in to change notification settings - Fork 122
backend: fan out WebSocket events to multiple subscribers #3934
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
Open
jstrnbrg
wants to merge
1
commit into
BitBoxSwiss:master
Choose a base branch
from
jstrnbrg:multiple_previews
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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,82 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package handlers | ||
|
|
||
| import "sync" | ||
|
|
||
| const eventChannelBufferSize = 1000 | ||
|
|
||
| // eventBroker fans out events to multiple subscribers. Each subscriber gets its own buffered | ||
| // channel. If a subscriber's channel is full, events are dropped for that subscriber to avoid | ||
| // blocking other subscribers or the publisher. | ||
| // | ||
| // Events published before any subscriber connects are buffered (up to eventChannelBufferSize) and | ||
| // replayed to the first subscriber that connects. This prevents a race where events (e.g. | ||
| // auth-result) are lost because the WebSocket client hasn't connected yet. | ||
| type eventBroker struct { | ||
| mu sync.Mutex | ||
| subscribers map[int]chan interface{} | ||
| nextID int | ||
| // earlyEvents buffers events published before the first subscriber connects. Once the first | ||
| // subscriber is added, this slice is drained into its channel and set to nil. | ||
| earlyEvents []interface{} | ||
| } | ||
|
|
||
| func newEventBroker() *eventBroker { | ||
| return &eventBroker{ | ||
| subscribers: make(map[int]chan interface{}), | ||
| earlyEvents: make([]interface{}, 0), | ||
| } | ||
| } | ||
|
|
||
| // subscribe registers a new subscriber and returns an ID (for unsubscribe) and a channel to | ||
| // receive events on. | ||
| func (b *eventBroker) subscribe() (int, <-chan interface{}) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| id := b.nextID | ||
| b.nextID++ | ||
| ch := make(chan interface{}, eventChannelBufferSize) | ||
| // Replay early events to the first subscriber, then discard the buffer. | ||
| if b.earlyEvents != nil { | ||
| for _, event := range b.earlyEvents { | ||
| select { | ||
| case ch <- event: | ||
| default: | ||
| } | ||
| } | ||
| b.earlyEvents = nil | ||
| } | ||
| b.subscribers[id] = ch | ||
| return id, ch | ||
| } | ||
|
|
||
| // unsubscribe removes a subscriber and closes its channel. | ||
| func (b *eventBroker) unsubscribe(id int) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| if ch, ok := b.subscribers[id]; ok { | ||
| delete(b.subscribers, id) | ||
| close(ch) | ||
| } | ||
| } | ||
|
|
||
| // publish sends an event to all subscribers. Non-blocking: if a subscriber's buffer is full, the | ||
| // event is dropped for that subscriber. | ||
| func (b *eventBroker) publish(event interface{}) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| if len(b.subscribers) == 0 && b.earlyEvents != nil { | ||
| if len(b.earlyEvents) < eventChannelBufferSize { | ||
| b.earlyEvents = append(b.earlyEvents, event) | ||
| } | ||
| return | ||
| } | ||
| for _, ch := range b.subscribers { | ||
| select { | ||
| case ch <- event: | ||
| default: | ||
| // Drop event for slow subscriber to avoid blocking others. | ||
| } | ||
| } | ||
| } | ||
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
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.
Didn't check in detail, but dropping events does not sound good. 1000 is an arbitrary cut-off, can't guarantee events won't be dropped in the released app.