blob: 8120d0213c58d550ea7cf20ab4d2329134abb12a [file] [log] [blame]
khenaidoo5e4fca32021-05-12 16:02:23 -04001package backoff
2
3import "time"
4
5type Timer interface {
6 Start(duration time.Duration)
7 Stop()
8 C() <-chan time.Time
9}
10
11// defaultTimer implements Timer interface using time.Timer
12type defaultTimer struct {
13 timer *time.Timer
14}
15
16// C returns the timers channel which receives the current time when the timer fires.
17func (t *defaultTimer) C() <-chan time.Time {
18 return t.timer.C
19}
20
21// Start starts the timer to fire after the given duration
22func (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.
31func (t *defaultTimer) Stop() {
32 if t.timer != nil {
33 t.timer.Stop()
34 }
35}