forked from cenkalti/backoff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backoff.go
51 lines (39 loc) · 1.18 KB
/
backoff.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Package backoff helps you at backing off !
//
//It was forked from github.com/cenkalti/backoff which is awesome.
//
//This BackOff sleeps upon BackOff() and calculates its next backoff time instead of returning the duration to sleep.
package backoff
import "time"
// Interface interface to use after a retryable operation failed.
// A Interface.BackOff sleeps.
type Interface interface {
// Example usage:
//
// for ;; {
// err, canRetry := somethingThatCanFail()
// if err != nil && canRetry {
// backoffer.Backoff()
// }
// }
BackOff()
// Reset to initial state.
Reset()
}
// ZeroBackOff is a fixed back-off policy whose back-off time is always zero,
// meaning that the operation is retried immediately without waiting.
type ZeroBackOff struct{}
var _ Interface = (*ZeroBackOff)(nil)
func (b *ZeroBackOff) Reset() {}
func (b *ZeroBackOff) BackOff() {}
type ConstantBackOff struct {
Interval time.Duration
}
var _ Interface = (*ConstantBackOff)(nil)
func (b *ConstantBackOff) Reset() {}
func (b *ConstantBackOff) BackOff() {
time.Sleep(b.Interval)
}
func NewConstant(d time.Duration) *ConstantBackOff {
return &ConstantBackOff{Interval: d}
}