-
Notifications
You must be signed in to change notification settings - Fork 7
/
backoff.go
126 lines (116 loc) · 3.48 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package httpretry
import (
"math"
"math/rand"
"time"
)
// BackoffPolicy is used to calculate the time to wait, before executing another request.
//
// The backoff can be calculated by taking the current number of retries into consideration.
type BackoffPolicy func(attemptCount int) time.Duration
var (
// defaultBackoffPolicy uses ExponentialBackoff with 1 second minWait, 30 seconds max wait and 200ms jitter
defaultBackoffPolicy = ExponentialBackoff(1*time.Second, 30*time.Second, 200*time.Millisecond)
// ConstantBackoff waits for the exact same duration after a failed retry.
//
// constantWait: the constant backoff
//
// maxJitter: random interval [0, maxJitter) added to the exponential backoff
//
// Example:
// minWait = 2 * time.Seconds
// maxJitter = 0 * time.Seconds
//
// Backoff will be: 2, 2, 2, ...
ConstantBackoff = func(constantWait time.Duration, maxJitter time.Duration) BackoffPolicy {
if constantWait < 0 {
constantWait = 0
}
if maxJitter < 0 {
maxJitter = 0
}
return func(attemptCount int) time.Duration {
return constantWait + randJitter(maxJitter)
}
}
// LinearBackoff increases the backoff time by multiplying the minWait duration by the number of attempts.
//
// minWait: the initial backoff
//
// maxWait: sets an upper bound on the maximum time to wait between two requests. set to 0 for no upper bound
//
// maxJitter: random interval [0, maxJitter) added to the linear backoff
//
// Example:
// minWait = 1 * time.Seconds
// maxWait = 5 * time.Seconds
// maxJitter = 0 * time.Seconds
//
// Backoff will be: 1, 2, 3, 4, 5, 5, 5, ...
LinearBackoff = func(minWait time.Duration, maxWait time.Duration, maxJitter time.Duration) BackoffPolicy {
if minWait < 0 {
minWait = 0
}
if maxJitter < 0 {
maxJitter = 0
}
if maxWait < minWait {
maxWait = 0
}
return func(attemptCount int) time.Duration {
nextWait := time.Duration(attemptCount)*minWait + randJitter(maxJitter)
if maxWait > 0 {
return minDuration(nextWait, maxWait)
}
return nextWait
}
}
// ExponentialBackoff increases the backoff exponentially by multiplying the minWait with 2^attemptCount
//
// minWait: the initial backoff
//
// maxWait: sets an upper bound on the maximum time to wait between two requests. set to 0 for no upper bound
//
// maxJitter: random interval [0, maxJitter) added to the exponential backoff
//
// Example:
// minWait = 1 * time.Seconds
// maxWait = 60 * time.Seconds
// maxJitter = 0 * time.Seconds
//
// Backoff will be: 1, 2, 4, 8, 16, 32, 60, 60, ...
ExponentialBackoff = func(minWait time.Duration, maxWait time.Duration, maxJitter time.Duration) BackoffPolicy {
if minWait < 0 {
minWait = 0
}
if maxJitter < 0 {
maxJitter = 0
}
if maxWait < minWait {
maxWait = 0
}
return func(attemptCount int) time.Duration {
nextWait := time.Duration(math.Pow(2, float64(attemptCount-1)))*minWait + randJitter(maxJitter)
if maxWait > 0 {
return minDuration(nextWait, maxWait)
}
return nextWait
}
}
)
// minDuration returns the minimum of two durations
func minDuration(duration1 time.Duration, duration2 time.Duration) time.Duration {
if duration1 < duration2 {
return duration1
}
return duration2
}
// randJitter returns a random duration in the interval [0, maxJitter)
//
// if maxJitter is <= 0, a duration of 0 is returned
func randJitter(maxJitter time.Duration) time.Duration {
if maxJitter <= 0 {
return 0
}
return time.Duration(rand.Intn(int(maxJitter)))
}