Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/visible/cruel/llms.txt

Use this file to discover all available pages before exploring further.

Function Signature

function createCircuitBreaker<T extends AnyFn>(
  fn: T,
  options: CircuitBreakerOptions
): T & { getState: () => CircuitBreakerState; reset: () => void }
Wraps a function with circuit breaker pattern. The circuit breaker monitors failures and opens the circuit after reaching a threshold, preventing further calls until the timeout expires.

Options

threshold
number
required
Number of consecutive failures before opening the circuit. Must be at least 1.
timeout
number
required
Time in milliseconds to wait before attempting to close the circuit. Must be at least 1.
onOpen
() => void
Callback invoked when the circuit opens.
onClose
() => void
Callback invoked when the circuit closes.
onHalfOpen
() => void
Callback invoked when the circuit enters half-open state.

Return Type

Returns the wrapped function with additional methods:
  • getState(): CircuitBreakerState - Returns current state of the circuit breaker
  • reset(): void - Resets the circuit breaker to closed state

Types

interface CircuitBreakerState {
  failures: number
  state: "closed" | "open" | "half-open"
  lastFailure: number
}

Example

import { createCircuitBreaker } from 'cruel'

const protectedFn = createCircuitBreaker(
  async () => {
    const response = await fetch('https://api.example.com/data')
    return response.json()
  },
  {
    threshold: 5,
    timeout: 30000,
    onOpen: () => console.log('Circuit opened'),
    onClose: () => console.log('Circuit closed'),
    onHalfOpen: () => console.log('Circuit half-open')
  }
)

// Check circuit state
const state = protectedFn.getState()
console.log(state.state) // "closed" | "open" | "half-open"

// Reset circuit breaker
protectedFn.reset()

Build docs developers (and LLMs) love