/** * Circuit Breaker Module * * Implements the circuit breaker pattern for fault tolerance in distributed systems. * Prevents cascading failures by temporarily blocking requests to failing services. */ import { Logger } from 'winston'; import { EventEmitter } from 'events'; /** * Circuit breaker states */ export declare enum CircuitState { CLOSED = "CLOSED",// Normal operation, requests pass through OPEN = "OPEN",// Circuit broken, requests blocked HALF_OPEN = "HALF_OPEN" } /** * Circuit breaker configuration */ export interface CircuitBreakerConfig { /** Name of the circuit for identification */ name: string; /** Number of failures before opening circuit */ failureThreshold: number; /** Success threshold to close circuit from half-open */ successThreshold: number; /** Time window for counting failures (ms) */ failureCountWindow: number; /** Time to wait before attempting recovery (ms) */ resetTimeout: number; /** Percentage of requests to fail before opening (0-100) */ failureRateThreshold?: number; /** Minimum number of requests before applying rate threshold */ volumeThreshold?: number; /** Optional timeout for operations (ms) */ operationTimeout?: number; /** Function to determine if an error counts as failure */ isFailure?: (error: any) => boolean; /** Fallback function when circuit is open */ fallback?: () => T | Promise; } /** * Default circuit breaker configuration */ export declare const DEFAULT_CIRCUIT_CONFIG: Omit; /** * Circuit breaker statistics */ export interface CircuitStats { state: CircuitState; failures: number; successes: number; totalCalls: number; lastFailureTime?: Date; lastSuccessTime?: Date; lastStateChange: Date; consecutiveSuccesses: number; consecutiveFailures: number; failureRate: number; } /** * Circuit breaker implementation */ export declare class CircuitBreaker extends EventEmitter { private state; private readonly config; private readonly logger?; private stats; private resetTimer?; private readonly requestHistory; private lastStateChangeTime; constructor(config: CircuitBreakerConfig, logger?: Logger); /** * Execute an operation through the circuit breaker */ execute(operation: () => Promise): Promise; /** * Get current circuit statistics */ getStats(): CircuitStats; /** * Get current state */ getState(): CircuitState; /** * Manually reset the circuit breaker */ reset(): void; /** * Force open the circuit */ open(): void; /** * Initialize statistics */ private initializeStats; /** * Execute operation with timeout */ private executeWithTimeout; /** * Record successful operation */ private recordSuccess; /** * Record failed operation */ private recordFailure; /** * Check if circuit should open based on failure conditions */ private shouldOpenCircuit; /** * Add request to history */ private addToHistory; /** * Get recent requests within the time window */ private getRecentRequests; /** * Calculate current failure rate */ private calculateFailureRate; /** * Determine if error should count as failure */ private shouldCountAsFailure; /** * Transition to new state */ private transition; /** * Schedule circuit reset attempt */ private scheduleReset; /** * Clear reset timer */ private clearResetTimer; /** * Cleanup resources */ dispose(): void; } /** * Circuit breaker factory for managing multiple circuits */ export declare class CircuitBreakerFactory { private readonly circuits; private readonly logger?; constructor(logger?: Logger); /** * Get or create a circuit breaker */ getOrCreate(config: CircuitBreakerConfig): CircuitBreaker; /** * Get existing circuit breaker */ get(name: string): CircuitBreaker | undefined; /** * Get all circuit breakers */ getAll(): Map; /** * Get statistics for all circuits */ getAllStats(): Record; /** * Reset all circuits */ resetAll(): void; /** * Dispose all circuits */ dispose(): void; } /** * Decorator for adding circuit breaker to methods */ export declare function WithCircuitBreaker(config: Omit): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor; //# sourceMappingURL=CircuitBreaker.d.ts.map