timer.go raw
1 package backoff
2
3 import "time"
4
5 type Timer interface {
6 Start(duration time.Duration)
7 Stop()
8 C() <-chan time.Time
9 }
10
11 // defaultTimer implements Timer interface using time.Timer
12 type defaultTimer struct {
13 timer *time.Timer
14 }
15
16 // C returns the timers channel which receives the current time when the timer fires.
17 func (t *defaultTimer) C() <-chan time.Time {
18 return t.timer.C
19 }
20
21 // Start starts the timer to fire after the given duration
22 func (t *defaultTimer) Start(duration time.Duration) {
23 if t.timer == nil {
24 t.timer = time.NewTimer(duration)
25 } else {
26 t.timer.Reset(duration)
27 }
28 }
29
30 // Stop is called when the timer is not used anymore and resources may be freed.
31 func (t *defaultTimer) Stop() {
32 if t.timer != nil {
33 t.timer.Stop()
34 }
35 }
36