tendermint/common/repeat_timer.go

109 lines
2.2 KiB
Go
Raw Normal View History

2015-10-21 12:15:19 -07:00
package common
import (
2017-12-07 10:15:38 +01:00
"fmt"
"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
timer *time.Timer
stopped bool
2015-10-21 12:15:19 -07:00
}
2017-12-07 10:15:38 +01:00
type repeatCommand int32
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
2017-12-07 10:15:38 +01:00
timer: time.NewTimer(dur),
dur: 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 {
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-07 10:15:38 +01:00
case <-t.timer.C:
// send if not blocked, then start the next tick
t.trySend()
t.timer.Reset(t.dur)
}
2015-10-21 12:15:19 -07:00
}
2017-12-07 10:36:03 +01:00
fmt.Println("end run")
2017-12-07 10:15:38 +01:00
}
// trySend performs non-blocking send on t.Ch
func (t *RepeatTimer) trySend() {
// TODO: this was blocking in previous version (t.Ch <- t_)
// should I use that behavior unstead of unblocking as per throttle?
2017-12-07 10:36:03 +01:00
// select {
// case t.output <- time.Now():
// default:
// }
t.output <- time.Now()
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) {
fmt.Printf("process: %d\n", cmd)
switch cmd {
case Reset:
t.timer.Reset(t.dur)
case RQuit:
2017-12-07 10:36:03 +01:00
fmt.Println("got quit")
2017-12-07 10:15:38 +01:00
t.timer.Stop()
shutdown = true
default:
panic("unknown command!")
}
return shutdown
2015-10-21 12:15:19 -07:00
}