fix(events): serialize websocket writes per connection - #135
Open
cesar-carlos wants to merge 1 commit into
Open
Conversation
gorilla/websocket allows only one concurrent writer. CallWebhook runs in a goroutine per event, so Produce() could WriteJSON the same Conn from multiple goroutines under load (HistorySync/Receipts) and panic, killing the whole process. Wrap each Conn in wsConn with a write mutex so RLock still protects the client maps while writes are serialized per connection. Closes evolution-foundation#99 Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Sorry @cesar-carlos, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Reviewer's GuideIntroduces a per-connection wrapper around gorilla/websocket connections to serialize writes and adds race-detector tests that exercise concurrent instance-specific and broadcast websocket event delivery. Sequence diagram for serialized websocket writes via wsConnsequenceDiagram
participant Producer as websocketProducer
participant Map as clients_and_broadcast
participant WsConn as wsConn
participant WS as websocket_Conn
Producer->>Map: Produce(queueName, payload, instanceID)
Map-->>Producer: wsConn for instanceID
Producer->>WsConn: writeJSON(message)
activate WsConn
WsConn->>WsConn: writeMu.Lock()
WsConn->>WS: WriteJSON(message)
WS-->>WsConn: error or nil
WsConn->>WsConn: writeMu.Unlock()
deactivate WsConn
WsConn-->>Producer: error or nil
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
concurrent write to websocket connectionunder concurrent event dispatch (HistorySync / Receipts).*websocket.Connis wrapped inwsConnwith a per-connection write mutex; map access still usesRLock.Closes #99
Test plan
go test -race ./pkg/events/websocket/...Made with Cursor
Summary by Sourcery
Serialize websocket writes per connection to prevent concurrent write panics and add regression tests exercising concurrent instance and broadcast event delivery.
Bug Fixes:
Tests: