CircuitBreakerOptions
import { CircuitBreakerOptions } from "jsr:@ende/async/unstable-circuit-breaker";interface ยท experimental
interface CircuitBreakerOptions<T>Options for CircuitBreaker.
Properties
failureRateThreshold
failureRateThreshold?: numberFailure rate threshold as a ratio between 0 (exclusive) and 1 (inclusive).
The circuit opens when the failure rate in the sliding window meets or
exceeds this value and at least minimumThroughput requests
have been recorded.
Default: 0.5
minimumThroughput
minimumThroughput?: numberMinimum number of requests in the sliding window before the failure rate is evaluated. Prevents the circuit from tripping on statistically insignificant samples during low traffic.
Default: 20
windowMs
windowMs?: numberDuration of the sliding window in milliseconds.
Default: 60000
segmentsPerWindow
segmentsPerWindow?: numberNumber of segments the sliding window is divided into. More segments give finer-grained decay at the cost of slightly more memory.
Default: 10
cooldownMs
cooldownMs?: numberDuration in milliseconds the circuit stays open before entering half-open.
Default: 30000
successThreshold
successThreshold?: numberNumber of consecutive successes needed to close from half-open state.
Default: 2
halfOpenMaxConcurrent
halfOpenMaxConcurrent?: numberMaximum concurrent requests allowed in half-open state.
Default: 3
isFailure
isFailure?: (error: unknown) => booleanCustom predicate to determine if an error should count as a circuit failure. By default, any thrown error is a failure.
Returns
true if the error should count as a circuit failure.
isResultFailure
isResultFailure?: (result: T) => booleanCustom predicate to determine if a successful result should count as failure. Useful for HTTP responses where status codes indicate logical failures.
Returns
true if the result should count as a circuit failure.
onStateChange
onStateChange?: (from: CircuitState, to: CircuitState) => voidCallback invoked when circuit state changes.
If this callback throws, the error is reported asynchronously without disrupting circuit breaker operation.
onFailure
onFailure?: (error: unknown, failureCount: number, totalRequests: number) => voidCallback invoked when a failure is recorded.
If this callback throws, the error is reported asynchronously without disrupting circuit breaker operation.
onOpen
onOpen?: (failureCount: number, totalRequests: number) => voidCallback invoked when circuit opens.
If this callback throws, the error is reported asynchronously without disrupting circuit breaker operation.
onHalfOpen
onHalfOpen?: () => voidCallback invoked when circuit enters half-open state (testing recovery).
If this callback throws, the error is reported asynchronously without disrupting circuit breaker operation.
onClose
onClose?: () => voidCallback invoked when circuit closes (recovery complete).
If this callback throws, the error is reported asynchronously without disrupting circuit breaker operation.