Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feature: add FullJitterBackoffDelay
  • Loading branch information
amirreza.fahimidero committed May 30, 2025
commit 10df0c15507f53a58c22876846d15c711afe70a9
168 changes: 168 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc {
}
}

// FullJitterBackoffDelay is a DelayTypeFunc that calculates delay using exponential backoff
// with full jitter. The delay is a random value between 0 and the current backoff ceiling.
// Formula: sleep = random_between(0, min(cap, base * 2^attempt))
// It uses config.Delay as the base delay and config.MaxDelay as the cap.
func FullJitterBackoffDelay(n uint, err error, config *Config) time.Duration {
// Calculate the exponential backoff ceiling for the current attempt
backoffCeiling := float64(config.delay) * math.Pow(2, float64(n))
currentCap := float64(config.maxDelay)

// If MaxDelay is set and backoffCeiling exceeds it, cap at MaxDelay
if currentCap > 0 && backoffCeiling > currentCap {
backoffCeiling = currentCap
}

// Ensure backoffCeiling is at least 0
if backoffCeiling < 0 {
backoffCeiling = 0
}

// Add jitter: random value between 0 and backoffCeiling
// rand.Int63n panics if argument is <= 0
if backoffCeiling <= 0 {
return 0 // No delay if ceiling is zero or negative
}

jitter := rand.Int63n(int64(backoffCeiling)) // #nosec G404 -- Using math/rand is acceptable for non-security critical jitter.
return time.Duration(jitter)
}

// OnRetry function callback are called each retry
//
// log each retry example:
Expand Down
69 changes: 69 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"os"
"testing"
"time"
Expand Down Expand Up @@ -642,3 +643,71 @@ func TestIsRecoverable(t *testing.T) {
err = fmt.Errorf("wrapping: %w", err)
assert.False(t, IsRecoverable(err))
}

func TestFullJitterBackoffDelay(t *testing.T) {
// Seed for predictable randomness in tests
// In real usage, math/rand is auto-seeded in Go 1.20+ or should be seeded once at program start.
// For library test predictability, local seeding is fine.
// However, retry-go's RandomDelay uses global math/rand without explicit seeding in tests.
// Let's follow the existing pattern of not explicitly seeding in each test for now,
// assuming test runs are isolated enough or that exact delay values aren't asserted,
// but rather ranges or properties.

baseDelay := 50 * time.Millisecond
maxDelay := 500 * time.Millisecond

config := &Config{
delay: baseDelay,
maxDelay: maxDelay,
// other fields can be zero/default for this test
}

attempts := []uint{0, 1, 2, 3, 4, 5, 6, 10}

for _, n := range attempts {
delay := FullJitterBackoffDelay(n, errors.New("test error"), config)

expectedMaxCeiling := float64(baseDelay) * math.Pow(2, float64(n))
if expectedMaxCeiling > float64(maxDelay) {
expectedMaxCeiling = float64(maxDelay)
}

assert.True(t, delay >= 0, "Delay should be non-negative. Got: %v for attempt %d", delay, n)
assert.True(t, delay <= time.Duration(expectedMaxCeiling),
"Delay %v should be less than or equal to current backoff ceiling %v for attempt %d", delay, time.Duration(expectedMaxCeiling), n)

t.Logf("Attempt %d: BaseDelay=%v, MaxDelay=%v, Calculated Ceiling=~%v, Actual Delay=%v",
n, baseDelay, maxDelay, time.Duration(expectedMaxCeiling), delay)

// Test with MaxDelay disabled (0)
configNoMax := &Config{delay: baseDelay, maxDelay: 0}
delayNoMax := FullJitterBackoffDelay(n, errors.New("test error"), configNoMax)
expectedCeilingNoMax := float64(baseDelay) * math.Pow(2, float64(n))
if expectedCeilingNoMax > float64(10*time.Minute) { // Avoid overflow for very large N
expectedCeilingNoMax = float64(10 * time.Minute)
}
assert.True(t, delayNoMax >= 0, "Delay (no max) should be non-negative. Got: %v for attempt %d", delayNoMax, n)
assert.True(t, delayNoMax <= time.Duration(expectedCeilingNoMax),
"Delay (no max) %v should be less than or equal to current backoff ceiling %v for attempt %d", delayNoMax, time.Duration(expectedCeilingNoMax), n)
}

// Test case where baseDelay might be zero
configZeroBase := &Config{delay: 0, maxDelay: maxDelay}
delayZeroBase := FullJitterBackoffDelay(0, errors.New("test error"), configZeroBase)
assert.Equal(t, time.Duration(0), delayZeroBase, "Delay with zero base delay should be 0")

delayZeroBaseAttempt1 := FullJitterBackoffDelay(1, errors.New("test error"), configZeroBase)
assert.Equal(t, time.Duration(0), delayZeroBaseAttempt1, "Delay with zero base delay (attempt > 0) should be 0")

// Test with very small base delay
smallBaseDelay := 1 * time.Nanosecond
configSmallBase := &Config{delay: smallBaseDelay, maxDelay: 100 * time.Nanosecond}
for i := uint(0); i < 5; i++ {
d := FullJitterBackoffDelay(i, errors.New("test"), configSmallBase)
ceil := float64(smallBaseDelay) * math.Pow(2, float64(i))
if ceil > 100 {
ceil = 100
}
assert.True(t, d <= time.Duration(ceil))
}
}