-
Notifications
You must be signed in to change notification settings - Fork 566
/
backoff.go
110 lines (92 loc) · 3.13 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Package backoff implements backoff algorithms for retrying operations.
//
// Use Retry function for retrying operations that may fail.
// If Retry does not meet your needs,
// copy/paste the function into your project and modify as you wish.
//
// There is also Ticker type similar to time.Ticker.
// You can use it if you need to work with channels.
//
// See Examples section below for usage examples.
package backoff
import "time"
// BackOff is a backoff policy for retrying an operation.
type BackOff interface {
// NextBackOff returns the duration to wait before retrying the operation,
// or backoff.Stop to indicate that no more retries should be made.
//
// Example usage:
//
// duration := backoff.NextBackOff();
// if (duration == backoff.Stop) {
// // Do not retry operation.
// } else {
// // Sleep for duration and retry operation.
// }
//
NextBackOff() time.Duration
// Reset to initial state.
Reset()
}
// Stop indicates that no more retries should be made for use in NextBackOff().
const Stop time.Duration = -1
// ZeroBackOff is a fixed backoff policy whose backoff time is always zero,
// meaning that the operation is retried immediately without waiting, indefinitely.
type ZeroBackOff struct{}
// Reset ...
func (b *ZeroBackOff) Reset() {}
// NextBackOff ...
func (b *ZeroBackOff) NextBackOff() time.Duration { return 0 }
// StopBackOff is a fixed backoff policy that always returns backoff.Stop for
// NextBackOff(), meaning that the operation should never be retried.
type StopBackOff struct{}
// Reset ...
func (b *StopBackOff) Reset() {}
// NextBackOff ...
func (b *StopBackOff) NextBackOff() time.Duration { return Stop }
// ConstantBackOff is a backoff policy that always returns the same backoff delay.
// This is in contrast to an exponential backoff policy,
// which returns a delay that grows longer as you call NextBackOff() over and over again.
//
// Note: Implementation is not thread-safe
type ConstantBackOff struct {
Interval time.Duration
// After MaxElapsedTime the ConstantBackOff stops.
// It never stops if MaxElapsedTime == 0.
MaxElapsedTime time.Duration
startTime time.Time
}
// Reset ...
func (b *ConstantBackOff) Reset() {
b.startTime = time.Now()
}
// GetElapsedTime returns the elapsed time since an ExponentialBackOff instance
// is created and is reset when Reset() is called.
//
// The elapsed time is computed using time.Now().UnixNano().
func (b *ConstantBackOff) GetElapsedTime() time.Duration {
return time.Since(b.startTime)
}
// NextBackOff ...
func (b *ConstantBackOff) NextBackOff() time.Duration {
if b.MaxElapsedTime != 0 && b.GetElapsedTime() > b.MaxElapsedTime {
return Stop
}
return b.Interval
}
// NewConstantBackOff ...
func NewConstantBackOff(d time.Duration) *ConstantBackOff {
return &ConstantBackOff{
Interval: d,
}
}
// RetryEvery is an alias for NewConstantBackoff, with a nicer name for inline
// calls
func RetryEvery(d time.Duration) *ConstantBackOff {
return NewConstantBackOff(d)
}
// For sets b.MaxElapsedTime to 'maxElapsed' and returns b
func (b *ConstantBackOff) For(maxElapsed time.Duration) *ConstantBackOff {
b.MaxElapsedTime = maxElapsed
return b
}