Reference
BreakerHandle
API reference for the BreakerHandle class
A BreakerHandle is returned by client.breaker(slug). It delegates all operations to the parent client.
const stripe = client.breaker('stripe-api')Properties
slug
readonly slug: stringThe breaker slug this handle is bound to.
Methods
protect()
protect<T>(
fn: (signal: AbortSignal) => Promise<T> | T,
options?: TProtectOptions<T>,
): Promise<T>Executes fn with circuit breaker protection. Checks breaker state, records metrics, and supports timeouts and fallbacks.
Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (signal: AbortSignal) => Promise<T> | T | The function to protect. Receives an AbortSignal for cooperative cancellation. |
options | TProtectOptions<T> | Timeout, fallback, and signal configuration. |
TProtectOptions
type TProtectOptions<T> = {
timeout?: number
fallback?: () => Promise<T> | T
signal?: AbortSignal
}| Option | Type | Description |
|---|---|---|
timeout | number | Timeout in milliseconds. Throws TimeoutError if exceeded. |
fallback | () => Promise<T> | T | Called when the breaker is open instead of fn. Not wrapped with timeout or signal. |
signal | AbortSignal | External cancellation signal. Throws AbortOperationError when aborted. |
Behavior
- Breaker closed/half-open:
fnexecutes. Metrics are recorded. - Breaker open + fallback: Fallback executes. No metrics recorded.
- Breaker open + no fallback:
fnexecutes with a warning logged (fail-open). Metrics are recorded. - State unknown:
fnexecutes (fail-open). Metrics are recorded.
Throws
TimeoutErroriffnexceedstimeout.AbortOperationErrorifsignalis aborted.- Any error thrown by
fnis rethrown.
const charge = await stripe.protect(
async (signal) => {
return await paymentApi.charge({ amount: 1000 }, { signal })
},
{
timeout: 5000,
fallback: () => ({ status: 'queued' }),
},
)isOpen()
isOpen(): Promise<boolean>Returns true if the breaker is open. Returns false if closed, half-open, unknown, or on any error. Never throws. Has a 500ms budget.
if (await stripe.isOpen()) {
return cachedResponse
}isClosed()
isClosed(): Promise<boolean>Returns true if the breaker is closed. Returns true on any error (fail-open). Never throws. Has a 500ms budget.
if (await stripe.isClosed()) {
// Safe to proceed
}status()
status(signal?: AbortSignal): Promise<TBreaker | null>Returns the full breaker object or null if not found or unreachable. Never throws. Has a 500ms budget.
| Parameter | Type | Description |
|---|---|---|
signal | AbortSignal | Optional cancellation signal. |
const breaker = await stripe.status()
if (breaker) {
console.log(breaker.state)
console.log(breaker.updatedAt)
console.log(breaker.retryAfter)
}