From 1ac4c5dd6d007a708337e1ad2636e456e1e4b8db Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 6 Dec 2017 21:20:30 +0100 Subject: [PATCH] Made throttle output non-blocking --- common/throttle_timer.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/common/throttle_timer.go b/common/throttle_timer.go index f2ce60b2a..069b6d84b 100644 --- a/common/throttle_timer.go +++ b/common/throttle_timer.go @@ -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