Skip to content

Commit 258846d

Browse files
committed
Revert "add support for custom Stop duration in ExponentialBackOff"
This reverts commit 671f609.
1 parent 720b789 commit 258846d

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

exponential.go

+22-29
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ period for each retry attempt using a randomization function that grows exponent
1111
1212
NextBackOff() is calculated using the following formula:
1313
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])
1616
1717
In other words NextBackOff() will range between the randomization factor
1818
percentage below and above the retry interval.
1919
2020
For example, given the following parameters:
2121
22-
RetryInterval = 2
23-
RandomizationFactor = 0.5
24-
Multiplier = 2
22+
RetryInterval = 2
23+
RandomizationFactor = 0.5
24+
Multiplier = 2
2525
2626
the actual backoff period used in the next retry attempt will range between 1 and 3 seconds,
2727
multiplied by the exponential, that is, between 2 and 6 seconds.
@@ -36,18 +36,18 @@ The elapsed time can be reset by calling Reset().
3636
Example: Given the following default arguments, for 10 tries the sequence will be,
3737
and assuming we go over the MaxElapsedTime on the 10th try:
3838
39-
Request # RetryInterval (seconds) Randomized Interval (seconds)
39+
Request # RetryInterval (seconds) Randomized Interval (seconds)
4040
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
5151
5252
Note: Implementation is not thread-safe.
5353
*/
@@ -56,10 +56,9 @@ type ExponentialBackOff struct {
5656
RandomizationFactor float64
5757
Multiplier float64
5858
MaxInterval time.Duration
59-
// After MaxElapsedTime the ExponentialBackOff returns Stop.
59+
// After MaxElapsedTime the ExponentialBackOff stops.
6060
// It never stops if MaxElapsedTime == 0.
6161
MaxElapsedTime time.Duration
62-
Stop time.Duration
6362
Clock Clock
6463

6564
currentInterval time.Duration
@@ -91,7 +90,6 @@ func NewExponentialBackOff(opts ...ExponentialBackOffOpts) *ExponentialBackOff {
9190
Multiplier: DefaultMultiplier,
9291
MaxInterval: DefaultMaxInterval,
9392
MaxElapsedTime: DefaultMaxElapsedTime,
94-
Stop: Stop,
9593
Clock: SystemClock,
9694
}
9795
for _, fn := range opts {
@@ -136,13 +134,6 @@ func WithMaxElapsedTime(duration time.Duration) ExponentialBackOffOpts {
136134
}
137135
}
138136

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-
146137
// WithClockProvider sets the clock used to measure time.
147138
func WithClockProvider(clock Clock) ExponentialBackOffOpts {
148139
return func(ebo *ExponentialBackOff) {
@@ -167,14 +158,15 @@ func (b *ExponentialBackOff) Reset() {
167158
}
168159

169160
// NextBackOff calculates the next backoff interval using the formula:
170-
// Randomized interval = RetryInterval * (1 ± RandomizationFactor)
161+
//
162+
// Randomized interval = RetryInterval * (1 ± RandomizationFactor)
171163
func (b *ExponentialBackOff) NextBackOff() time.Duration {
172164
// Make sure we have not gone over the maximum elapsed time.
173165
elapsed := b.GetElapsedTime()
174166
next := getRandomValueFromInterval(b.RandomizationFactor, rand.Float64(), b.currentInterval)
175167
b.incrementCurrentInterval()
176168
if b.MaxElapsedTime != 0 && elapsed+next > b.MaxElapsedTime {
177-
return b.Stop
169+
return Stop
178170
}
179171
return next
180172
}
@@ -200,7 +192,8 @@ func (b *ExponentialBackOff) incrementCurrentInterval() {
200192
}
201193

202194
// Returns a random value from the following interval:
203-
// [currentInterval - randomizationFactor * currentInterval, currentInterval + randomizationFactor * currentInterval].
195+
//
196+
// [currentInterval - randomizationFactor * currentInterval, currentInterval + randomizationFactor * currentInterval].
204197
func getRandomValueFromInterval(randomizationFactor, random float64, currentInterval time.Duration) time.Duration {
205198
if randomizationFactor == 0 {
206199
return currentInterval // make sure no randomness is used when randomizationFactor is 0.

exponential_test.go

-16
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ func TestMaxElapsedTime(t *testing.T) {
8383
assertEquals(t, Stop, exp.NextBackOff())
8484
}
8585

86-
func TestCustomStop(t *testing.T) {
87-
var exp = NewExponentialBackOff()
88-
customStop := time.Minute
89-
exp.Stop = customStop
90-
exp.Clock = &TestClock{start: time.Time{}.Add(10000 * time.Second)}
91-
// Change the currentElapsedTime to be 0 ensuring that the elapsed time will be greater
92-
// than the max elapsed time.
93-
exp.startTime = time.Time{}
94-
assertEquals(t, customStop, exp.NextBackOff())
95-
}
96-
9786
func TestBackOffOverflow(t *testing.T) {
9887
var (
9988
testInitialInterval time.Duration = math.MaxInt64 / 2
@@ -125,7 +114,6 @@ func TestNewExponentialBackOff(t *testing.T) {
125114
WithMultiplier(2.0),
126115
WithMaxInterval(10*time.Second),
127116
WithMaxElapsedTime(30*time.Second),
128-
WithRetryStopDuration(0),
129117
WithClockProvider(SystemClock),
130118
)
131119

@@ -151,10 +139,6 @@ func TestNewExponentialBackOff(t *testing.T) {
151139
t.Errorf("Expected MaxElapsedTime to be 30 seconds, got %v", backOff.MaxElapsedTime)
152140
}
153141

154-
if backOff.Stop != 0 {
155-
t.Errorf("Expected Stop to be 0 (no stop), got %v", backOff.Stop)
156-
}
157-
158142
if backOff.Clock != SystemClock {
159143
t.Errorf("Expected Clock to be SystemClock, got %v", backOff.Clock)
160144
}

0 commit comments

Comments
 (0)