@@ -11,7 +11,13 @@ export type RetryConfig = {
1111 attempts ?: number ;
1212 minDelayMs ?: number ;
1313 maxDelayMs ?: number ;
14- jitter ?: number ;
14+ /**
15+ * Delay spread strategy. A fraction (0-1) spreads proportionally around the
16+ * backoff delay (existing behavior). `"full"` draws uniformly from
17+ * [delay, 2*delay): the backoff delay stays a hard floor and `maxDelayMs`
18+ * clamps after the draw, so capped attempts land exactly on the cap.
19+ */
20+ jitter ?: number | "full" ;
1521} ;
1622
1723/** Metadata emitted before a retry attempt sleeps and reruns the operation. */
@@ -30,6 +36,8 @@ export type RetryOptions = RetryConfig & {
3036 retryAfterMs ?: ( err : unknown ) => number | undefined ;
3137 retryAfterMaxDelayMs ?: number ;
3238 onRetry ?: ( info : RetryInfo ) => void ;
39+ /** Random fraction source in [0, 1); injectable for deterministic tests. */
40+ random ?: ( ) => number ;
3341} ;
3442
3543const DEFAULT_RETRY_CONFIG = {
@@ -79,6 +87,17 @@ function resolveRetryDelayMs(value: number): number {
7987 return resolveTimerTimeoutMs ( value , 0 , 0 ) ;
8088}
8189
90+ function resolveJitterConfig ( value : unknown , fallback : number | "full" ) : number | "full" {
91+ if ( value === "full" ) {
92+ return "full" ;
93+ }
94+ const fraction = asFiniteNumber ( value ) ;
95+ if ( fraction === undefined ) {
96+ return fallback ;
97+ }
98+ return Math . min ( Math . max ( fraction , 0 ) , 1 ) ;
99+ }
100+
82101/** Resolves retry config overrides into clamped timer-safe settings. */
83102export function resolveRetryConfig (
84103 defaults : Required < RetryConfig > = DEFAULT_RETRY_CONFIG ,
@@ -95,13 +114,34 @@ export function resolveRetryConfig(
95114 minDelayMs ,
96115 resolveRetryDelayMs ( Math . round ( clampNumber ( overrides ?. maxDelayMs , defaults . maxDelayMs , 0 ) ) ) ,
97116 ) ;
98- const jitter = clampNumber ( overrides ?. jitter , defaults . jitter , 0 , 1 ) ;
117+ const jitter = resolveJitterConfig ( overrides ?. jitter , defaults . jitter ) ;
99118 return { attempts, minDelayMs, maxDelayMs, jitter } ;
100119}
101120
102121type JitterMode = "symmetric" | "positive" ;
103122
104- function applyJitter ( delayMs : number , jitter : number , mode : JitterMode = "symmetric" ) : number {
123+ function applyJitter (
124+ delayMs : number ,
125+ jitter : number | "full" ,
126+ mode : JitterMode ,
127+ random : ( ) => number ,
128+ ) : number {
129+ if ( jitter === "full" ) {
130+ if ( mode === "symmetric" ) {
131+ // Unsatisfiable over-cap Retry-After: an upward draw would be erased by
132+ // the caller's cap clamp and every client would land in lockstep at the
133+ // cap, so draw downward across one half-period instead. That preserves
134+ // spread (the invariant the numeric symmetric fallback below protects)
135+ // while staying as close to the server's hint as the cap allows.
136+ return Math . max ( 0 , Math . round ( delayMs * ( 0.5 + random ( ) * 0.5 ) ) ) ;
137+ }
138+ // Full jitter draws uniformly from [delay, 2*delay): the backoff delay is
139+ // a hard floor (never fire early), which also keeps honorable Retry-After
140+ // lower bounds. Callers clamp `maxDelayMs` after this, so capped attempts
141+ // land exactly on the cap (same boundary trade-off as `positive` mode
142+ // below). Ceil preserves the floor contract for fractional bases.
143+ return Math . max ( 0 , Math . ceil ( delayMs * ( 1 + random ( ) ) ) ) ;
144+ }
105145 if ( jitter <= 0 ) {
106146 return delayMs ;
107147 }
@@ -111,7 +151,7 @@ function applyJitter(delayMs: number, jitter: number, mode: JitterMode = "symmet
111151 // lower bound the caller must respect (for example a server-supplied
112152 // Retry-After) so concurrent clients still spread without ever dipping
113153 // below the caller's floor.
114- const fraction = generateSecureFraction ( ) ;
154+ const fraction = random ( ) ;
115155 const offset = mode === "positive" ? fraction * jitter : ( fraction * 2 - 1 ) * jitter ;
116156 const raw = delayMs * ( 1 + offset ) ;
117157 // Rounding choice preserves the mode's contract. `positive` guarantees
@@ -164,6 +204,7 @@ export async function retryAsync<T>(
164204 resolveRetryDelayMs ( Math . round ( clampNumber ( options . retryAfterMaxDelayMs , maxDelayMs , 0 ) ) ) ,
165205 ) ;
166206 const jitter = resolved . jitter ;
207+ const random = options . random ?? generateSecureFraction ;
167208 const shouldRetry = options . shouldRetry ?? ( ( ) => true ) ;
168209 const attemptErrors : unknown [ ] = [ ] ;
169210
@@ -210,7 +251,13 @@ export async function retryAsync<T>(
210251 // unsatisfiable and we gain spread without adding a violation.
211252 const canHonorRetryAfter =
212253 hasRetryAfter && typeof retryAfterMs === "number" && retryAfterMs <= delayCap ;
213- delay = applyJitter ( delay , jitter , canHonorRetryAfter ? "positive" : "symmetric" ) ;
254+ // Full jitter's upward draw is inherently positive, so it serves both
255+ // plain backoff and honorable Retry-After floors; only the unsatisfiable
256+ // over-cap hint must switch to the symmetric downward spread. Numeric
257+ // jitter keeps the original positive/symmetric split.
258+ const overCapRetryAfter = hasRetryAfter && ! canHonorRetryAfter ;
259+ const wantsPositiveDraw = jitter === "full" ? ! overCapRetryAfter : canHonorRetryAfter ;
260+ delay = applyJitter ( delay , jitter , wantsPositiveDraw ? "positive" : "symmetric" , random ) ;
214261 delay = Math . min ( Math . max ( delay , minDelayMs ) , delayCap ) ;
215262
216263 options . onRetry ?.( {
0 commit comments