Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backoff

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backoff - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

examples/fail.js

29

lib/backoff.js

@@ -18,2 +18,3 @@ /*

this.backoffStrategy_ = backoffStrategy;
this.maxNumberOfRetry_ = -1;
this.backoffNumber_ = 0;

@@ -30,2 +31,16 @@ this.backoffDelay_ = 0;

/**
* Sets a limit, greater than 0, on the maximum number of backoffs. A 'fail'
* event will be emitted when the limit is reached.
* @param maxNumberOfRetry The maximum number of backoffs.
*/
Backoff.prototype.failAfter = function(maxNumberOfRetry) {
if (maxNumberOfRetry < 1) {
throw new Error('Maximum number of retry must be greater than 0. ' +
'Actual: ' + maxNumberOfRetry);
}
this.maxNumberOfRetry_ = maxNumberOfRetry;
};
/**
* Starts a backoff operation.

@@ -38,5 +53,10 @@ */

this.backoffDelay_ = this.backoffStrategy_.next();
this.timeoutID_ = setTimeout(this.handlers.backoff, this.backoffDelay_);
this.emit('backoff', this.backoffNumber_, this.backoffDelay_);
if (this.backoffNumber_ === this.maxNumberOfRetry_) {
this.emit('fail');
this.reset();
} else {
this.backoffDelay_ = this.backoffStrategy_.next();
this.timeoutID_ = setTimeout(this.handlers.backoff, this.backoffDelay_);
this.emit('backoff', this.backoffNumber_, this.backoffDelay_);
}
};

@@ -50,3 +70,4 @@

this.timeoutID_ = -1;
this.emit('ready', this.backoffNumber_++, this.backoffDelay_);
this.emit('ready', this.backoffNumber_, this.backoffDelay_);
this.backoffNumber_++;
};

@@ -53,0 +74,0 @@

6

lib/strategy/strategy.js

@@ -49,3 +49,3 @@ /*

* Retrieves the maximal backoff delay.
* @return The maximal backoff delay.
* @return The maximal backoff delay, in milliseconds.
*/

@@ -58,3 +58,3 @@ BackoffStrategy.prototype.getMaxDelay = function() {

* Retrieves the initial backoff delay.
* @return The initial backoff delay.
* @return The initial backoff delay, in milliseconds.
*/

@@ -79,2 +79,3 @@ BackoffStrategy.prototype.getInitialDelay = function() {

* @return The backoff delay, in milliseconds.
* @protected
*/

@@ -94,2 +95,3 @@ BackoffStrategy.prototype.next_ = function() {

* Resets the backoff delay to its initial value.
* @protected
*/

@@ -96,0 +98,0 @@ BackoffStrategy.prototype.reset_ = function() {

{
"name": "backoff",
"description": "Fibonacci and exponential backoffs.",
"version": "1.0.0",
"version": "1.1.0",
"author": "Mathieu Turcotte <turcotte.mat@gmail.com>",

@@ -14,6 +14,6 @@ "keywords": ["backoff", "fibonacci", "exponential"],

"nodeunit": "0.7",
"jshint": "0.7"
"jshint": "0.9"
},
"scripts": {
"pretest": "node_modules/jshint/bin/hint lib/*.js lib/strategy/*.js tests/*.js examples/*.js",
"pretest": "node_modules/jshint/bin/hint lib/ lib/ tests/ examples/ index.js",
"test": "node_modules/nodeunit/bin/nodeunit tests/"

@@ -20,0 +20,0 @@ },

@@ -6,13 +6,15 @@ # Backoff for Node.js [![Build Status](https://secure.travis-ci.org/MathieuTurcotte/node-backoff.png?branch=master)](http://travis-ci.org/MathieuTurcotte/node-backoff)

## Installation
```
npm install backoff
```
## Usage
In order to use backoff, require `backoff`.
## Unit tests
```js
var backoff = require('backoff');
```
npm test
```
## Usage
The usual way to instantiate a new `Backoff` object is to use one predefined

@@ -27,10 +29,15 @@ factory method: `backoff.fibonacci([options])`, `backoff.exponential([options])`.

``` js
var backoff = require('backoff');
var fibonacciBackoff = backoff.fibonacci({
randomisationFactor: 0,
initialDelay: 10,
maxDelay: 1000
maxDelay: 300
});
fibonacciBackoff.failAfter(10);
fibonacciBackoff.on('backoff', function(number, delay) {
// Do something when backoff starts.
// Do something when backoff starts, e.g. show to the
// user the delay before next reconnection attempt.
console.log(number + ' ' + delay + 'ms');

@@ -40,8 +47,13 @@ });

fibonacciBackoff.on('ready', function(number, delay) {
// Do something when backoff ends.
if (number < 15) {
fibonacciBackoff.backoff();
}
// Do something when backoff ends, e.g. retry a failed
// operation (DNS lookup, API call, etc.).
fibonacciBackoff.backoff();
});
fibonacciBackoff.on('fail', function() {
// Do something when the maximum number of backoffs is
// reached, e.g. ask the user to check its connection.
console.log('fail');
});
fibonacciBackoff.backoff();

@@ -61,14 +73,9 @@ ```

7 210ms
8 340ms
9 550ms
10 890ms
11 1000ms
12 1000ms
13 1000ms
14 1000ms
15 1000ms
8 300ms
9 300ms
fail
```
Backoff objects are meant to be instantiated once and reused several times
by calling `reset` after a successful "retry".
Note that `Backoff` objects are meant to be instantiated once and reused
several times by calling `reset` after a successful "retry".

@@ -106,2 +113,11 @@ ## API

#### backoff.failAfter(numberOfBackoffs)
- numberOfBackoffs: maximum number of backoffs before the fail event gets
emitted, must be greater than 0
Sets a limit on the maximum number of backoffs that can be performed before
a fail event gets emitted and the backoff instance is reset. By default, there
is no limit on the number of backoffs that can be performed.
#### backoff.backoff()

@@ -142,2 +158,9 @@

#### Event: 'fail'
Emitted when the maximum number of backoffs is reached. This event will only
be emitted if the client has set a limit on the number of backoffs by calling
`backoff.failAfter(numberOfBackoffs)`. The backoff instance is automatically
reset after this event is emitted.
### Interface BackoffStrategy

@@ -144,0 +167,0 @@

@@ -74,2 +74,23 @@ /*

"the fail event should be emitted when backoff limit is reached": function(test) {
this.backoffStrategy.next.returns(10);
var spy = new sinon.spy();
this.backoff.on('fail', spy);
this.backoff.failAfter(2);
// Consume first 2 backoffs.
for (var i = 0; i < 2; i++) {
this.backoff.backoff();
this.clock.tick(10);
}
// Failure should occur on the third call, and not before.
test.ok(!spy.calledOnce);
this.backoff.backoff();
test.ok(spy.calledOnce);
test.done();
},
"calling backoff while a backoff is in progress should throw an error": function(test) {

@@ -88,2 +109,10 @@ this.backoffStrategy.next.returns(10);

"backoff limit should be greater than 0": function(test) {
var backoff = this.backoff;
test.throws(function() {
backoff.failAfter(0);
});
test.done();
},
"reset should cancel any backoff in progress": function(test) {

@@ -110,2 +139,15 @@ this.backoffStrategy.next.returns(10);

"backoff should be reset after fail": function(test) {
this.backoffStrategy.next.returns(10);
this.backoff.failAfter(1);
this.backoff.backoff();
this.clock.tick(10);
this.backoff.backoff();
test.ok(this.backoffStrategy.reset.calledOnce);
test.done();
},
"the backoff number should increase from 0 to N - 1": function(test) {

@@ -112,0 +154,0 @@ this.backoffStrategy.next.returns(10);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc