circuit-breaker-sns29
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Circuit Breaker for Node.js

npm v1.0.0

Overview

The Circuit Breaker for Node.js library is a powerful and flexible implementation of the circuit breaker pattern for handling external service dependencies in Node.js applications. It helps you build more resilient and fault-tolerant applications by preventing cascading failures when a service experiences issues.

Features

  • Configurable Circuit Breaker: Easily configure circuit breaker settings such as failure thresholds, success thresholds, timeouts, and more to suit your application's needs.
  • Proxy Support: Seamlessly integrate with HTTP and HTTPS proxies, allowing you to route requests through a proxy server.
  • Logging: Integrated logging for tracking requests and circuit breaker state changes.
  • Customizable Callbacks: Define custom success and failure callback functions for fine-grained control.
  • Promises: Utilizes Promises to make it easy to work with asynchronous operations.

Installation

You can install the Circuit Breaker for Node.js library via npm:

npm install circuit-breaker-sns28

Usage

Importing the Library To use the library in your Node.js application, import it as follows:

const { CircuitBreaker, setProxyAgent } = require('circuit-breaker-sns28');

or

import { cbInit, cbSuccess } from './circuit-breaker-sns28';

Basic Example
Here's a basic example of how to use the circuit breaker:
// Initialize the Circuit Breaker with configuration
cbInit({
      requestUrl,
      jsonObjectData,
      headersObjectData,
      cbfailurethreshold,
      cbsuccessthreshold,
      cbtimeoutms,
    })

You can call 'cbInit' function with the target request's data; it will trigger the request, and while doing so, the circuit breaker instance will be activated. It will return a promise that you can handle as per your need.

// Execute a request using the circuit breaker
cbSuccess() and cbFailure() methods can be called in the callback of the request you are making; these will keep track of success and failure attempts against a particular URL.

// Configure a proxy agent if needed
setProxyAgent({ proxyUrl: 'https://proxy.example.com' });

This proxy agent function is used inside the got request to set the proxy; you can provide other parameters like:
{
	keepAlive: true,
	keepAliveMsecs: 1000,
	maxSockets: 256,
	maxFreeSockets: 256,
	scheduling: 'lifo',
	proxy: proxyUrl,
}

Expected Behaviour:

Circuit should suspend once the failure threshold is crossed.
After a successful attempt against the failed URL, the failure count will be reset to 0 for that particular URL.
Once the cooldown time is over and the same request fails again, the failure count is again set to the threshold, and the circuit is suspended after one more failed attempt.

Contributing
We welcome contributions from the community! If you'd like to contribute to the Circuit Breaker for Node.js library, please follow our contribution guidelines.

License
This library is licensed under the ISC License.

Contact
If you have any questions, issues, or suggestions, please feel free to open an issue on GitHub or reach out to the maintainers directly.

Thank you for using the Circuit Breaker for Node.js library! We hope it helps you build more robust and resilient applications.

Readme

Keywords

none

Package Sidebar

Install

npm i circuit-breaker-sns29

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

39.8 kB

Total Files

15

Last publish

Collaborators

  • sandeep_chekka