@@ -158,6 +158,35 @@ func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc {
158158 }
159159}
160160
161+ // FullJitterBackoffDelay is a DelayTypeFunc that calculates delay using exponential backoff
162+ // with full jitter. The delay is a random value between 0 and the current backoff ceiling.
163+ // Formula: sleep = random_between(0, min(cap, base * 2^attempt))
164+ // It uses config.Delay as the base delay and config.MaxDelay as the cap.
165+ func FullJitterBackoffDelay (n uint , err error , config * Config ) time.Duration {
166+ // Calculate the exponential backoff ceiling for the current attempt
167+ backoffCeiling := float64 (config .delay ) * math .Pow (2 , float64 (n ))
168+ currentCap := float64 (config .maxDelay )
169+
170+ // If MaxDelay is set and backoffCeiling exceeds it, cap at MaxDelay
171+ if currentCap > 0 && backoffCeiling > currentCap {
172+ backoffCeiling = currentCap
173+ }
174+
175+ // Ensure backoffCeiling is at least 0
176+ if backoffCeiling < 0 {
177+ backoffCeiling = 0
178+ }
179+
180+ // Add jitter: random value between 0 and backoffCeiling
181+ // rand.Int63n panics if argument is <= 0
182+ if backoffCeiling <= 0 {
183+ return 0 // No delay if ceiling is zero or negative
184+ }
185+
186+ jitter := rand .Int63n (int64 (backoffCeiling )) // #nosec G404 -- Using math/rand is acceptable for non-security critical jitter.
187+ return time .Duration (jitter )
188+ }
189+
161190// OnRetry function callback are called each retry
162191//
163192// log each retry example:
0 commit comments