2015-10-21 12:15:19 -07:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
ThrottleTimer fires an event at most "dur" after each .Set() call.
|
|
|
|
If a short burst of .Set() calls happens, ThrottleTimer fires once.
|
|
|
|
If a long continuous burst of .Set() calls happens, ThrottleTimer fires
|
|
|
|
at most once every "dur".
|
|
|
|
*/
|
|
|
|
type ThrottleTimer struct {
|
2017-12-06 21:51:23 +01:00
|
|
|
Name string
|
|
|
|
Ch chan struct{}
|
|
|
|
input chan command
|
|
|
|
dur time.Duration
|
2016-07-11 22:17:09 -04:00
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
timer *time.Timer
|
2016-07-11 22:17:09 -04:00
|
|
|
isSet bool
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
2017-12-06 11:17:50 +01:00
|
|
|
type command int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
Set command = iota
|
|
|
|
Unset
|
|
|
|
Quit
|
|
|
|
)
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
func NewThrottleTimer(name string, dur time.Duration) *ThrottleTimer {
|
2017-12-06 11:17:50 +01:00
|
|
|
var t = &ThrottleTimer{
|
2017-12-06 21:51:23 +01:00
|
|
|
Name: name,
|
|
|
|
Ch: make(chan struct{}),
|
|
|
|
dur: dur,
|
|
|
|
input: make(chan command),
|
|
|
|
timer: time.NewTimer(dur),
|
2017-12-06 11:17:50 +01:00
|
|
|
}
|
2015-10-21 12:15:19 -07:00
|
|
|
t.timer.Stop()
|
2017-12-06 11:17:50 +01:00
|
|
|
go t.run()
|
2015-10-21 12:15:19 -07:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:51:23 +01:00
|
|
|
// C is the proper way to listen to the timer output.
|
|
|
|
// t.Ch will be made private in the (near?) future
|
|
|
|
func (t *ThrottleTimer) C() <-chan struct{} {
|
|
|
|
return t.Ch
|
|
|
|
}
|
|
|
|
|
2017-12-06 11:17:50 +01:00
|
|
|
func (t *ThrottleTimer) run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case cmd := <-t.input:
|
|
|
|
// stop goroutine if the input says so
|
2017-12-06 22:28:18 +01:00
|
|
|
// don't close channels, as closed channels mess up select reads
|
2017-12-06 11:17:50 +01:00
|
|
|
if t.processInput(cmd) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-t.timer.C:
|
2017-12-06 21:20:30 +01:00
|
|
|
t.trySend()
|
2017-12-06 11:17:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:51:23 +01:00
|
|
|
// trySend performs non-blocking send on t.Ch
|
2017-12-06 21:20:30 +01:00
|
|
|
func (t *ThrottleTimer) trySend() {
|
|
|
|
select {
|
2017-12-06 21:51:23 +01:00
|
|
|
case t.Ch <- struct{}{}:
|
2017-12-06 21:20:30 +01:00
|
|
|
t.isSet = false
|
|
|
|
default:
|
|
|
|
// if we just want to drop, replace this with t.isSet = false
|
|
|
|
t.timer.Reset(t.dur)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 11:17:50 +01:00
|
|
|
// 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
|
|
|
|
func (t *ThrottleTimer) processInput(cmd command) (shutdown bool) {
|
|
|
|
switch cmd {
|
|
|
|
case Set:
|
|
|
|
if !t.isSet {
|
|
|
|
t.isSet = true
|
|
|
|
t.timer.Reset(t.dur)
|
|
|
|
}
|
|
|
|
case Quit:
|
|
|
|
shutdown = true
|
|
|
|
fallthrough
|
|
|
|
case Unset:
|
|
|
|
if t.isSet {
|
|
|
|
t.isSet = false
|
2017-12-06 11:21:01 +01:00
|
|
|
t.timer.Stop()
|
2017-12-06 11:17:50 +01:00
|
|
|
}
|
2015-10-21 12:15:19 -07:00
|
|
|
default:
|
2017-12-06 11:17:50 +01:00
|
|
|
panic("unknown command!")
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
2017-12-06 11:17:50 +01:00
|
|
|
return shutdown
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ThrottleTimer) Set() {
|
2017-12-06 11:17:50 +01:00
|
|
|
t.input <- Set
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:12:10 -08:00
|
|
|
func (t *ThrottleTimer) Unset() {
|
2017-12-06 11:17:50 +01:00
|
|
|
t.input <- Unset
|
2016-01-10 08:12:10 -08:00
|
|
|
}
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
// For ease of .Stop()'ing services before .Start()'ing them,
|
|
|
|
// we ignore .Stop()'s on nil ThrottleTimers
|
|
|
|
func (t *ThrottleTimer) Stop() bool {
|
|
|
|
if t == nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-12-06 11:17:50 +01:00
|
|
|
t.input <- Quit
|
|
|
|
return true
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|