@@ -11,17 +11,17 @@ period for each retry attempt using a randomization function that grows exponent
11
11
12
12
NextBackOff() is calculated using the following formula:
13
13
14
- randomized interval =
15
- RetryInterval * (random value in range [1 - RandomizationFactor, 1 + RandomizationFactor])
14
+ randomized interval =
15
+ RetryInterval * (random value in range [1 - RandomizationFactor, 1 + RandomizationFactor])
16
16
17
17
In other words NextBackOff() will range between the randomization factor
18
18
percentage below and above the retry interval.
19
19
20
20
For example, given the following parameters:
21
21
22
- RetryInterval = 2
23
- RandomizationFactor = 0.5
24
- Multiplier = 2
22
+ RetryInterval = 2
23
+ RandomizationFactor = 0.5
24
+ Multiplier = 2
25
25
26
26
the actual backoff period used in the next retry attempt will range between 1 and 3 seconds,
27
27
multiplied by the exponential, that is, between 2 and 6 seconds.
@@ -36,18 +36,18 @@ The elapsed time can be reset by calling Reset().
36
36
Example: Given the following default arguments, for 10 tries the sequence will be,
37
37
and assuming we go over the MaxElapsedTime on the 10th try:
38
38
39
- Request # RetryInterval (seconds) Randomized Interval (seconds)
39
+ Request # RetryInterval (seconds) Randomized Interval (seconds)
40
40
41
- 1 0.5 [0.25, 0.75]
42
- 2 0.75 [0.375, 1.125]
43
- 3 1.125 [0.562, 1.687]
44
- 4 1.687 [0.8435, 2.53]
45
- 5 2.53 [1.265, 3.795]
46
- 6 3.795 [1.897, 5.692]
47
- 7 5.692 [2.846, 8.538]
48
- 8 8.538 [4.269, 12.807]
49
- 9 12.807 [6.403, 19.210]
50
- 10 19.210 backoff.Stop
41
+ 1 0.5 [0.25, 0.75]
42
+ 2 0.75 [0.375, 1.125]
43
+ 3 1.125 [0.562, 1.687]
44
+ 4 1.687 [0.8435, 2.53]
45
+ 5 2.53 [1.265, 3.795]
46
+ 6 3.795 [1.897, 5.692]
47
+ 7 5.692 [2.846, 8.538]
48
+ 8 8.538 [4.269, 12.807]
49
+ 9 12.807 [6.403, 19.210]
50
+ 10 19.210 backoff.Stop
51
51
52
52
Note: Implementation is not thread-safe.
53
53
*/
@@ -56,10 +56,9 @@ type ExponentialBackOff struct {
56
56
RandomizationFactor float64
57
57
Multiplier float64
58
58
MaxInterval time.Duration
59
- // After MaxElapsedTime the ExponentialBackOff returns Stop .
59
+ // After MaxElapsedTime the ExponentialBackOff stops .
60
60
// It never stops if MaxElapsedTime == 0.
61
61
MaxElapsedTime time.Duration
62
- Stop time.Duration
63
62
Clock Clock
64
63
65
64
currentInterval time.Duration
@@ -91,7 +90,6 @@ func NewExponentialBackOff(opts ...ExponentialBackOffOpts) *ExponentialBackOff {
91
90
Multiplier : DefaultMultiplier ,
92
91
MaxInterval : DefaultMaxInterval ,
93
92
MaxElapsedTime : DefaultMaxElapsedTime ,
94
- Stop : Stop ,
95
93
Clock : SystemClock ,
96
94
}
97
95
for _ , fn := range opts {
@@ -136,13 +134,6 @@ func WithMaxElapsedTime(duration time.Duration) ExponentialBackOffOpts {
136
134
}
137
135
}
138
136
139
- // WithRetryStopDuration sets the duration after which retries should stop.
140
- func WithRetryStopDuration (duration time.Duration ) ExponentialBackOffOpts {
141
- return func (ebo * ExponentialBackOff ) {
142
- ebo .Stop = duration
143
- }
144
- }
145
-
146
137
// WithClockProvider sets the clock used to measure time.
147
138
func WithClockProvider (clock Clock ) ExponentialBackOffOpts {
148
139
return func (ebo * ExponentialBackOff ) {
@@ -167,14 +158,15 @@ func (b *ExponentialBackOff) Reset() {
167
158
}
168
159
169
160
// NextBackOff calculates the next backoff interval using the formula:
170
- // Randomized interval = RetryInterval * (1 ± RandomizationFactor)
161
+ //
162
+ // Randomized interval = RetryInterval * (1 ± RandomizationFactor)
171
163
func (b * ExponentialBackOff ) NextBackOff () time.Duration {
172
164
// Make sure we have not gone over the maximum elapsed time.
173
165
elapsed := b .GetElapsedTime ()
174
166
next := getRandomValueFromInterval (b .RandomizationFactor , rand .Float64 (), b .currentInterval )
175
167
b .incrementCurrentInterval ()
176
168
if b .MaxElapsedTime != 0 && elapsed + next > b .MaxElapsedTime {
177
- return b . Stop
169
+ return Stop
178
170
}
179
171
return next
180
172
}
@@ -200,7 +192,8 @@ func (b *ExponentialBackOff) incrementCurrentInterval() {
200
192
}
201
193
202
194
// Returns a random value from the following interval:
203
- // [currentInterval - randomizationFactor * currentInterval, currentInterval + randomizationFactor * currentInterval].
195
+ //
196
+ // [currentInterval - randomizationFactor * currentInterval, currentInterval + randomizationFactor * currentInterval].
204
197
func getRandomValueFromInterval (randomizationFactor , random float64 , currentInterval time.Duration ) time.Duration {
205
198
if randomizationFactor == 0 {
206
199
return currentInterval // make sure no randomness is used when randomizationFactor is 0.
0 commit comments