class CircuitBreaker

Circuit Breaker implementation for fault tolerance

Tracks failures and prevents requests to failing resources by:

  1. Counting consecutive failures
  2. Opening the circuit after threshold is reached
  3. Blocking requests while circuit is open
  4. Attempting recovery after timeout period
  5. Closing circuit on successful recovery

Constructors

new
CircuitBreaker(options?: CircuitBreakerOptions)

Creates a new circuit breaker

Properties

private
failureCount: number
private
optional
lastFailureTime: Date
private
readonly
logger: ILogger
private
readonly
name: string
private
readonly
threshold: number
private
readonly
timeout: number

Methods

execute<T>(fn: () => Promise<T>): Promise<T>

Executes a function with circuit breaker protection

Gets the current failure count

getLastFailureTime(): Date | undefined

Gets the last failure time

Gets the current state of the circuit breaker

getStats(): CircuitBreakerStats

Gets circuit breaker statistics

private
getTimeUntilRecovery(): number

Gets time remaining until recovery attempt

private
onFailure(): void

Handles failed execution

private
onSuccess(): void

Handles successful execution

reset(): void

Manually resets the circuit breaker to CLOSED state Useful for testing or manual recovery

private
shouldAttemptRecovery(): boolean

Checks if enough time has passed to attempt recovery