Skip to content

Commit

Permalink
fix: improve memory usage of the subscribe handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Sep 8, 2020
1 parent 503b36d commit 0a4526c
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion hub/subscribe.go
Expand Up @@ -30,6 +30,7 @@ func (h *Hub) SubscribeHandler(w http.ResponseWriter, r *http.Request) {
var heartbeatTimerC <-chan time.Time
if heartbeatInterval != time.Duration(0) {
heartbeatTimer = time.NewTimer(heartbeatInterval)
defer heartbeatTimer.Stop()
heartbeatTimerC = heartbeatTimer.C
}

Expand All @@ -39,6 +40,7 @@ func (h *Hub) SubscribeHandler(w http.ResponseWriter, r *http.Request) {
var writeTimerC <-chan time.Time
if writeTimeout != 0 {
writeTimer = time.NewTimer(writeTimeout - dispatchTimeout)
defer writeTimer.Stop()
writeTimerC = writeTimer.C
}

Expand Down Expand Up @@ -169,10 +171,12 @@ func (h *Hub) write(w io.Writer, s *Subscriber, data string, d time.Duration) bo
close(done)
}()

timeout := time.NewTimer(d)
defer timeout.Stop()
select {
case <-done:
return true
case <-time.After(d):
case <-timeout.C:
log.WithFields(s.LogFields).Warn("Dispatch timeout reached")

return false
Expand Down

0 comments on commit 0a4526c

Please sign in to comment.