Skip to content

Commit 18fe4ce

Browse files
committed
add next interval to elapsed time before deciding to stop exponential backoff #95
1 parent 78afdac commit 18fe4ce

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

exponential.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ func (b *ExponentialBackOff) Reset() {
115115
// Randomized interval = RetryInterval * (1 ± RandomizationFactor)
116116
func (b *ExponentialBackOff) NextBackOff() time.Duration {
117117
// Make sure we have not gone over the maximum elapsed time.
118-
if b.MaxElapsedTime != 0 && b.GetElapsedTime() > b.MaxElapsedTime {
118+
elapsed := b.GetElapsedTime()
119+
next := getRandomValueFromInterval(b.RandomizationFactor, rand.Float64(), b.currentInterval)
120+
b.incrementCurrentInterval()
121+
if b.MaxElapsedTime != 0 && elapsed+next > b.MaxElapsedTime {
119122
return b.Stop
120123
}
121-
defer b.incrementCurrentInterval()
122-
return getRandomValueFromInterval(b.RandomizationFactor, rand.Float64(), b.currentInterval)
124+
return next
123125
}
124126

125127
// GetElapsedTime returns the elapsed time since an ExponentialBackOff instance

exponential_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestBackOffOverflow(t *testing.T) {
108108
exp.Reset()
109109

110110
exp.NextBackOff()
111-
// Assert that when an overflow is possible the current varerval time.Duration is set to the max varerval time.Duration .
111+
// Assert that when an overflow is possible, the current varerval time.Duration is set to the max varerval time.Duration.
112112
assertEquals(t, testMaxInterval, exp.currentInterval)
113113
}
114114

0 commit comments

Comments
 (0)