failover-sdk
Version:
One-line API failover with zero downtime. Native Rust performance with TypeScript interface.
112 lines • 3.64 kB
TypeScript
export declare enum CircuitState {
Closed = "Closed",// Normal operation
Open = "Open",// Circuit is open, rejecting calls
HalfOpen = "HalfOpen"
}
export interface CircuitBreakerConfig {
failure_threshold: number;
success_threshold: number;
timeout_duration_ms: number;
request_timeout_ms: number;
min_requests: number;
failure_rate_threshold: number;
}
export interface CircuitBreakerStats {
state: CircuitState;
failure_count: number;
success_count: number;
total_requests: number;
consecutive_failures: number;
consecutive_successes: number;
last_failure_time?: number;
last_success_time?: number;
state_changed_at: number;
average_response_time_ms: number;
}
export interface RequestResult {
success: boolean;
timestamp: number;
response_time_ms: number;
error_type?: string;
}
declare class CircuitBreakerInstance {
private config;
private stats;
private recent_requests;
private readonly MAX_RECENT_REQUESTS;
constructor(config?: Partial<CircuitBreakerConfig>);
execute<T>(operation: () => Promise<T>): Promise<T>;
private canExecute;
private onSuccess;
private onFailure;
private shouldOpenCircuit;
private transitionTo;
private addRequestResult;
private updateAverageResponseTime;
getStats(): CircuitBreakerStats;
reset(): void;
trip(): void;
getConfig(): CircuitBreakerConfig;
}
export declare class CircuitBreakerError extends Error {
constructor(message: string);
}
export declare class CircuitBreakerManager {
/**
* Get or create a circuit breaker for a provider
*/
static getCircuitBreaker(provider: string, config?: Partial<CircuitBreakerConfig>): CircuitBreakerInstance;
/**
* Execute an operation with circuit breaker protection
*/
static execute<T>(provider: string, operation: () => Promise<T>, config?: Partial<CircuitBreakerConfig>): Promise<T>;
/**
* Get circuit breaker statistics for a specific provider
*/
static getStats(provider: string): CircuitBreakerStats | null;
/**
* Get circuit breaker statistics for all providers
*/
static getAllStats(): Record<string, CircuitBreakerStats>;
/**
* Get health score for a specific provider (0.0 = unhealthy, 1.0 = healthy)
*/
static getHealthScore(provider: string): number;
/**
* Get health scores for multiple providers
*/
static getHealthScores(providers: string[]): Record<string, number>;
/**
* Reset circuit breaker for a specific provider (back to closed state)
*/
static reset(provider: string): void;
/**
* Manually trip the circuit breaker for a specific provider (force to open state)
*/
static trip(provider: string): void;
/**
* Check if a provider's circuit breaker is healthy (closed state)
*/
static isHealthy(provider: string): boolean;
/**
* Get a summary of all circuit breakers
*/
static getSummary(): Record<string, {
state: CircuitState;
health_score: number;
total_requests: number;
}>;
/**
* Monitor circuit breaker events (returns a function to unsubscribe)
*/
static monitor(callback: (provider: string, event: string, data: unknown) => void): () => void;
/**
* Get recommendations for circuit breaker configuration
*/
static getRecommendations(provider: string): {
current_config: CircuitBreakerConfig;
recommendations: string[];
};
}
export { CircuitBreakerInstance };
//# sourceMappingURL=circuit-breaker.d.ts.map