
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
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.
The npm package nodejs-circuit-breaker receives a total of 1 weekly downloads. As such, nodejs-circuit-breaker popularity was classified as not popular.
We found that nodejs-circuit-breaker demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.