Skip to content

Commit

Permalink
fix: don't block on unresponsive SSE receivers
Browse files Browse the repository at this point in the history
Originally discovered, reported, and fixed by @ricmzn.
  • Loading branch information
b1naryth1ef committed Jan 20, 2022
1 parent d36949b commit 19feb77
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ func (h *httpServer) streamServerEvents(w http.ResponseWriter, r *http.Request)

for {
select {
case msg := <-sub:
case msg, ok := <-sub:
if !ok {
return
}

outgoing := []byte("data: ")
outgoing = append(outgoing, msg...)
outgoing = append(outgoing, '\n', '\n')
Expand Down
10 changes: 8 additions & 2 deletions server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ func (s *serverSession) publish(event string, data interface{}) error {
}

s.Lock()
for _, sub := range s.subscribers {
sub <- encoded
for subId, sub := range s.subscribers {
select {
case sub <- encoded:
continue
default:
log.Printf("[session:%v] subscriber %v non-responsive, closing", s.server.Name, subId)
close(sub)
}
}
s.Unlock()
return nil
Expand Down

0 comments on commit 19feb77

Please sign in to comment.