MCPcopy Index your code
hub / github.com/riverqueue/river / ExponentialBackoff

Function ExponentialBackoff

rivershared/util/serviceutil/service_util.go:66–73  ·  view source on GitHub ↗

ExponentialBackoff returns a duration for a reasonable exponential backoff interval for a service based on the given attempt number, which can then be fed into CancellableSleep to perform the sleep. Uses a 2**N second algorithm, +/- 10% random jitter. Sleep is cancelled if the given context is cance

(attempt, maxAttemptsBeforeReset int)

Source from the content-addressed store, hash-verified

64// - 32s
65// - 64s.
66func ExponentialBackoff(attempt, maxAttemptsBeforeReset int) time.Duration {
67 retrySeconds := exponentialBackoffSecondsWithoutJitter(attempt, maxAttemptsBeforeReset)
68
69 // Jitter number of seconds +/- 10%.
70 retrySeconds += retrySeconds * (rand.Float64()*0.2 - 0.1)
71
72 return timeutil.SecondsAsDuration(retrySeconds)
73}
74
75func exponentialBackoffSecondsWithoutJitter(attempt, maxAttemptsBeforeReset int) float64 {
76 // It's easier for callers and more intuitive if attempt starts at one, but

Callers 7

tryStartMethod · 0.92
runFollowerStateMethod · 0.92
runLeaderStateMethod · 0.92
attemptResignLoopMethod · 0.92
StartMethod · 0.92
withRetriesFunction · 0.92
TestExponentialBackoffFunction · 0.85

Calls 2

SecondsAsDurationFunction · 0.92

Tested by 1

TestExponentialBackoffFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…