RetryManager
Defined in: packages/core/src/data/retry-manager.ts:151
Retry manager with exponential backoff and jitter.
Remarks
Section titled “Remarks”Implements exponential backoff with the formula:
delay = min(initialDelay * (backoffFactor ^ (attempt - 1)), maxDelay)With jitter applied as:
delay = delay + (random(-1, 1) * delay * jitterFactor)Jitter helps prevent thundering herd problems when multiple clients retry simultaneously.
Example
Section titled “Example”const retry = new RetryManager({ maxRetries: 5, initialDelay: 1000, backoffFactor: 2,});
try { const result = await retry.execute( async () => { const response = await fetch('https://api.example.com/data'); if (!response.ok) throw new Error('Request failed'); return response.json(); }, { onRetry: (attempt, delay, error) => { console.log(`Retry ${attempt} in ${delay}ms: ${error.message}`); }, } ); console.log('Success:', result);} catch (error) { console.error('All retries failed:', error);}Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new RetryManager(
config?):RetryManager
Defined in: packages/core/src/data/retry-manager.ts:168
Create a new RetryManager instance
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config? | Partial<RetryConfig> | Retry configuration options |
Returns
Section titled “Returns”RetryManager
Methods
Section titled “Methods”calculateDelay()
Section titled “calculateDelay()”calculateDelay(
attempt):number
Defined in: packages/core/src/data/retry-manager.ts:253
Calculate delay for a given attempt using exponential backoff
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
attempt | number | Current attempt number (1-indexed) |
Returns
Section titled “Returns”number
Delay in milliseconds
Example
Section titled “Example”const retry = new RetryManager({ initialDelay: 1000, backoffFactor: 2 });console.log(retry.calculateDelay(1)); // ~1000msconsole.log(retry.calculateDelay(2)); // ~2000msconsole.log(retry.calculateDelay(3)); // ~4000msexecute()
Section titled “execute()”execute<
T>(fn,callbacks?):Promise<T>
Defined in: packages/core/src/data/retry-manager.ts:196
Execute a function with retry logic
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
T | Return type of the function |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn | () => Promise<T> | Async function to execute with retries |
callbacks? | RetryCallbacks | Optional lifecycle callbacks |
Returns
Section titled “Returns”Promise<T>
Promise that resolves with the function’s result
Throws
Section titled “Throws”When all retry attempts fail
Example
Section titled “Example”const data = await retry.execute( () => fetchData(url), { isRetryable: (error) => { // Don't retry 4xx errors except 429 (rate limit) if (error.message.includes('429')) return true; if (error.message.match(/4\d\d/)) return false; return true; }, });reset()
Section titled “reset()”reset():
void
Defined in: packages/core/src/data/retry-manager.ts:281
Reset internal state
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”Currently this class is stateless, but this method is provided for API consistency and future extensibility.