recede v0.0.0
Recede
Receive, or recede (using configurable strategies for exponential backoff).
Installation
$ npm install recede
Options
minTimeout - minimum timeout millis [100]
maxTimeout - maximum timeout millis [300000]
maxAttempts - maximum no of consecutive failed attempts [3]
multiplier - multiplication factor for computing next timeout [2]
fractions - whether to include fractional values in timeout [false]
jitter - randomization factor [0]
stopOnReceive - whether to stop on first receive [true]
Examples
Require and initialize:
var Recede = require('recede');
var recede = new Recede(); // using defaults
Basic retry
You need to receive a response from an api
which errors often. The api
is currently invoked as:
api(params, function(err, res) {
if(err) console.log(err);
else console.log(res);
});
To employ a retry and backoff strategy you can invoke the api
using recede
:
recede.retry(api, params, function(err, res) {
// this part executes after each attempt with err or
// res (whichever applicable)
// If api returns multiple values, res becomes an array
// containing the values as elements
});
Note:
1. api
is a node-styled function (accepting a callback function as it's last argument).
2. api
invokes its callback with error (if present) as its first argument, and values as other arguments.
3. By default recede.retry
stops when a valid response is received or number of continued failed attempts reaches maxAttempts
.
4. Each failed response results in exponentially increasing backoff timeout for next attempt.
Continued retry
If you wish to execute an api/function forever using the backoff strategy above (probably the case when polling a queue), set maxAttempts
to a negative value and stopOnReceive
to false
:
var recede = new Recede({maxAttempts: -1, stopOnReceive: false});
Forcing stop
To force stop a running retry
operation use:
recede.stop();
Using only the timeouts
You can also use recede
to only calculate backoff timeouts and employ your own retry strategy. In this case you need to use the two functions below:
recede.timeout(); // Get next backoff timeout
recede.reset(); // Set backoff timeout to minTimeout
License
The MIT License (MIT)
Copyright (c) 2015 Nishant Shreshth
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.