Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
retry | 0.10.1 | 9.13 kB | MIT | prod |
The retry
package provides an abstraction for implementing exponential and custom retry strategies for failed operations in your Node.js programs. This npm package allows developers to automatically retry failing operations a specific number of times, with the frequency of retries following either a default exponential backoff strategy or a manually configured custom strategy.
The retry
package can be utilized in a Node.js program after installation via npm using the command npm install retry
. Below is an example code snippet on how to use this package:
var dns = require('dns');
var retry = require('retry');
function faultTolerantResolve(address, cb) {
var operation = retry.operation();
operation.attempt(function(currentAttempt) {
dns.resolve(address, function(err, addresses) {
if (operation.retry(err)) {
return;
}
cb(err ? operation.mainError() : null, addresses);
});
});
}
faultTolerantResolve('nodejs.org', function(err, addresses) {
console.log(err, addresses);
});
The above code tries to resolve a given DNS address using the dns.resolve
function with a fault-tolerant approach. If the resolve operation fails, operation.retry(err)
is called to retry the operation according to the retry strategy defined by the operation
object. In this example, we use the default retry strategy, which retried failed operations 10 times with an exponential backoff. The retry behavior can be customized using an options object as shown below:
var operation = retry.operation({
retries: 5,
factor: 3,
minTimeout: 1 * 1000,
maxTimeout: 60 * 1000,
randomize: true,
});
The documentation for the retry
package is available in the README file of the project's GitHub repository. It provides a detailed overview of the API available as part of the package along with examples and explanations for all options used for retry strategies.
The link to the repository is https://github.com/tim-kos/node-retry where the README file is located at the root of the repository. The API documentation section includes explanations of all the methods such as retry.operation
, retry.timeouts
, retry.createTimeout
, retry.wrap
, and more. Additionally, specific use-cases and strategies related to retries, timeout calculations, and the exponential backoff approach are also documented in detail.