9 RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period.
10 It's good for keeping connections alive.
11 A RepeatTimer must be Stop()'d or it will keep a goroutine alive.
13 type RepeatTimer struct {
24 func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
26 Ch: make(chan time.Time),
27 ticker: time.NewTicker(dur),
28 quit: make(chan struct{}),
29 wg: new(sync.WaitGroup),
34 go t.fireRoutine(t.ticker)
38 func (t *RepeatTimer) fireRoutine(ticker *time.Ticker) {
41 case t_ := <-ticker.C:
44 // needed so we know when we can reset t.quit
51 // Wait the duration again before firing.
52 func (t *RepeatTimer) Reset() {
58 t.ticker = time.NewTicker(t.dur)
59 t.quit = make(chan struct{})
61 go t.fireRoutine(t.ticker)
64 // For ease of .Stop()'ing services before .Start()'ing them,
65 // we ignore .Stop()'s on nil RepeatTimers.
66 func (t *RepeatTimer) Stop() bool {
73 exists := t.ticker != nil
75 t.ticker.Stop() // does not close the channel
78 // read off channel if there's anything there
82 t.wg.Wait() // must wait for quit to close else we race Reset