-
Notifications
You must be signed in to change notification settings - Fork 4
fix(sse): fixed connection leak issue #77
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
Conversation
Reviewer's GuideRefactored SSE server connection handling to prevent client reuse and potential connection leaks by enforcing a single join per client with safe locking, and extended the Conn API with a new error and Context accessor. Sequence diagram for SSE Server Join process with single client enforcementsequenceDiagram
participant Client as actor Client
participant Server
participant Conn
participant Streamer
Client->>Server: Join(ctx, id, rw)
Server->>Server: RLock conns
alt id already in conns
Server-->>Client: return ErrClientJoined
else id not in conns
Server->>Server: Lock conns
Server->>Server: conns[id] = new Conn
Server->>Server: Unlock conns
Server->>Conn: Connect(ctx, sm)
Server->>Streamer: Set SSE headers
Server-->>Client: return Conn
end
Class diagram for updated SSE server connection handlingclassDiagram
class Server {
+map[string]*Conn conns
+Join(ctx, id, rw) (*Conn, error)
}
class Conn {
+string ID
+Send(evt Event) error
+Context() context.Context
}
Server "1" -- "*" Conn : manages
class errors {
<<Error>>
+ErrClientClosed
+ErrClientJoined
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey @cnlangzi - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `ext/sse/server.go:38` </location>
<code_context>
- s.Lock()
- defer s.Unlock()
- c, ok := s.conns[id]
+ s.RLock()
+ _, ok := s.conns[id]
+ s.RUnlock()
- if !ok {
</code_context>
<issue_to_address>
Consider using a single Lock instead of RLock/Lock mix for consistency.
Acquiring the Lock once and performing both the existence check and addition within the same critical section will prevent a potential race condition where two goroutines could add the same connection.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
ext/sse/server.go
Outdated
s.RLock() | ||
_, ok := s.conns[id] | ||
s.RUnlock() |
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.
issue (bug_risk): Consider using a single Lock instead of RLock/Lock mix for consistency.
Acquiring the Lock once and performing both the existence check and addition within the same critical section will prevent a potential race condition where two goroutines could add the same connection.
Here's the code health analysis summary for commits Analysis Summary
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #77 +/- ##
==========================================
- Coverage 91.71% 91.70% -0.01%
==========================================
Files 65 65
Lines 2582 2616 +34
==========================================
+ Hits 2368 2399 +31
- Misses 186 188 +2
- Partials 28 29 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Changed
Fixed
Added
Tests
Tasks to complete before merging PR:
make unit-tests
to check for any regressions 📋make lint
to check for any issuesSummary by Sourcery
Prevent connection leaks by rejecting duplicate SSE client joins and improving concurrency control
Bug Fixes:
Enhancements: