
Security News
Potemkin Understanding in LLMs: New Study Reveals Flaws in AI Benchmarks
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
The 'backoff' npm package provides a mechanism to handle retry logic with exponential backoff and other backoff strategies. It is useful for scenarios where you need to retry operations that may fail intermittently, such as network requests or database queries.
Exponential Backoff
This feature allows you to implement an exponential backoff strategy using the Fibonacci sequence. The code sample demonstrates how to set up a Fibonacci backoff with a randomisation factor, initial delay, and maximum delay. It also shows how to handle the 'ready' and 'fail' events.
const backoff = require('backoff');
const fibonacciBackoff = backoff.fibonacci({ randomisationFactor: 0.2, initialDelay: 10, maxDelay: 1000 });
fibonacciBackoff.on('ready', function(number, delay) {
console.log(`Retrying after ${delay} ms`);
// Perform the operation that needs to be retried
});
fibonacciBackoff.on('fail', function() {
console.log('Operation failed after maximum retries');
});
// Start the backoff process
fibonacciBackoff.backoff();
Retry with Custom Strategy
This feature allows you to implement a custom retry strategy using exponential backoff. The code sample demonstrates how to set up an exponential backoff with an initial delay and maximum delay. It also shows how to handle the 'ready' and 'fail' events.
const backoff = require('backoff');
const customStrategy = backoff.exponential({ initialDelay: 100, maxDelay: 10000 });
customStrategy.on('ready', function(number, delay) {
console.log(`Retry attempt #${number} after ${delay} ms`);
// Perform the operation that needs to be retried
});
customStrategy.on('fail', function() {
console.log('Operation failed after maximum retries');
});
// Start the backoff process
customStrategy.backoff();
Handling Errors
This feature demonstrates how to handle errors during the retry process. The code sample shows how to simulate an operation that may fail and how to retry the operation using exponential backoff until it succeeds or reaches the maximum number of retries.
const backoff = require('backoff');
const exponentialBackoff = backoff.exponential({ initialDelay: 100, maxDelay: 10000 });
exponentialBackoff.on('ready', function(number, delay) {
console.log(`Retry attempt #${number} after ${delay} ms`);
// Simulate an operation that may fail
const success = Math.random() > 0.5;
if (!success) {
exponentialBackoff.backoff();
} else {
console.log('Operation succeeded');
}
});
exponentialBackoff.on('fail', function() {
console.log('Operation failed after maximum retries');
});
// Start the backoff process
exponentialBackoff.backoff();
The 'retry' package provides a similar functionality to 'backoff' by allowing you to retry operations with configurable strategies. It supports exponential backoff, custom retry strategies, and error handling. Compared to 'backoff', 'retry' offers a more flexible API for defining custom retry logic.
The 'promise-retry' package is designed for retrying operations that return promises. It provides a simple API for retrying promise-based operations with configurable retry strategies, including exponential backoff. Compared to 'backoff', 'promise-retry' is more focused on promise-based workflows.
The 'async-retry' package is another alternative that supports retrying asynchronous operations with configurable strategies. It works well with both promises and async/await syntax. Compared to 'backoff', 'async-retry' offers a more modern API that integrates seamlessly with async/await.
Fibonacci and exponential backoffs for Node.js.
npm install backoff
In order to use backoff, require backoff
.
var backoff = require('backoff');
The usual way to instantiate a new Backoff
object is to use one predefined
factory method: backoff.fibonacci([options])
, backoff.exponential([options])
.
Backoff
inherits from EventEmitter
. When a backoff starts, a backoff
event is emitted and, when a backoff ends, a ready
event is emitted.
Handlers for these two events are called with the current backoff number and
delay.
var fibonacciBackoff = backoff.fibonacci({
randomisationFactor: 0,
initialDelay: 10,
maxDelay: 1000
});
fibonacciBackoff.on('backoff', function(number, delay) {
// Do something when backoff starts.
console.log(number + ' ' + delay + 'ms');
});
fibonacciBackoff.on('ready', function(number, delay) {
// Do something when backoff ends.
if (number < 15) {
fibonacciBackoff.backoff();
}
});
fibonacciBackoff.backoff();
The previous example would print:
0 10ms
1 10ms
2 20ms
3 30ms
4 50ms
5 80ms
6 130ms
7 210ms
8 340ms
9 550ms
10 890ms
11 1000ms
12 1000ms
13 1000ms
14 1000ms
15 1000ms
Backoff objects are meant to be instantiated once and reused several times
by calling reset
after a successful "retry".
Constructs a Fibonacci backoff (10, 10, 20, 30, 50, etc.).
See bellow for the options description.
Constructs an exponential backoff (10, 20, 40, 80, etc.).
The options are:
With these values, the backoff delay will increase from 100 ms to 10000 ms. The randomisation factor controls the range of randomness and must be between 0 and 1. By default, no randomisation is applied on the backoff delay.
Constructs a new backoff object from a specific backoff strategy. The backoff
strategy must implement the BackoffStrategy
interface defined bellow.
Starts a backoff operation. Will throw an error if a backoff operation is already in progress.
In practice, this method should be called after a failed attempt to perform a sensitive operation (connecting to a database, downloading a resource over the network, etc.).
Resets the backoff delay to the initial backoff delay and stop any backoff operation in progress. After reset, a backoff instance can and should be reused.
In practice, this method should be called after having successfully completed the sensitive operation guarded by the backoff instance or if the client code request to stop any reconnection attempt.
Emitted when a backoff operation is started. Signals to the client how long the next backoff delay will be.
Emitted when a backoff operation is done. Signals that the failing operation should be retried.
A backoff strategy must provide the following methods.
Computes and returns the next backoff delay.
Resets the backoff delay to its initial value.
Exponential (10, 20, 40, 80, etc.) backoff strategy implementation.
The options are:
Fibonnaci (10, 10, 20, 30, 50, etc.) backoff strategy implementation.
The options are:
This code is free to use under the terms of the MIT license.
1.0.0
start
and done
events backoff
and ready
.backoff.fibonnaci
.FAQs
Fibonacci and exponential backoffs.
The npm package backoff receives a total of 900,795 weekly downloads. As such, backoff popularity was classified as popular.
We found that backoff demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.