tendermint/common/throttle_timer.go

130 lines
2.6 KiB
Go
Raw Normal View History

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 {
Name string
Ch <-chan struct{}
2017-12-07 01:15:38 -08:00
input chan throttleCommand
output chan<- struct{}
dur time.Duration
2017-12-07 01:36:03 -08:00
timer *time.Timer
isSet bool
stopped bool
2015-10-21 12:15:19 -07:00
}
2017-12-08 01:07:04 -08:00
type throttleCommand int8
2017-12-06 02:17:50 -08:00
const (
2017-12-07 01:15:38 -08:00
Set throttleCommand = iota
2017-12-06 02:17:50 -08:00
Unset
2017-12-07 01:15:38 -08:00
TQuit
2017-12-06 02:17:50 -08:00
)
// NewThrottleTimer creates a new ThrottleTimer.
2015-10-21 12:15:19 -07:00
func NewThrottleTimer(name string, dur time.Duration) *ThrottleTimer {
c := make(chan struct{})
2017-12-06 02:17:50 -08:00
var t = &ThrottleTimer{
Name: name,
Ch: c,
dur: dur,
2017-12-07 01:15:38 -08:00
input: make(chan throttleCommand),
output: c,
timer: time.NewTimer(dur),
2017-12-06 02:17:50 -08:00
}
2015-10-21 12:15:19 -07:00
t.timer.Stop()
2017-12-06 02:17:50 -08:00
go t.run()
2015-10-21 12:15:19 -07:00
return t
}
2017-12-06 02:17:50 -08:00
func (t *ThrottleTimer) run() {
for {
select {
case cmd := <-t.input:
// stop goroutine if the input says so
// don't close channels, as closed channels mess up select reads
2017-12-06 02:17:50 -08:00
if t.processInput(cmd) {
return
}
case <-t.timer.C:
2017-12-06 12:20:30 -08:00
t.trySend()
2017-12-06 02:17:50 -08:00
}
}
}
// trySend performs non-blocking send on t.Ch
2017-12-06 12:20:30 -08:00
func (t *ThrottleTimer) trySend() {
select {
case t.output <- struct{}{}:
2017-12-06 12:20:30 -08: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 02:17:50 -08: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
2017-12-07 01:15:38 -08:00
func (t *ThrottleTimer) processInput(cmd throttleCommand) (shutdown bool) {
2017-12-06 02:17:50 -08:00
switch cmd {
case Set:
if !t.isSet {
t.isSet = true
t.timer.Reset(t.dur)
}
2017-12-07 01:15:38 -08:00
case TQuit:
2017-12-06 02:17:50 -08:00
shutdown = true
fallthrough
case Unset:
if t.isSet {
t.isSet = false
2017-12-06 02:21:01 -08:00
t.timer.Stop()
2017-12-06 02:17:50 -08:00
}
2015-10-21 12:15:19 -07:00
default:
2017-12-06 02:17:50 -08:00
panic("unknown command!")
2015-10-21 12:15:19 -07:00
}
2017-12-06 02:17:50 -08:00
return shutdown
2015-10-21 12:15:19 -07:00
}
func (t *ThrottleTimer) Set() {
2017-12-06 02:17:50 -08: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 02:17:50 -08:00
t.input <- Unset
2016-01-10 08:12:10 -08:00
}
// Stop prevents the ThrottleTimer from firing. It always returns true. Stop does not
// close the channel, to prevent a read from the channel succeeding
// incorrectly.
//
// To prevent a timer created with NewThrottleTimer from firing after a call to
// Stop, check the return value and drain the channel.
//
// For example, assuming the program has not received from t.C already:
//
// if !t.Stop() {
// <-t.C
// }
//
// For ease of stopping services before starting them, we ignore Stop on nil
// ThrottleTimers.
2015-10-21 12:15:19 -07:00
func (t *ThrottleTimer) Stop() bool {
2017-12-07 01:36:03 -08:00
if t == nil || t.stopped {
2015-10-21 12:15:19 -07:00
return false
}
2017-12-07 01:15:38 -08:00
t.input <- TQuit
2017-12-08 01:07:04 -08:00
t.stopped = true
2017-12-06 02:17:50 -08:00
return true
2015-10-21 12:15:19 -07:00
}