fixing bug - possible overflow in overflow branch of FibonacciBackoff::Next
This commit is contained in:
parent
81979ef12d
commit
c8bf4e5e2b
@ -34,20 +34,23 @@ func (f *FibonacciBackoff) Next() time.Duration {
|
||||
if multiplier <= 0 {
|
||||
multiplier = DefaultMultiplier
|
||||
}
|
||||
pause := time.Duration(Fibonacci(f.Iteration))
|
||||
var pause time.Duration
|
||||
for {
|
||||
pause = time.Duration(Fibonacci(f.Iteration))
|
||||
if product, overflows := utilities.ProductWouldOverflowInt64(pause.Nanoseconds(), multiplier.Nanoseconds()); overflows {
|
||||
// cowardly refusal to overflow - return time.Duration(math.MaxInt64)
|
||||
// we can't calculate jitter, because that could overflow.
|
||||
// cowardly refusal to overflow - decrement Iteration until multiplication is safe.
|
||||
// realistically, this should never happen, we're talking about almost 300 years.
|
||||
f.foundMax = f.Iteration - 1
|
||||
f.Iteration = f.foundMax
|
||||
pause = time.Duration(Fibonacci(f.Iteration)) * multiplier
|
||||
continue
|
||||
} else if f.MaxPause > 0 && time.Duration(product) > f.MaxPause {
|
||||
f.foundMax = f.Iteration
|
||||
pause = f.MaxPause
|
||||
} else {
|
||||
pause = time.Duration(product)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// increment f.Iteration if appropriate
|
||||
maxIteration := int64(len(TheFibonacciSequence) - 1)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user