/** * Circuit Breaker Implementation * * Implements the circuit breaker pattern for preventing cascading failures. * Tracks failures and successes, automatically opening when thresholds are * exceeded and recovering through half-open state. * * @module execution/resilience/circuit-breaker */ import type { CircuitBreaker, CircuitState } from './types.js'; /** * CircuitBreakerManager - Manages multiple circuit breakers * * Maintains a collection of circuit breakers, typically one per task type * or service. Provides methods for checking state, recording outcomes, * and managing circuit lifecycle. * * @example * ```typescript * const manager = new CircuitBreakerManager(); * const breaker = manager.getOrCreate('issue-executor', { * failureThreshold: 5, * successThreshold: 2, * timeout: 60000, * }); * * if (manager.canExecute('issue-executor')) { * // Execute task * const success = await executeTask(); * if (success) { * manager.recordSuccess('issue-executor'); * } else { * manager.recordFailure('issue-executor', new Error('Task failed')); * } * } * ``` */ export declare class CircuitBreakerManager { private breakers; /** * Get an existing circuit breaker by name * * @param name - Circuit breaker name * @returns Circuit breaker or null if not found */ get(name: string): CircuitBreaker | null; /** * Get or create a circuit breaker * * If a circuit breaker with the given name exists, returns it. * Otherwise creates a new one with the provided configuration. * * @param name - Circuit breaker name (typically task type) * @param config - Configuration for new circuit breaker * @returns Circuit breaker instance */ getOrCreate(name: string, config?: CircuitBreaker['config']): CircuitBreaker; /** * Check if a circuit breaker allows execution * * Returns false if circuit is open and timeout hasn't elapsed yet. * Returns true for closed and half-open states. * * @param name - Circuit breaker name * @returns True if execution is allowed */ canExecute(name: string): boolean; /** * Record a successful execution * * Updates metrics and may transition circuit from half-open to closed * if success threshold is met. * * @param name - Circuit breaker name */ recordSuccess(name: string): void; /** * Record a failed execution * * Updates metrics and may transition circuit from closed/half-open to open * if failure threshold is met. * * @param name - Circuit breaker name * @param error - Error that occurred */ recordFailure(name: string, error: Error): void; /** * Reset a circuit breaker to closed state * * Clears all failure counts and returns circuit to closed state. * Useful for manual recovery or after fixing underlying issues. * * @param name - Circuit breaker name */ reset(name: string): void; /** * Get all circuit breakers * * @returns Map of circuit breaker name to breaker instance */ getAll(): Map; /** * Check if enough time has passed to transition from open to half-open * * @param breaker - Circuit breaker to check * @returns True if timeout has elapsed * @private */ private shouldTransitionToHalfOpen; /** * Get count of consecutive successes * * In a production implementation, this would track a sliding window * of recent attempts. For simplicity, we use total successful requests. * * @param breaker - Circuit breaker to check * @returns Number of consecutive successes * @private */ private getConsecutiveSuccesses; } /** * Create a new circuit breaker instance * * Helper function to create a properly configured circuit breaker. * * @param name - Circuit breaker name * @param config - Circuit breaker configuration * @returns New circuit breaker instance * * @example * ```typescript * const breaker = createCircuitBreaker('api-calls', { * failureThreshold: 10, * successThreshold: 3, * timeout: 30000, * }); * ``` */ export declare function createCircuitBreaker(name: string, config?: CircuitBreaker['config']): CircuitBreaker; /** * Check if a circuit breaker is in a specific state * * @param breaker - Circuit breaker to check * @param state - State to check for * @returns True if breaker is in the specified state */ export declare function isInState(breaker: CircuitBreaker, state: CircuitState): boolean; /** * Get the current state of a circuit breaker * * @param breaker - Circuit breaker to check * @returns Current state */ export declare function getState(breaker: CircuitBreaker): CircuitState; /** * Calculate failure rate for a circuit breaker * * @param breaker - Circuit breaker to analyze * @returns Failure rate (0-1) or 0 if no requests */ export declare function getFailureRate(breaker: CircuitBreaker): number; /** * Calculate success rate for a circuit breaker * * @param breaker - Circuit breaker to analyze * @returns Success rate (0-1) or 0 if no requests */ export declare function getSuccessRate(breaker: CircuitBreaker): number; //# sourceMappingURL=circuit-breaker.d.ts.map