Avoid integer overflow leading to panic in rand.N
This commit is contained in:
parent
9ba1d4d915
commit
e9c9ef8b43
|
@ -60,9 +60,8 @@ func (e *verifyEntriesError) Error() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func withRetry(ctx context.Context, maxRetries int, f func() error) error {
|
func withRetry(ctx context.Context, maxRetries int, f func() error) error {
|
||||||
const minSleep = 1 * time.Second
|
|
||||||
const maxSleep = 10 * time.Minute
|
|
||||||
|
|
||||||
|
minSleep := 1 * time.Second
|
||||||
numRetries := 0
|
numRetries := 0
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
err := f()
|
err := f()
|
||||||
|
@ -72,12 +71,11 @@ func withRetry(ctx context.Context, maxRetries int, f func() error) error {
|
||||||
if maxRetries != -1 && numRetries >= maxRetries {
|
if maxRetries != -1 && numRetries >= maxRetries {
|
||||||
return fmt.Errorf("%w (retried %d times)", err, numRetries)
|
return fmt.Errorf("%w (retried %d times)", err, numRetries)
|
||||||
}
|
}
|
||||||
upperBound := min(minSleep*(1<<numRetries)*2, maxSleep)
|
sleepTime := minSleep + mathrand.N(minSleep)
|
||||||
lowerBound := max(upperBound/2, minSleep)
|
|
||||||
sleepTime := lowerBound + mathrand.N(upperBound-lowerBound)
|
|
||||||
if err := sleep(ctx, sleepTime); err != nil {
|
if err := sleep(ctx, sleepTime); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
minSleep = min(minSleep*2, 5*time.Minute)
|
||||||
numRetries++
|
numRetries++
|
||||||
}
|
}
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
|
|
Loading…
Reference in New Issue