Skip to content

Commit 5c2bc21

Browse files
committed
Add jitter
1 parent 8e6a547 commit 5c2bc21

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

cmd/convertor/builder/builder_utils.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"fmt"
2626
"io"
2727
"math"
28+
"math/rand"
2829
"os"
2930
"path"
3031
"time"
@@ -88,8 +89,15 @@ func retryWithBackoff(ctx context.Context, maxRetries int, operation func() erro
8889
return lastErr
8990
}
9091

91-
// Exponential backoff: base delay of 1s, max 30s
92-
backoffDelay := time.Duration(math.Min(float64(time.Second)*math.Pow(2, float64(attempt)), float64(30*time.Second)))
92+
// Exponential backoff with random jitter: base delay of 1s, max 30s
93+
baseDelay := time.Duration(math.Min(float64(time.Second)*math.Pow(2, float64(attempt)), float64(30*time.Second)))
94+
// Add random jitter: ±25% of the base delay
95+
jitter := time.Duration(float64(baseDelay) * (rand.Float64() - 0.5) * 0.5)
96+
backoffDelay := baseDelay + jitter
97+
// Ensure minimum delay of 500ms
98+
if backoffDelay < 500*time.Millisecond {
99+
backoffDelay = 500 * time.Millisecond
100+
}
93101
logrus.Infof("received retryable error, retrying in %v (attempt %d/%d): %v", backoffDelay, attempt+1, maxRetries, lastErr)
94102

95103
select {

0 commit comments

Comments
 (0)