/** * Circuit breaker states */ export declare enum CircuitState { /** Circuit is closed, requests flow normally */ CLOSED = "closed", /** Circuit is open, requests are rejected immediately */ OPEN = "open", /** Circuit is half-open, testing if service recovered */ HALF_OPEN = "half_open" } /** * Circuit breaker configuration */ export interface CircuitBreakerConfig { /** Number of failures before opening circuit */ failureThreshold: number; /** Number of successes in half-open state before closing */ successThreshold: number; /** Time in milliseconds circuit stays open before attempting half-open */ timeout: number; /** Time window in milliseconds for counting failures */ rollingWindow: number; /** Whether circuit breaker is enabled */ enabled: boolean; } /** * Default circuit breaker configuration */ export declare const DEFAULT_CIRCUIT_BREAKER_CONFIG: CircuitBreakerConfig; /** * Circuit breaker error thrown when circuit is open */ export declare class CircuitBreakerOpenError extends Error { readonly circuitName: string; readonly nextAttemptTime: Date; constructor(circuitName: string, nextAttemptTime: Date); } /** * Circuit breaker for preventing cascading failures * * @example * ```typescript * const breaker = new CircuitBreaker('elasticsearch', { * failureThreshold: 5, * timeout: 60000, * }); * * try { * const result = await breaker.execute(async () => { * return await someElasticsearchOperation(); * }); * } catch (error) { * if (error instanceof CircuitBreakerOpenError) { * // Circuit is open, handle gracefully * } * } * ``` */ export declare class CircuitBreaker { private name; private config; private state; private failures; private successCount; private openedAt?; private nextAttemptTime?; constructor(name: string, config?: Partial); /** * Execute an operation through the circuit breaker */ execute(operation: () => Promise): Promise; /** * Handle successful operation */ private onSuccess; /** * Handle failed operation */ private onFailure; /** * Update circuit state based on time */ private updateState; /** * Transition to a new state */ private transitionTo; /** * Remove failures outside the rolling window */ private removeOldFailures; /** * Callback when state changes (can be overridden) */ protected onStateChange(from: CircuitState, to: CircuitState): void; /** * Get current circuit state */ getState(): CircuitState; /** * Get circuit statistics */ getStats(): { state: CircuitState; failureCount: number; successCount: number; openedAt?: Date; nextAttemptTime?: Date; }; /** * Manually open the circuit */ open(): void; /** * Manually close the circuit */ close(): void; /** * Reset the circuit breaker */ reset(): void; /** * Check if circuit is open */ isOpen(): boolean; /** * Check if circuit is closed */ isClosed(): boolean; /** * Check if circuit is half-open */ isHalfOpen(): boolean; }