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

@hazeljs/resilience

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hazeljs/resilience

Fault-tolerance and resilience patterns for HazelJS - Circuit Breaker, Retry, Bulkhead, Timeout, Rate Limiter

latest
Source
npmnpm
Version
0.8.7
Version published
Maintainers
1
Created
Source

@hazeljs/resilience

Fault-tolerance and resilience patterns for HazelJS. Provides circuit breaker, retry, timeout, bulkhead, rate limiter, and metrics collection — all usable via decorators or programmatic API.

npm version npm downloads License: Apache-2.0

Installation

npm install @hazeljs/resilience

Features

  • Circuit Breaker — Prevents cascading failures with sliding window metrics, configurable failure predicates, fallback support, and event-driven state transitions
  • Retry — Configurable retry with exponential/linear/fixed backoff, jitter, and retryable-error predicates
  • Timeout — Promise-based timeout wrapper with cancellation
  • Bulkhead — Concurrency limiter with queue support to isolate failures
  • Rate Limiter — Token bucket and sliding window strategies
  • Metrics — Tracks success/failure/latency per target, feeds into gateway canary decisions

Quick Start

Decorator API

import { Injectable } from '@hazeljs/core';
import {
  WithCircuitBreaker,
  WithRetry,
  WithTimeout,
  WithBulkhead,
  Fallback,
} from '@hazeljs/resilience';

@Injectable()
class PaymentService {
  @WithCircuitBreaker({
    failureThreshold: 5,
    slidingWindow: { type: 'count', size: 20 },
    resetTimeout: 30_000,
    fallback: 'processPaymentFallback',
  })
  @WithRetry({ maxAttempts: 3, backoff: 'exponential', baseDelay: 500 })
  @WithTimeout(5000)
  @WithBulkhead({ maxConcurrent: 10, maxQueue: 50 })
  async processPayment(order: Order): Promise<PaymentResult> {
    return await this.paymentGateway.charge(order);
  }

  @Fallback('processPayment')
  async processPaymentFallback(order: Order): Promise<PaymentResult> {
    return { status: 'queued', message: 'Payment will be processed later' };
  }
}

Programmatic API

import { CircuitBreaker, RetryPolicy, Timeout, Bulkhead } from '@hazeljs/resilience';

// Circuit Breaker
const breaker = new CircuitBreaker({ failureThreshold: 5 });
const result = await breaker.execute(() => fetch('/api/data'));

breaker.on('stateChange', (from, to) => console.log(`${from} -> ${to}`));
breaker.getMetrics(); // { totalRequests, failureRate, p99Latency, ... }

// Retry
const retry = new RetryPolicy({ maxAttempts: 3, backoff: 'exponential', baseDelay: 1000 });
const data = await retry.execute(() => fetch('/api/unstable'));

// Timeout
const timeout = new Timeout(5000);
const response = await timeout.execute(() => fetch('/api/slow'));

// Bulkhead
const bulkhead = new Bulkhead({ maxConcurrent: 10, maxQueue: 50 });
const result = await bulkhead.execute(() => intensiveOperation());

Circuit Breaker States

CLOSED  ──(failures >= threshold)──>  OPEN
  ^                                     |
  |                                     | (reset timeout)
  |                                     v
  └──(successes >= threshold)──  HALF_OPEN

API Reference

See the full documentation for complete API reference.

License

Apache 2.0

Keywords

hazeljs

FAQs

Package last updated on 12 May 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