generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
31 lines (25 loc) · 1.02 KB
/
timer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package retry
import "time"
// Timer is a closure strategy for starting a timer. The returned stop
// function has the same semantics as time.Timer.Stop.
//
// The default Timer used internally delegates to time.NewTimer. A custom
// Timer is primarily useful in unit tests.
type Timer func(time.Duration) (ch <-chan time.Time, stop func() bool)
// defaultTimer is the strategy used to create a timer using the stdlib.
func defaultTimer(d time.Duration) (<-chan time.Time, func() bool) {
t := time.NewTimer(d)
return t.C, t.Stop
}
func nopStop() bool { return true }
// immediateTimer is a Timer function that returns a channel that is immediately
// signaled along with a stop function that is a nop.
//
// The main use case for this type of Timer is a unit test.
func immediateTimer(d time.Duration) (<-chan time.Time, func() bool) {
ch := make(chan time.Time, 1)
ch <- time.Now().Add(d)
return ch, nopStop
}