tendermint/common/repeat_timer.go

103 lines
2.1 KiB
Go
Raw Normal View History

2015-10-21 12:15:19 -07:00
package common
import (
"time"
)
2015-10-21 12:15:19 -07:00
/*
RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period.
It's good for keeping connections alive.
A RepeatTimer must be Stop()'d or it will keep a goroutine alive.
*/
type RepeatTimer struct {
2017-12-07 10:15:38 +01:00
Name string
Ch <-chan time.Time
output chan<- time.Time
input chan repeatCommand
2015-10-21 12:15:19 -07:00
2017-12-07 10:36:03 +01:00
dur time.Duration
ticker *time.Ticker
2017-12-07 10:36:03 +01:00
stopped bool
2015-10-21 12:15:19 -07:00
}
2017-12-08 10:07:04 +01:00
type repeatCommand int8
2017-12-07 10:15:38 +01:00
const (
Reset repeatCommand = iota
RQuit
)
2015-10-21 12:15:19 -07:00
func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
2017-12-07 10:15:38 +01:00
c := make(chan time.Time)
2015-10-21 12:15:19 -07:00
var t = &RepeatTimer{
2017-12-07 10:15:38 +01:00
Name: name,
Ch: c,
output: c,
input: make(chan repeatCommand),
2015-10-21 12:15:19 -07:00
dur: dur,
ticker: time.NewTicker(dur),
2015-10-21 12:15:19 -07:00
}
2017-12-07 10:15:38 +01:00
go t.run()
return t
2015-10-21 12:15:19 -07:00
}
// Wait the duration again before firing.
func (t *RepeatTimer) Reset() {
2017-12-07 10:15:38 +01:00
t.input <- Reset
2015-10-21 12:15:19 -07:00
}
// For ease of .Stop()'ing services before .Start()'ing them,
// we ignore .Stop()'s on nil RepeatTimers.
func (t *RepeatTimer) Stop() bool {
// use t.stopped to gracefully handle many Stop() without blocking
2017-12-07 10:36:03 +01:00
if t == nil || t.stopped {
2015-10-21 12:15:19 -07:00
return false
}
2017-12-07 10:15:38 +01:00
t.input <- RQuit
2017-12-07 10:36:03 +01:00
t.stopped = true
2017-12-07 10:15:38 +01:00
return true
}
2015-10-21 12:15:19 -07:00
2017-12-07 10:15:38 +01:00
func (t *RepeatTimer) run() {
2017-12-07 10:36:03 +01:00
done := false
for !done {
select {
2017-12-07 10:15:38 +01:00
case cmd := <-t.input:
// stop goroutine if the input says so
// don't close channels, as closed channels mess up select reads
2017-12-07 10:36:03 +01:00
done = t.processInput(cmd)
2017-12-08 10:07:04 +01:00
case tick := <-t.ticker.C:
2017-12-08 11:17:07 -06:00
t.send(tick)
}
2015-10-21 12:15:19 -07:00
}
2017-12-07 10:15:38 +01:00
}
2017-12-08 11:17:07 -06:00
// send performs blocking send on t.Ch
func (t *RepeatTimer) send(tick time.Time) {
// XXX: possibly it is better to not block:
// https://golang.org/src/time/sleep.go#L132
2017-12-08 10:07:04 +01:00
// select {
// case t.output <- tick:
// default:
// }
2017-12-08 11:17:07 -06:00
t.output <- tick
2017-12-07 10:15:38 +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 *RepeatTimer) processInput(cmd repeatCommand) (shutdown bool) {
switch cmd {
case Reset:
t.ticker.Stop()
t.ticker = time.NewTicker(t.dur)
2017-12-07 10:15:38 +01:00
case RQuit:
t.ticker.Stop()
2017-12-07 10:15:38 +01:00
shutdown = true
default:
panic("unknown command!")
}
return shutdown
2015-10-21 12:15:19 -07:00
}