Made throttle output non-blocking

This commit is contained in:
Ethan Frey 2017-12-06 21:20:30 +01:00
parent 0a8721113a
commit 1ac4c5dd6d
1 changed files with 13 additions and 3 deletions

View File

@ -30,7 +30,7 @@ const (
)
func NewThrottleTimer(name string, dur time.Duration) *ThrottleTimer {
c := make(chan struct{}, 1)
c := make(chan struct{})
var t = &ThrottleTimer{
Name: name,
Ch: c,
@ -54,12 +54,22 @@ func (t *ThrottleTimer) run() {
return
}
case <-t.timer.C:
t.isSet = false
t.output <- struct{}{}
t.trySend()
}
}
}
// trySend performs non-blocking send on t.output (t.Ch)
func (t *ThrottleTimer) trySend() {
select {
case t.output <- struct{}{}:
t.isSet = false
default:
// if we just want to drop, replace this with t.isSet = false
t.timer.Reset(t.dur)
}
}
// all modifications of the internal state of ThrottleTimer
// happen in this method. It is only called from the run goroutine
// so we avoid any race conditions