You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
750 B
36 lines
750 B
3 years ago
|
package backoff
|
||
|
|
||
|
import "time"
|
||
|
|
||
|
type Timer interface {
|
||
|
Start(duration time.Duration)
|
||
|
Stop()
|
||
|
C() <-chan time.Time
|
||
|
}
|
||
|
|
||
|
// defaultTimer implements Timer interface using time.Timer
|
||
|
type defaultTimer struct {
|
||
|
timer *time.Timer
|
||
|
}
|
||
|
|
||
|
// C returns the timers channel which receives the current time when the timer fires.
|
||
|
func (t *defaultTimer) C() <-chan time.Time {
|
||
|
return t.timer.C
|
||
|
}
|
||
|
|
||
|
// Start starts the timer to fire after the given duration
|
||
|
func (t *defaultTimer) Start(duration time.Duration) {
|
||
|
if t.timer == nil {
|
||
|
t.timer = time.NewTimer(duration)
|
||
|
} else {
|
||
|
t.timer.Reset(duration)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Stop is called when the timer is not used anymore and resources may be freed.
|
||
|
func (t *defaultTimer) Stop() {
|
||
|
if t.timer != nil {
|
||
|
t.timer.Stop()
|
||
|
}
|
||
|
}
|