
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
nodejs-circuit-breaker
Advanced tools
A robust Node.js Circuit Breaker implementation that monitors API health and handles failures gracefully by temporarily blocking failing URLs and retrying them after a certain timeout. Supports multiple HTTP methods and customizable failure thresholds.
A flexible Circuit Breaker implementation for handling network requests. This package helps manage unreliable services, preventing requests to unhealthy services, and automatically retrying when the service is healthy again. It supports HTTP methods, detailed error handling, and tracks the health of services across requests.
Multiple HTTP Methods: Supports different HTTP methods like GET, POST, PUT, DELETE.
Circuit Breaker States: Manages the CLOSED, OPEN, and HALF-OPEN states autonomously.
Error Logging: Stores the last error data for better debugging.
Timeout and Retry Mechanism: Implements custom timeouts with exponential backoff.
Threshold Configuration: Customizable failure and success thresholds.
Persistent State: Tracks URL states and saves failure/success information to a JSON file.
Install the pacakage with npm
npm install my-circuit-breaker
Here’s a simple example of how to use the node-circuit-breaker module to protect API calls:
const initiateBreaker = require('my-circuit-breaker');
const response = await initiateBreaker('https://api.example.com/data', {
method: 'GET', // HTTP method (default: GET)
failureThreshold: 3, // Number of failures before opening the circuit
successThreshold: 5, // Number of successes before closing the circuit
timeout: 60000 // Time in ms before trying again after an open circuit
});
console.log(response.message);
In this example:
The initiateBreaker function accepts two arguments:
const initiateBreaker = require('my-circuit-breaker');
const response = await initiateBreaker('https://api.example.com/data', {
method: 'POST',
failureThreshold: 4,
successThreshold: 3,
timeout: 30000, // 30 seconds
});
if (response.error) {
console.log(`Request failed: ${response.message}`, response.error);
} else {
console.log(`Request succeeded: ${response.message}`, response.data);
}
The initiateBreaker function returns an object with the following structure:
{
"error": Object, // Contains error data from the last failed request (if any)
"data": Object, // Contains data from the successful request (if successful)
"message": String // Describes the result of the request and circuit state
}
If a request fails, the last error data is stored and can be accessed in subsequent requests to the same URL.
const response = await initiateBreaker('https://api.example.com/data');
if (response.error) {
console.log('Last error data:', response.error);
}
If a request is successful, the request response is accessed in the data object
const response = await initiateBreaker('https://api.example.com/data');
if (response.data) {
console.log('Sucessfull data:', response.data);
}
Feel free to open issues or contribute to this project by submitting pull requests.
This project was build with ❤ by Abdulwahab Abdulwarith.
FAQs
A robust Node.js Circuit Breaker implementation that monitors API health and handles failures gracefully by temporarily blocking failing URLs and retrying them after a certain timeout. Supports multiple HTTP methods and customizable failure thresholds.
We found that nodejs-circuit-breaker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

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.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.