Fix race between with event timer stopping early

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit b38cee9f9c)
This commit is contained in:
Tonis Tiigi 2016-03-31 09:48:20 -07:00 committed by Tibor Vass
parent bab77d4991
commit 9e3bfd5864

View file

@ -55,11 +55,10 @@ func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *
return err
}
timer := time.NewTimer(0)
timer.Stop()
var timeout <-chan time.Time
if until > 0 || untilNano > 0 {
dur := time.Unix(until, untilNano).Sub(time.Now())
timer = time.NewTimer(dur)
timeout = time.NewTimer(dur).C
}
ef, err := filters.FromParam(r.Form.Get("filters"))
@ -99,7 +98,7 @@ func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *
if err := enc.Encode(jev); err != nil {
return err
}
case <-timer.C:
case <-timeout:
return nil
case <-closeNotify:
logrus.Debug("Client disconnected, stop sending events")