Skip to content

Commit

Permalink
fix: exponential back function in retry (stackrox#7780)
Browse files Browse the repository at this point in the history
  • Loading branch information
stehessel authored Sep 18, 2023
1 parent 05de93c commit 5bd3b9f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/retry/retry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package retry

import "time"
import (
"math"
"time"
)

// WithRetry allows you to call an error returning function with a suite of retry options and modifiers.
func WithRetry(f func() error, retriableOptions ...OptionsModifier) error {
Expand Down Expand Up @@ -65,9 +68,9 @@ func (t *retryOptions) do() (err error) {
t.between(i)
}
if t.withExponentialBackoff {
// Back off by 100 milliseconds after the first attempt,
// 400 milliseconds after the second, 900 milliseconds after the third, etc.
time.Sleep(time.Duration(100*(i+1)*(i+1)) * time.Millisecond)
// First retry after 100 milliseconds, second retry after 200 milliseconds,
// third retry after 400 milliseconds. Backoff time doubles after each attempt.
time.Sleep(time.Duration(100*math.Pow(2, float64(i))) * time.Millisecond)
}
} else {
// If we can't retry then return.
Expand Down

0 comments on commit 5bd3b9f

Please sign in to comment.