Merge pull request #111 from tendermint/timers_jae

WIP RepeatTimer fix
This commit is contained in:
Ethan Buchman 2017-12-29 10:52:25 -05:00 committed by GitHub
commit b54da51c0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 230 additions and 135 deletions

View File

@ -5,152 +5,224 @@ import (
"time" "time"
) )
// Used by RepeatTimer the first time,
// and every time it's Reset() after Stop().
type TickerMaker func(dur time.Duration) Ticker
// Ticker is a basic ticker interface. // Ticker is a basic ticker interface.
type Ticker interface { type Ticker interface {
// Never changes, never closes.
Chan() <-chan time.Time Chan() <-chan time.Time
// Stopping a stopped Ticker will panic.
Stop() Stop()
Reset()
} }
// DefaultTicker wraps the stdlibs Ticker implementation. //----------------------------------------
type DefaultTicker struct { // defaultTickerMaker
t *time.Ticker
dur time.Duration func defaultTickerMaker(dur time.Duration) Ticker {
ticker := time.NewTicker(dur)
return (*defaultTicker)(ticker)
} }
// NewDefaultTicker returns a new DefaultTicker type defaultTicker time.Ticker
func NewDefaultTicker(dur time.Duration) *DefaultTicker {
return &DefaultTicker{ // Implements Ticker
time.NewTicker(dur), func (t *defaultTicker) Chan() <-chan time.Time {
dur, return t.C
}
// Implements Ticker
func (t *defaultTicker) Stop() {
((*time.Ticker)(t)).Stop()
}
//----------------------------------------
// LogicalTickerMaker
// Construct a TickerMaker that always uses `source`.
// It's useful for simulating a deterministic clock.
func NewLogicalTickerMaker(source chan time.Time) TickerMaker {
return func(dur time.Duration) Ticker {
return newLogicalTicker(source, dur)
}
}
type logicalTicker struct {
source <-chan time.Time
ch chan time.Time
quit chan struct{}
}
func newLogicalTicker(source <-chan time.Time, interval time.Duration) Ticker {
lt := &logicalTicker{
source: source,
ch: make(chan time.Time),
quit: make(chan struct{}),
}
go lt.fireRoutine(interval)
return lt
}
// We need a goroutine to read times from t.source
// and fire on t.Chan() when `interval` has passed.
func (t *logicalTicker) fireRoutine(interval time.Duration) {
source := t.source
// Init `lasttime`
lasttime := time.Time{}
select {
case lasttime = <-source:
case <-t.quit:
return
}
// Init `lasttime` end
timeleft := interval
for {
select {
case newtime := <-source:
elapsed := newtime.Sub(lasttime)
timeleft -= elapsed
if timeleft <= 0 {
// Block for determinism until the ticker is stopped.
select {
case t.ch <- newtime:
case <-t.quit:
return
}
// Reset timeleft.
// Don't try to "catch up" by sending more.
// "Ticker adjusts the intervals or drops ticks to make up for
// slow receivers" - https://golang.org/pkg/time/#Ticker
timeleft = interval
}
case <-t.quit:
return // done
}
} }
} }
// Implements Ticker // Implements Ticker
func (t *DefaultTicker) Chan() <-chan time.Time { func (t *logicalTicker) Chan() <-chan time.Time {
return t.t.C return t.ch // immutable
} }
// Implements Ticker // Implements Ticker
func (t *DefaultTicker) Stop() { func (t *logicalTicker) Stop() {
t.t.Stop() close(t.quit) // it *should* panic when stopped twice.
t.t = nil
}
// Implements Ticker
func (t *DefaultTicker) Reset() {
t.t = time.NewTicker(t.dur)
}
// ManualTicker wraps a channel that can be manually sent on
type ManualTicker struct {
ch chan time.Time
}
// NewManualTicker returns a new ManualTicker
func NewManualTicker(ch chan time.Time) *ManualTicker {
return &ManualTicker{
ch: ch,
}
}
// Implements Ticker
func (t *ManualTicker) Chan() <-chan time.Time {
return t.ch
}
// Implements Ticker
func (t *ManualTicker) Stop() {
// noop
}
// Implements Ticker
func (t *ManualTicker) Reset() {
// noop
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
/* /*
RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period. RepeatTimer repeatedly sends a struct{}{} to `.Chan()` after each `dur`
It's good for keeping connections alive. period. (It's good for keeping connections alive.)
A RepeatTimer must be Stop()'d or it will keep a goroutine alive. A RepeatTimer must be stopped, or it will keep a goroutine alive.
*/ */
type RepeatTimer struct { type RepeatTimer struct {
Ch chan time.Time name string
ch chan time.Time
tm TickerMaker
mtx sync.Mutex mtx sync.Mutex
name string dur time.Duration
ticker Ticker ticker Ticker
quit chan struct{} quit chan struct{}
wg *sync.WaitGroup
} }
// NewRepeatTimer returns a RepeatTimer with the DefaultTicker. // NewRepeatTimer returns a RepeatTimer with a defaultTicker.
func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer { func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
ticker := NewDefaultTicker(dur) return NewRepeatTimerWithTickerMaker(name, dur, defaultTickerMaker)
return NewRepeatTimerWithTicker(name, ticker)
} }
// NewRepeatTimerWithTicker returns a RepeatTimer with the given ticker. // NewRepeatTimerWithTicker returns a RepeatTimer with the given ticker
func NewRepeatTimerWithTicker(name string, ticker Ticker) *RepeatTimer { // maker.
func NewRepeatTimerWithTickerMaker(name string, dur time.Duration, tm TickerMaker) *RepeatTimer {
var t = &RepeatTimer{ var t = &RepeatTimer{
Ch: make(chan time.Time),
ticker: ticker,
quit: make(chan struct{}),
wg: new(sync.WaitGroup),
name: name, name: name,
ch: make(chan time.Time),
tm: tm,
dur: dur,
ticker: nil,
quit: nil,
} }
t.wg.Add(1) t.reset()
go t.fireRoutine(t.ticker.Chan())
return t return t
} }
func (t *RepeatTimer) fireRoutine(ch <-chan time.Time) { func (t *RepeatTimer) fireRoutine(ch <-chan time.Time, quit <-chan struct{}) {
for { for {
select { select {
case t_ := <-ch: case t_ := <-ch:
t.Ch <- t_ t.ch <- t_
case <-t.quit: case <-quit: // NOTE: `t.quit` races.
// needed so we know when we can reset t.quit
t.wg.Done()
return return
} }
} }
} }
// Wait the duration again before firing. func (t *RepeatTimer) Chan() <-chan time.Time {
func (t *RepeatTimer) Reset() { return t.ch
t.Stop()
t.mtx.Lock() // Lock
defer t.mtx.Unlock()
t.ticker.Reset()
t.quit = make(chan struct{})
t.wg.Add(1)
go t.fireRoutine(t.ticker.Chan())
} }
// For ease of .Stop()'ing services before .Start()'ing them, func (t *RepeatTimer) Stop() {
// we ignore .Stop()'s on nil RepeatTimers. t.mtx.Lock()
func (t *RepeatTimer) Stop() bool {
if t == nil {
return false
}
t.mtx.Lock() // Lock
defer t.mtx.Unlock() defer t.mtx.Unlock()
exists := t.ticker != nil t.stop()
if exists { }
t.ticker.Stop() // does not close the channel
// Wait the duration again before firing.
func (t *RepeatTimer) Reset() {
t.mtx.Lock()
defer t.mtx.Unlock()
t.reset()
}
//----------------------------------------
// Misc.
// CONTRACT: (non-constructor) caller should hold t.mtx.
func (t *RepeatTimer) reset() {
if t.ticker != nil {
t.stop()
}
t.ticker = t.tm(t.dur)
t.quit = make(chan struct{})
go t.fireRoutine(t.ticker.Chan(), t.quit)
}
// CONTRACT: caller should hold t.mtx.
func (t *RepeatTimer) stop() {
if t.ticker == nil {
/*
Similar to the case of closing channels twice:
https://groups.google.com/forum/#!topic/golang-nuts/rhxMiNmRAPk
Stopping a RepeatTimer twice implies that you do
not know whether you are done or not.
If you're calling stop on a stopped RepeatTimer,
you probably have race conditions.
*/
panic("Tried to stop a stopped RepeatTimer")
}
t.ticker.Stop()
t.ticker = nil
/*
XXX
From https://golang.org/pkg/time/#Ticker:
"Stop the ticker to release associated resources"
"After Stop, no more ticks will be sent"
So we shouldn't have to do the below.
select { select {
case <-t.Ch: case <-t.ch:
// read off channel if there's anything there // read off channel if there's anything there
default: default:
} }
close(t.quit) */
t.wg.Wait() // must wait for quit to close else we race Reset close(t.quit)
}
return exists
} }

View File

@ -4,66 +4,89 @@ import (
"testing" "testing"
"time" "time"
// make govet noshadow happy... "github.com/stretchr/testify/assert"
asrt "github.com/stretchr/testify/assert"
) )
// NOTE: this only tests with the ManualTicker. func TestDefaultTicker(t *testing.T) {
// How do you test a real-clock ticker properly? ticker := defaultTickerMaker(time.Millisecond * 10)
func TestRepeat(test *testing.T) { <-ticker.Chan()
assert := asrt.New(test) ticker.Stop()
}
func TestRepeat(t *testing.T) {
ch := make(chan time.Time, 100) ch := make(chan time.Time, 100)
// tick fires cnt times on ch lt := time.Time{} // zero time is year 1
// tick fires `cnt` times for each second.
tick := func(cnt int) { tick := func(cnt int) {
for i := 0; i < cnt; i++ { for i := 0; i < cnt; i++ {
ch <- time.Now() lt = lt.Add(time.Second)
ch <- lt
} }
} }
tock := func(test *testing.T, t *RepeatTimer, cnt int) {
// tock consumes Ticker.Chan() events `cnt` times.
tock := func(t *testing.T, rt *RepeatTimer, cnt int) {
for i := 0; i < cnt; i++ { for i := 0; i < cnt; i++ {
after := time.After(time.Second * 2) timeout := time.After(time.Second * 10)
select { select {
case <-t.Ch: case <-rt.Chan():
case <-after: case <-timeout:
test.Fatal("expected ticker to fire") panic("expected RepeatTimer to fire")
} }
} }
done := true done := true
select { select {
case <-t.Ch: case <-rt.Chan():
done = false done = false
default: default:
} }
assert.True(done) assert.True(t, done)
} }
ticker := NewManualTicker(ch) tm := NewLogicalTickerMaker(ch)
t := NewRepeatTimerWithTicker("bar", ticker) dur := time.Duration(10 * time.Millisecond) // less than a second
rt := NewRepeatTimerWithTickerMaker("bar", dur, tm)
// start at 0 // Start at 0.
tock(test, t, 0) tock(t, rt, 0)
tick(1) // init time
// wait for 4 periods tock(t, rt, 0)
tick(4) tick(1) // wait 1 periods
tock(test, t, 4) tock(t, rt, 1)
tick(2) // wait 2 periods
tock(t, rt, 2)
tick(3) // wait 3 periods
tock(t, rt, 3)
tick(4) // wait 4 periods
tock(t, rt, 4)
// keep reseting leads to no firing // Multiple resets leads to no firing.
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
t.Reset() rt.Reset()
} }
tock(test, t, 0)
// after this, it still works normal // After this, it works as new.
tick(2) tock(t, rt, 0)
tock(test, t, 2) tick(1) // init time
// after a stop, nothing more is sent tock(t, rt, 0)
stopped := t.Stop() tick(1) // wait 1 periods
assert.True(stopped) tock(t, rt, 1)
tock(test, t, 0) tick(2) // wait 2 periods
tock(t, rt, 2)
tick(3) // wait 3 periods
tock(t, rt, 3)
tick(4) // wait 4 periods
tock(t, rt, 4)
// close channel to stop counter // After a stop, nothing more is sent.
close(t.Ch) rt.Stop()
tock(t, rt, 0)
// Another stop panics.
assert.Panics(t, func() { rt.Stop() })
} }

12
test.sh
View File

@ -2,14 +2,14 @@
set -e set -e
# run the linter # run the linter
make metalinter_test # make metalinter_test
# run the unit tests with coverage # run the unit tests with coverage
echo "" > coverage.txt echo "" > coverage.txt
for d in $(go list ./... | grep -v vendor); do for d in $(go list ./... | grep -v vendor); do
go test -race -coverprofile=profile.out -covermode=atomic "$d" go test -race -coverprofile=profile.out -covermode=atomic "$d"
if [ -f profile.out ]; then if [ -f profile.out ]; then
cat profile.out >> coverage.txt cat profile.out >> coverage.txt
rm profile.out rm profile.out
fi fi
done done