Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@fortify-ts/circuit-breaker

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fortify-ts/circuit-breaker

Circuit breaker pattern for Fortify TS resilience library

latest
Source
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

@fortify-ts/circuit-breaker

Circuit breaker pattern implementation for the Fortify-TS resilience library.

Installation

npm install @fortify-ts/circuit-breaker
# or
pnpm add @fortify-ts/circuit-breaker

Features

  • State Machine: CLOSED, OPEN, and HALF-OPEN states
  • Configurable Thresholds: Max failures, timeout, half-open requests
  • Custom Predicates: readyToTrip and isSuccessful callbacks
  • State Change Notifications: onStateChange callback
  • Automatic Recovery: Transitions from OPEN to HALF-OPEN after timeout

Usage

Basic Usage

import { CircuitBreaker } from '@fortify-ts/circuit-breaker';

const breaker = new CircuitBreaker<Response>({
  maxFailures: 5,
  timeout: 60000, // 60 seconds
});

try {
  const result = await breaker.execute(async (signal) => {
    return fetch('/api/data', { signal });
  });
} catch (error) {
  if (error instanceof CircuitOpenError) {
    console.log('Circuit is open, try again later');
  }
}

Configuration Options

const breaker = new CircuitBreaker<Response>({
  // Maximum failures before opening circuit
  maxFailures: 5,

  // Time in ms before attempting recovery
  timeout: 60000,

  // Requests allowed in half-open state
  halfOpenMaxRequests: 1,

  // Reset counts interval (0 = disabled)
  interval: 0,

  // Custom trip condition
  readyToTrip: (counts) => counts.consecutiveFailures >= 3,

  // Custom success condition
  isSuccessful: (result) => result.ok,

  // State change notification
  onStateChange: (from, to) => {
    console.log(`Circuit state: ${from} -> ${to}`);
  },

  // Optional logger
  logger: myLogger,
});

State Machine

     ┌─────────────────────────────────────────────────┐
     │                                                 │
     ▼                                                 │
  CLOSED ──── failures >= maxFailures ────► OPEN ────►│
     ▲                                        │        │
     │                                        │ timeout
     │                                        ▼        │
     └────── success ◄──────────────────── HALF-OPEN ─┘
                                               │
                                               │ failure
                                               ▼
                                             OPEN

Checking State

// Get current state
const state = breaker.getState(); // 'closed' | 'open' | 'half-open'

// Get request counts
const counts = breaker.getCounts();
console.log(counts.requests, counts.totalSuccesses, counts.totalFailures);

// Reset circuit breaker
breaker.reset();

// Clean up resources
await breaker.close();

Configuration Reference

OptionTypeDefaultDescription
maxFailuresnumber5Failures before opening
timeoutnumber60000Recovery timeout (ms)
halfOpenMaxRequestsnumber1Requests in half-open
intervalnumber0Count reset interval
readyToTripfunction-Custom trip condition
isSuccessfulfunction-Custom success check
onStateChangefunction-State change callback
loggerFortifyLogger-Optional logger

License

MIT

Keywords

resilience

FAQs

Package last updated on 23 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts