fix(SSE): capture Writer before goroutine, use select for channel send

Address PR review feedback:
- Capture w := c.Writer before goroutine start, use w (not c.Writer)
  inside the goroutine to avoid holding *Context reference
- Move channel send into select alongside context cancellation in all
  examples and tests, preventing goroutine leak when client disconnects
  while blocked on unbuffered send
This commit is contained in:
wjqserver 2026-03-29 16:05:09 +08:00
parent 2f94763c65
commit 559aefeb85
3 changed files with 16 additions and 17 deletions

View file

@ -68,8 +68,7 @@ func TestEventStreamChanUnblocksOnClientDisconnect(t *testing.T) {
select {
case <-ctx.Done():
return
default:
eventChan <- Event{Data: "tick"}
case eventChan <- Event{Data: "tick"}:
time.Sleep(10 * time.Millisecond)
}
}