UNPKG

deepsource-mcp-server

Version:
172 lines (171 loc) 4.53 kB
/** * @fileoverview Circuit breaker implementation for fault tolerance * This module implements the circuit breaker pattern to prevent cascade failures. */ /** * Circuit breaker states * @enum * @public */ export declare enum CircuitState { /** Normal operation - requests pass through */ CLOSED = "closed", /** Circuit is open - requests fail immediately */ OPEN = "open", /** Testing if service has recovered */ HALF_OPEN = "half-open" } /** * Circuit breaker configuration * @interface * @public */ export interface CircuitBreakerConfig { /** Number of failures before opening circuit */ failureThreshold: number; /** Time window for counting failures (ms) */ failureWindow: number; /** Time to wait before attempting recovery (ms) */ recoveryTimeout: number; /** Number of successful requests needed to close from half-open */ successThreshold: number; /** Maximum number of test requests in half-open state */ halfOpenMaxAttempts: number; } /** * Circuit breaker statistics * @interface * @public */ export interface CircuitBreakerStats { /** Current circuit state */ state: CircuitState; /** Number of failures in current window */ failureCount: number; /** Number of successful requests */ successCount: number; /** Total requests processed */ totalRequests: number; /** Time when circuit was last opened */ lastOpenTime?: number; /** Time when circuit was last closed */ lastCloseTime?: number; /** Success rate percentage */ successRate: number; } /** * Circuit breaker implementation * @class * @public */ export declare class CircuitBreaker { private state; private failures; private successes; private lastStateChange; private halfOpenAttempts; private totalRequests; private readonly config; private readonly name; /** * Creates a new circuit breaker instance * @param name The name of this circuit breaker (for logging) * @param config Optional configuration overrides */ constructor(name: string, config?: Partial<CircuitBreakerConfig>); /** * Check if a request should be allowed through * @returns True if the request can proceed * @public */ canRequest(): boolean; /** * Record a successful request * @public */ recordSuccess(): void; /** * Record a failed request * @public */ recordFailure(): void; /** * Get current circuit breaker statistics * @returns The current statistics * @public */ getStats(): CircuitBreakerStats; /** * Reset the circuit breaker to closed state * @public */ reset(): void; /** * Get the current state * @returns The current circuit state * @public */ getState(): CircuitState; /** * Transition to a new state * @param newState The new state to transition to * @private */ private transitionTo; /** * Clean up old entries outside the time window * @private */ private cleanupOldEntries; /** * Get the count of recent failures within the time window * @returns The number of recent failures * @private */ private getRecentFailureCount; /** * Get the count of recent successes within the time window * @returns The number of recent successes * @private */ private getRecentSuccessCount; } /** * Circuit breaker manager for managing multiple circuit breakers * @class * @public */ export declare class CircuitBreakerManager { private static instance; private breakers; /** * Get the singleton instance * @returns The circuit breaker manager instance * @public */ static getInstance(): CircuitBreakerManager; /** * Get or create a circuit breaker for an endpoint * @param endpoint The endpoint name * @param config Optional configuration overrides * @returns The circuit breaker instance * @public */ getBreaker(endpoint: string, config?: Partial<CircuitBreakerConfig>): CircuitBreaker; /** * Get statistics for all circuit breakers * @returns Map of endpoint to statistics * @public */ getAllStats(): Map<string, CircuitBreakerStats>; /** * Reset all circuit breakers * @public */ resetAll(): void; /** * Clear all circuit breakers (for testing) * @public */ clear(): void; }