Use Ticker in Repeat again to avoid drift

This commit is contained in:
Ethan Frey 2017-12-07 11:22:54 +01:00
parent 8797197cdf
commit cc7a87e27c
2 changed files with 17 additions and 23 deletions

View File

@ -1,7 +1,6 @@
package common package common
import ( import (
"fmt"
"time" "time"
) )
@ -17,7 +16,7 @@ type RepeatTimer struct {
input chan repeatCommand input chan repeatCommand
dur time.Duration dur time.Duration
timer *time.Timer ticker *time.Ticker
stopped bool stopped bool
} }
@ -36,8 +35,8 @@ func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
output: c, output: c,
input: make(chan repeatCommand), input: make(chan repeatCommand),
timer: time.NewTimer(dur),
dur: dur, dur: dur,
ticker: time.NewTicker(dur),
} }
go t.run() go t.run()
return t return t
@ -51,6 +50,7 @@ func (t *RepeatTimer) Reset() {
// For ease of .Stop()'ing services before .Start()'ing them, // For ease of .Stop()'ing services before .Start()'ing them,
// we ignore .Stop()'s on nil RepeatTimers. // we ignore .Stop()'s on nil RepeatTimers.
func (t *RepeatTimer) Stop() bool { func (t *RepeatTimer) Stop() bool {
// use t.stopped to gracefully handle many Stop() without blocking
if t == nil || t.stopped { if t == nil || t.stopped {
return false return false
} }
@ -67,39 +67,33 @@ func (t *RepeatTimer) run() {
// stop goroutine if the input says so // stop goroutine if the input says so
// don't close channels, as closed channels mess up select reads // don't close channels, as closed channels mess up select reads
done = t.processInput(cmd) done = t.processInput(cmd)
case <-t.timer.C: case <-t.ticker.C:
// send if not blocked, then start the next tick
t.trySend() t.trySend()
t.timer.Reset(t.dur)
} }
} }
fmt.Println("end run")
} }
// trySend performs non-blocking send on t.Ch // trySend performs non-blocking send on t.Ch
func (t *RepeatTimer) trySend() { func (t *RepeatTimer) trySend() {
// TODO: this was blocking in previous version (t.Ch <- t_) // NOTE: this was blocking in previous version (t.Ch <- t_)
// should I use that behavior unstead of unblocking as per throttle? // should I use that behavior unstead of unblocking as per throttle?
// probably not: https://golang.org/src/time/sleep.go#L132
// select { select {
// case t.output <- time.Now(): case t.output <- time.Now():
// default: default:
// } }
t.output <- time.Now()
} }
// all modifications of the internal state of ThrottleTimer // all modifications of the internal state of ThrottleTimer
// happen in this method. It is only called from the run goroutine // happen in this method. It is only called from the run goroutine
// so we avoid any race conditions // so we avoid any race conditions
func (t *RepeatTimer) processInput(cmd repeatCommand) (shutdown bool) { func (t *RepeatTimer) processInput(cmd repeatCommand) (shutdown bool) {
fmt.Printf("process: %d\n", cmd)
switch cmd { switch cmd {
case Reset: case Reset:
t.timer.Reset(t.dur) t.ticker.Stop()
t.ticker = time.NewTicker(t.dur)
case RQuit: case RQuit:
fmt.Println("got quit") t.ticker.Stop()
t.timer.Stop()
shutdown = true shutdown = true
default: default:
panic("unknown command!") panic("unknown command!")

View File

@ -39,11 +39,11 @@ func (c *rCounter) Read() {
func TestRepeat(test *testing.T) { func TestRepeat(test *testing.T) {
assert := asrt.New(test) assert := asrt.New(test)
dur := time.Duration(50) * time.Millisecond dur := time.Duration(100) * time.Millisecond
short := time.Duration(20) * time.Millisecond short := time.Duration(20) * time.Millisecond
// delay waits for cnt durations, an a little extra // delay waits for cnt durations, an a little extra
delay := func(cnt int) time.Duration { delay := func(cnt int) time.Duration {
return time.Duration(cnt)*dur + time.Duration(5)*time.Millisecond return time.Duration(cnt)*dur + time.Duration(10)*time.Millisecond
} }
t := NewRepeatTimer("bar", dur) t := NewRepeatTimer("bar", dur)
@ -70,7 +70,7 @@ func TestRepeat(test *testing.T) {
// after a stop, nothing more is sent // after a stop, nothing more is sent
stopped := t.Stop() stopped := t.Stop()
assert.True(stopped) assert.True(stopped)
time.Sleep(delay(7)) time.Sleep(delay(2))
assert.Equal(6, c.Count()) assert.Equal(6, c.Count())
// extra calls to stop don't block // extra calls to stop don't block