@bernierllc/retry-policy
Version:
Atomic retry policy utilities with exponential backoff and jitter
69 lines (64 loc) • 2.03 kB
text/typescript
/*
Copyright (c) 2025 Bernier LLC
This file is licensed to the client under a limited-use license.
The client may use and modify this code *only within the scope of the project it was delivered for*.
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
*/
/**
* Configuration options for retry policies
*/
export interface RetryPolicyOptions {
/** Maximum number of retry attempts */
maxRetries: number;
/** Initial delay in milliseconds */
initialDelayMs: number;
/** Maximum delay in milliseconds */
maxDelayMs?: number;
/** Exponential backoff factor */
backoffFactor?: number;
/** Whether to apply jitter to delays */
jitter?: boolean;
/** Function to determine if an error should trigger a retry */
shouldRetry?: (error: any) => boolean;
/** Callback function called before each retry attempt */
onRetry?: (attempt: number, delay: number, error: any) => void;
/** Callback function called when all retries are exhausted */
onFailure?: (error: any) => void;
}
/**
* Result of a retry policy evaluation
*/
export interface RetryPolicyResult {
/** Whether the operation should be retried */
shouldRetry: boolean;
/** Delay in milliseconds before next retry */
delay: number;
/** Current attempt number */
attempt: number;
/** Whether this is the final attempt */
isFinalAttempt: boolean;
}
/**
* Jitter configuration for retry delays
*/
export interface JitterConfig {
/** Type of jitter to apply */
type: 'none' | 'full' | 'equal' | 'decorrelated';
/** Jitter factor (0-1) */
factor?: number;
}
/**
* Backoff strategy configuration
*/
export interface BackoffConfig {
/** Type of backoff strategy */
type: 'exponential' | 'linear' | 'constant';
/** Base delay in milliseconds */
baseDelay: number;
/** Maximum delay in milliseconds */
maxDelay: number;
/** Backoff factor for exponential strategies */
factor?: number;
/** Jitter configuration */
jitter?: JitterConfig;
}