UNPKG

axios-retryer

Version:

axios-retryer is an advanced Axios request manager offering intelligent retry logic with token refresh, concurrency control, priority queuing, and a flexible plugin architecture, all built with TypeScript for robust HTTP client integrations.

86 lines (85 loc) 3.15 kB
import { RetryManager } from '../../core/RetryManager'; import { RetryPlugin } from '../../types'; /** * Configuration options for the Circuit Breaker behavior. */ export interface CircuitBreakerOptions { /** * Number of consecutive failures required to trip the circuit. * Once this threshold is exceeded, the circuit transitions from `CLOSED` to `OPEN`. */ failureThreshold: number; /** * Duration (in milliseconds) the circuit remains in the `OPEN` state * before allowing a test request in the `HALF_OPEN` state. */ openTimeout: number; /** * Maximum number of test requests allowed in `HALF_OPEN` state * before deciding to either reset (back to `CLOSED`) or trip again to `OPEN`. */ halfOpenMax: number; } /** * CircuitBreakerPlugin * * This plugin implements the Circuit Breaker pattern by temporarily stopping retries if a service * appears to be down. When too many consecutive failures occur, the circuit "trips" (opens) and all * incoming requests are immediately rejected (fail-fast). After a cool-down period, the circuit transitions * to a half-open state and allows a limited number of test requests. A successful test resets the circuit, * while a failure reopens it. * * @implements {RetryPlugin} */ export declare class CircuitBreakerPlugin implements RetryPlugin { readonly name = "CircuitBreakerPlugin"; readonly version = "1.0.0"; private _options; private _state; private _failureCount; private _halfOpenCount; private _nextAttempt; private _requestInterceptorId?; private _responseInterceptorId?; private _manager; static STATES: { CLOSED: string; OPEN: string; HALF_OPEN: string; }; /** * Creates an instance of CircuitBreakerPlugin. * @param {Partial<CircuitBreakerOptions>} [options] - Configuration options. * - failureThreshold: Number of consecutive failures to trip the circuit (default: 5). * - openTimeout: Time (in ms) the circuit remains open before transitioning to half-open (default: 30000). * - halfOpenMax: Maximum number of test requests allowed in the half-open state (default: 1). */ constructor(options?: Partial<CircuitBreakerOptions>); /** * Initializes the plugin by setting up request and response interceptors. * Called when the plugin is attached. * * @param {RetryManager} manager - The RetryManager instance. */ initialize(manager: RetryManager): void; /** * Called before the plugin is removed. * Ejects the interceptors. * * @param {RetryManager} manager - The RetryManager instance. */ onBeforeDestroyed(manager: RetryManager): void; /** * Trips the circuit by transitioning it to OPEN state. * Sets the next attempt time based on the openTimeout option. */ private _trip; /** * Resets the circuit by transitioning it back to CLOSED state. */ private _reset; /** * Transitions the circuit to HALF_OPEN state, allowing test requests. */ private _transitionToHalfOpen; }