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 {
|
if multiplier <= 0 {
|
||||||
multiplier = DefaultMultiplier
|
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 {
|
if product, overflows := utilities.ProductWouldOverflowInt64(pause.Nanoseconds(), multiplier.Nanoseconds()); overflows {
|
||||||
// cowardly refusal to overflow - return time.Duration(math.MaxInt64)
|
// cowardly refusal to overflow - decrement Iteration until multiplication is safe.
|
||||||
// we can't calculate jitter, because that could overflow.
|
|
||||||
// realistically, this should never happen, we're talking about almost 300 years.
|
// realistically, this should never happen, we're talking about almost 300 years.
|
||||||
f.foundMax = f.Iteration - 1
|
f.foundMax = f.Iteration - 1
|
||||||
f.Iteration = f.foundMax
|
f.Iteration = f.foundMax
|
||||||
pause = time.Duration(Fibonacci(f.Iteration)) * multiplier
|
continue
|
||||||
} else if f.MaxPause > 0 && time.Duration(product) > f.MaxPause {
|
} else if f.MaxPause > 0 && time.Duration(product) > f.MaxPause {
|
||||||
f.foundMax = f.Iteration
|
f.foundMax = f.Iteration
|
||||||
pause = f.MaxPause
|
pause = f.MaxPause
|
||||||
} else {
|
} else {
|
||||||
pause = time.Duration(product)
|
pause = time.Duration(product)
|
||||||
}
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// increment f.Iteration if appropriate
|
// increment f.Iteration if appropriate
|
||||||
maxIteration := int64(len(TheFibonacciSequence) - 1)
|
maxIteration := int64(len(TheFibonacciSequence) - 1)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user