import { CircuitState, CircuitOpenError } from '../utils/resilience'; import { ModuleId } from './types'; /** * Module circuit breaker configuration */ export interface ModuleCircuitBreakerConfig { /** Failure threshold before opening circuit */ failureThreshold?: number; /** Success threshold for half-open to close */ successThreshold?: number; /** Time to wait before trying half-open (ms) */ resetTimeout?: number; /** Time window for failure counting (ms) */ failureWindow?: number; /** Callback on state change */ onStateChange?: (from: CircuitState, to: CircuitState) => void; /** Callback when circuit opens */ onCircuitOpen?: () => void; /** Callback when circuit recovers */ onCircuitRecovery?: () => void; } /** * Module loading metrics */ export interface ModuleLoadingMetrics { totalAttempts: number; totalSuccesses: number; totalFailures: number; circuitOpenCount: number; lastFailureTime: Date | null; lastSuccessTime: Date | null; circuitState: CircuitState; failedModules: Set; } /** * Module Circuit Breaker * * Provides circuit breaker protection for dynamic module loading. * When module loading fails repeatedly (e.g., CDN is down), the circuit * opens and immediately fails subsequent requests, preventing: * - Thundering herd on module servers * - UI hangs waiting for modules to load * - Cascading failures across the application * * @example * ```typescript * const breaker = getModuleCircuitBreaker(); * * // Execute module load through circuit breaker * const component = await breaker.execute(() => import('./MyModule')); * * // Check if we can attempt to load modules * if (breaker.canLoadModules()) { * await loadModule(moduleId); * } else { * showOfflineFallback(); * } * ``` */ export declare class ModuleCircuitBreaker { private breaker; private config; private totalAttempts; private totalSuccesses; private totalFailures; private circuitOpenCount; private lastFailureTime; private lastSuccessTime; private failedModules; constructor(config?: ModuleCircuitBreakerConfig); /** * Get current circuit state */ getState(): CircuitState; /** * Check if module loading is allowed */ canLoadModules(): boolean; /** * Execute a module load through the circuit breaker * * @param loader - The module loading function * @param moduleId - Optional module ID for tracking * @returns The loaded module * @throws CircuitOpenError if circuit is open */ execute(loader: () => Promise, moduleId?: ModuleId): Promise; /** * Execute module load with fallback * * @param loader - The module loading function * @param fallback - Fallback value if circuit is open or load fails * @param moduleId - Optional module ID for tracking * @returns The loaded module or fallback */ executeWithFallback(loader: () => Promise, fallback: T, moduleId?: ModuleId): Promise; /** * Get loading metrics */ getMetrics(): ModuleLoadingMetrics; /** * Force reset the circuit */ reset(): void; /** * Handle circuit state changes */ private handleStateChange; /** * Record a successful module load */ private recordSuccess; /** * Record a failed module load */ private recordFailure; } /** * Error thrown when module circuit is open */ export declare class ModuleCircuitOpenError extends CircuitOpenError { readonly moduleId?: ModuleId; constructor(message: string, moduleId?: ModuleId); } /** * Get or create the global module circuit breaker */ export declare function getModuleCircuitBreaker(config?: ModuleCircuitBreakerConfig): ModuleCircuitBreaker; /** * Reset the global module circuit breaker */ export declare function resetModuleCircuitBreaker(): void; /** * Replace the global module circuit breaker (for testing) */ export declare function setModuleCircuitBreaker(breaker: ModuleCircuitBreaker | null): void; /** * Wrap a dynamic import with circuit breaker protection * * @example * ```typescript * const MyComponent = await withModuleCircuitBreaker( * () => import('./MyComponent'), * 'MyComponent' * ); * ``` */ export declare function withModuleCircuitBreaker(loader: () => Promise, moduleId?: ModuleId): Promise; /** * Wrap a dynamic import with circuit breaker and fallback * * @example * ```typescript * const MyComponent = await withModuleCircuitBreakerFallback( * () => import('./MyComponent'), * FallbackComponent, * 'MyComponent' * ); * ``` */ export declare function withModuleCircuitBreakerFallback(loader: () => Promise, fallback: T, moduleId?: ModuleId): Promise; /** * Check if module loading is currently available */ export declare function canLoadModules(): boolean; /** * Get module loading circuit state */ export declare function getModuleCircuitState(): CircuitState;