/** * Possible states for a circuit breaker */ export declare enum CircuitBreakerState { CLOSED = "CLOSED", OPEN = "OPEN", HALF_OPEN = "HALF_OPEN" } /** * Configuration for the circuit breaker */ export interface CircuitBreakerConfig { /** Number of failures before the circuit opens (default: 5) */ failureThreshold: number; /** Time in milliseconds before attempting recovery from OPEN state (default: 30000) */ resetTimeoutMs: number; /** Number of consecutive successes in HALF_OPEN required to close the circuit (default: 3) */ successThreshold: number; } /** * Callback invoked when the circuit breaker changes state */ export type CircuitBreakerStateChangeCallback = (from: CircuitBreakerState, to: CircuitBreakerState, serviceName: string) => void; /** * Default circuit breaker configuration */ export declare const DEFAULT_CIRCUIT_BREAKER_CONFIG: CircuitBreakerConfig; /** * Error thrown when a circuit breaker is open and rejecting calls */ export declare class CircuitBreakerOpenError extends Error { readonly serviceName: string; readonly resetTimeoutMs: number; constructor(serviceName: string, resetTimeoutMs: number); } /** * Generic circuit breaker implementation for external service calls. * * The circuit breaker monitors failures and transitions through three states: * - CLOSED: Normal operation. Calls pass through. Failures are counted. * - OPEN: Too many failures. Calls are rejected immediately without executing. * - HALF_OPEN: After a reset timeout, allows a limited number of trial calls * to test whether the service has recovered. * * @example * const breaker = new CircuitBreaker('slack-api'); * const result = await breaker.execute(() => sendSlackMessage(channel, msg)); */ export declare class CircuitBreaker { readonly serviceName: string; private state; private failureCount; private successCount; private lastFailureTime; private readonly config; private onStateChange; constructor(serviceName: string, config?: Partial, onStateChange?: CircuitBreakerStateChangeCallback); /** * Returns the current state of the circuit breaker. */ getState(): CircuitBreakerState; /** * Returns the current failure count. */ getFailureCount(): number; /** * Returns the current success count (relevant in HALF_OPEN state). */ getSuccessCount(): number; /** * Resets the circuit breaker to its initial CLOSED state. */ reset(): void; /** * Executes the provided function through the circuit breaker. * * - In CLOSED state: executes normally, tracking failures. * - In OPEN state: rejects immediately with CircuitBreakerOpenError * unless the reset timeout has elapsed, in which case it transitions to HALF_OPEN. * - In HALF_OPEN state: allows trial calls, tracking successes to decide * whether to close or re-open the circuit. * * @template T - The return type of the wrapped function * @param fn - The async function to execute * @returns The result of the function call * @throws CircuitBreakerOpenError if the circuit is open * @throws The original error if the function call fails */ execute(fn: () => Promise): Promise; /** * Sets or replaces the state change callback. */ setOnStateChange(callback: CircuitBreakerStateChangeCallback): void; private shouldAttemptReset; private onSuccess; private onFailure; private transitionTo; private notifyStateChange; }