(
attempt: number,
options: ExponentialBackoffOptions = {},
)
| 37 | }; |
| 38 | |
| 39 | export function exponentialBackoffDelay( |
| 40 | attempt: number, |
| 41 | options: ExponentialBackoffOptions = {}, |
| 42 | ): number { |
| 43 | const { |
| 44 | minDelay = 100, |
| 45 | maxDelay = 10000, |
| 46 | maxAttempts = 5, |
| 47 | jitter = 0.25, |
| 48 | } = options; |
| 49 | |
| 50 | if (attempt === 0) return 0; |
| 51 | |
| 52 | const base = (maxDelay / minDelay) ** (1 / (maxAttempts - 1)); |
| 53 | const delay = minDelay * base ** (attempt - 1); |
| 54 | |
| 55 | if (jitter > 0) { |
| 56 | const jitterRange = delay * jitter; |
| 57 | const jitterOffset = jitterRange * (Math.random() * 2 - 1); |
| 58 | return delay + jitterOffset; |
| 59 | } |
| 60 | |
| 61 | // return clamped delay |
| 62 | return Math.max(minDelay, Math.min(maxDelay, delay)); |
| 63 | } |
| 64 | |
| 65 | const resetColor = "\x1b[0m"; |
| 66 |
no outgoing calls
no test coverage detected