kinetex - v1.4.0
    Preparing search index...

    Class CircuitBreaker

    Per-key circuit breaker implementing the CLOSED → OPEN → HALF_OPEN state machine with sliding-window or consecutive-count failure detection.

    Thread-safe for async use. Rejects with CircuitOpenError when OPEN.

    const cb = new CircuitBreaker("api.example.com", { failureThreshold: 3 });
    const result = await cb.execute(() => fetch("https://api.example.com/data"));
    Index
    • get state(): CircuitState

      The current circuit state, as of right now.

      The OPEN → HALF_OPEN transition is driven by elapsed time, but it was only ever evaluated inside execute(). A caller reading state — a health endpoint, a dashboard, the onRejected snapshot — therefore saw a stale OPEN for as long as no traffic arrived, and could not tell a circuit that was about to probe from one that had been down for an hour. The elapsed check is applied on read as well. It is a pure read: the onHalfOpen callback still fires from execute(), so observing the state never mutates the breaker.

      Returns CircuitState

    • Execute a function through the circuit breaker. Throws CircuitOpenError if the circuit is open. Records success/failure and transitions state accordingly.

      Type Parameters

      • T

      Parameters

      • fn: () => Promise<T>

      Returns Promise<T>

    • Manually trip the circuit open — useful for maintenance windows.

      Returns void

    • Manually reset the circuit to CLOSED — useful after a fix deployment.

      Returns void