/** * Circuit Breaker Pattern Implementation * * Provides fault tolerance for external service calls by preventing * cascading failures and allowing systems to recover gracefully. * * States: * - CLOSED: Normal operation, requests pass through * - OPEN: Failures exceeded threshold, requests fail fast * - HALF_OPEN: Testing if service recovered, limited requests allowed * * @example * ```typescript * const breaker = new CircuitBreaker({ * failureThreshold: 5, * resetTimeout: 30000, * name: 'wordpress-api' * }); * * try { * const result = await breaker.execute(() => apiCall()); * } catch (error) { * if (error instanceof CircuitBreakerOpenError) { * // Circuit is open, use fallback * } * } * ``` */ /** * Circuit breaker states */ /** * Circuit state values */ export declare const CircuitState: { readonly CLOSED: "CLOSED"; readonly OPEN: "OPEN"; readonly HALF_OPEN: "HALF_OPEN"; }; /** * Circuit state type */ export type CircuitStateType = (typeof CircuitState)[keyof typeof CircuitState]; /** * Circuit breaker configuration options */ export interface CircuitBreakerOptions { /** Name for logging and identification */ name: string; /** Number of failures before opening circuit (default: 5) */ failureThreshold?: number; /** Time in ms before attempting to close circuit (default: 30000) */ resetTimeout?: number; /** Number of successful calls needed to close circuit from half-open (default: 2) */ successThreshold?: number; /** Time window in ms to count failures (default: 60000) */ failureWindow?: number; /** Timeout for individual operations in ms (default: 30000) */ timeout?: number; /** Function to determine if an error should trip the breaker */ isFailure?: (error: Error) => boolean; /** Callback when circuit opens */ onOpen?: (name: string, failures: number) => void; /** Callback when circuit closes */ onClose?: (name: string) => void; /** Callback when circuit enters half-open state */ onHalfOpen?: (name: string) => void; } /** * Circuit breaker statistics */ export interface CircuitBreakerStats { state: CircuitStateType; failures: number; successes: number; lastFailure: Date | null; lastSuccess: Date | null; totalRequests: number; failedRequests: number; successfulRequests: number; rejectedRequests: number; timeInCurrentState: number; } /** * Error thrown when circuit breaker is open */ export declare class CircuitBreakerOpenError extends Error { readonly circuitName: string; readonly resetTime: number; constructor(circuitName: string, resetTime: number); } /** * Error thrown when operation times out */ export declare class CircuitBreakerTimeoutError extends Error { readonly circuitName: string; readonly timeout: number; constructor(circuitName: string, timeout: number); } /** * Circuit Breaker Implementation */ export declare class CircuitBreaker { private state; private failures; private successes; private lastStateChange; private lastFailure; private lastSuccess; private totalRequests; private failedRequests; private successfulRequests; private rejectedRequests; private readonly name; private readonly failureThreshold; private readonly resetTimeout; private readonly successThreshold; private readonly failureWindow; private readonly timeout; private readonly isFailure; private readonly onOpen; private readonly onClose; private readonly onHalfOpen; private readonly logger; constructor(options: CircuitBreakerOptions); /** * Execute an operation through the circuit breaker */ execute(operation: () => Promise): Promise; /** * Execute operation with timeout */ private executeWithTimeout; /** * Handle successful operation */ private onSuccess; /** * Handle operation error */ private onError; /** * Record a failure */ private recordFailure; /** * Remove failures outside the time window */ private cleanOldFailures; /** * Check if we should attempt to reset (transition to half-open) */ private shouldAttemptReset; /** * Get remaining time until reset attempt */ private getRemainingResetTime; /** * Transition to a new state */ private transitionTo; /** * Default failure detection - all errors are failures except timeouts and circuit breaker errors */ private defaultIsFailure; /** * Get current circuit breaker statistics */ getStats(): CircuitBreakerStats; /** * Get current state */ getState(): CircuitStateType; /** * Check if circuit is allowing requests */ isAvailable(): boolean; /** * Force circuit to open state (for testing or manual intervention) */ forceOpen(): void; /** * Force circuit to closed state (for testing or manual intervention) */ forceClose(): void; /** * Reset circuit breaker to initial state */ reset(): void; } /** * Circuit Breaker Registry * Manages multiple circuit breakers for different services */ export declare class CircuitBreakerRegistry { private static instance; private breakers; private readonly logger; private constructor(); /** * Get singleton instance */ static getInstance(): CircuitBreakerRegistry; /** * Get or create a circuit breaker */ getBreaker(options: CircuitBreakerOptions): CircuitBreaker; /** * Get existing circuit breaker by name */ get(name: string): CircuitBreaker | undefined; /** * Get all circuit breaker statistics */ getAllStats(): Record; /** * Get health summary of all circuit breakers */ getHealthSummary(): { total: number; closed: number; open: number; halfOpen: number; healthy: boolean; }; /** * Reset all circuit breakers */ resetAll(): void; /** * Remove a circuit breaker */ remove(name: string): boolean; /** * Clear all circuit breakers */ clear(): void; } /** * Create a circuit breaker with default WordPress API settings */ export declare function createWordPressCircuitBreaker(siteId: string, options?: Partial): CircuitBreaker; //# sourceMappingURL=CircuitBreaker.d.ts.map