Skip to content

Commit

Permalink
Finish all timers when stopping Wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Dec 6, 2016
1 parent 06915ae commit 708d436
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions timers/timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (w *Wheel) Stop() {
w.ticker.Stop()
close(w.stop)
<-w.stopped
todo := w.gatherTimers(0, uint64(len(w.buckets)))
w.fire(todo)
})
}

Expand Down Expand Up @@ -155,13 +157,17 @@ func (w *Wheel) tick() {

func (w *Wheel) work() {
for batch := range w.fired {
for t := batch; t != nil; t = t.next {
t.f()
}
w.fire(batch)
}
close(w.stopped)
}

func (w *Wheel) fire(batch *Timer) {
for t := batch; t != nil; t = t.next {
t.f()
}
}

func (w *Wheel) gatherTimers(startTick, endTick uint64) *Timer {
var todo *Timer
for tick := startTick; tick < endTick; tick++ {
Expand Down
15 changes: 15 additions & 0 deletions timers/timers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ func TestTimerDroppingTicks(t *testing.T) {
wg.Wait()
}

func TestTimerClearAfterStop(t *testing.T) {
const numTimers = 100
w, _ := fakeWheel(1 * time.Millisecond)

var wg sync.WaitGroup
wg.Add(numTimers)
timeouts := randomTimeouts(numTimers, 100*time.Millisecond)
for _, d := range timeouts {
w.AfterFunc(d, func() { wg.Done() })
}

w.Stop()
wg.Wait()
}

func TestPowersOfTwo(t *testing.T) {
tests := []struct {
in int
Expand Down

0 comments on commit 708d436

Please sign in to comment.