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 0.3.0 to 1.0.0

6

examples/exponential.js

@@ -11,4 +11,8 @@ #!/usr/bin/env node

testBackoff.on('backoff', function(number, delay) {
console.log(number + ' ' + delay + 'ms');
console.log('Backoff start: ' + number + ' ' + delay + 'ms');
});
testBackoff.on('ready', function(number, delay) {
console.log('Backoff done: ' + number + ' ' + delay + 'ms');
if (number < 15) {

@@ -15,0 +19,0 @@ testBackoff.backoff();

@@ -11,4 +11,8 @@ #!/usr/bin/env node

testBackoff.on('backoff', function(number, delay) {
console.log(number + ' ' + delay + 'ms');
console.log('Backoff start: ' + number + ' ' + delay + 'ms');
});
testBackoff.on('ready', function(number, delay) {
console.log('Backoff done: ' + number + ' ' + delay + 'ms');
if (number < 15) {

@@ -20,1 +24,2 @@ testBackoff.backoff();

testBackoff.backoff();

@@ -12,4 +12,8 @@ #!/usr/bin/env node

randomizedBackoff.on('backoff', function(number, delay) {
console.log(number + ' ' + delay + 'ms');
console.log('Backoff start: ' + number + ' ' + delay + 'ms');
});
randomizedBackoff.on('ready', function(number, delay) {
console.log('Backoff done: ' + number + ' ' + delay + 'ms');
if (number < 15) {

@@ -16,0 +20,0 @@ randomizedBackoff.backoff();

4

examples/reset.js

@@ -7,4 +7,4 @@ #!/usr/bin/env node

backoff.on('backoff', function(number, delay) {
console.log('backoff #' + number + ' ' + delay + 'ms');
backoff.on('ready', function(number, delay) {
console.log('Backoff done: ' + number + ' ' + delay + 'ms');

@@ -11,0 +11,0 @@ if (number < 15) {

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

module.exports.fibonnaci = function(options) {
console.log('Deprecated: use backoff.fibonacci instead.');
return new module.exports.fibonacci(options);
};
/**

@@ -30,0 +25,0 @@ * Constructs an exponential backoff.

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

this.timeoutID_ = setTimeout(this.handlers.backoff, this.backoffDelay_);
this.emit('backoff', this.backoffNumber_, this.backoffDelay_);
};

@@ -47,3 +48,3 @@

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

@@ -50,0 +51,0 @@

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

@@ -17,3 +17,3 @@ "keywords": ["backoff", "fibonacci", "exponential"],

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

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

@@ -6,3 +6,2 @@ # 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
```

@@ -22,8 +21,10 @@ npm install backoff

`Backoff` inherits from `EventEmitter`. One can listen for backoff completion
by listening for `backoff` events. Registered handlers will be called with the
current backoff number and delay.
`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.
``` js
var fibonacciBackoff = backoff.fibonacci({
randomisationFactor: 0,
initialDelay: 10,

@@ -34,4 +35,8 @@ 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) {

@@ -67,6 +72,12 @@ fibonacciBackoff.backoff();

Backoff objects are meant to be instantiated once and reused several times
by calling `reset` after each successful backoff operation.
by calling `reset` after a successful "retry".
## API
### backoff.fibonacci([options])
Constructs a Fibonacci backoff (10, 10, 20, 30, 50, etc.).
See bellow for the options description.
### backoff.exponential([options])

@@ -76,22 +87,12 @@

`options` is an object with the following defaults:
The options are:
```js
options = {
randomisationFactor: 0,
initialDelay: 100,
maxDelay: 10000
};
```
- randomisationFactor: defaults to 0, must be between 0 and 1
- initialDelay: defaults to 100 ms
- maxDelay: defaults to 10000 ms
With these values, the backoff delay will increase from 100ms to 10000ms. The
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.
### backoff.fibonacci([options])
Constructs a Fibonacci backoff (10, 10, 20, 30, 50, etc.).
The Fibonacci backoff has the same set of options as the exponential backoff.
### Class Backoff

@@ -127,6 +128,14 @@

- number: number of backoff since last reset
- delay: current backoff delay
- number: number of backoffs since last reset, starting at 0
- delay: backoff delay in milliseconds
Emitted on backoff completion, effectively signaling that the failing operation
Emitted when a backoff operation is started. Signals to the client how long
the next backoff delay will be.
#### Event: 'ready'
- number: number of backoffs since last reset, starting at 0
- delay: backoff delay in milliseconds
Emitted when a backoff operation is done. Signals that the failing operation
should be retried.

@@ -133,0 +142,0 @@

@@ -24,3 +24,3 @@ /*

"a backoff event should be emitted on backoff completion": function(test) {
"the backoff event should be emitted when backoff starts": function(test) {
this.backoffStrategy.next.returns(10);

@@ -32,2 +32,14 @@

this.backoff.backoff();
test.ok(spy.calledOnce);
test.done();
},
"the ready event should be emitted on backoff completion": function(test) {
this.backoffStrategy.next.returns(10);
var spy = new sinon.spy();
this.backoff.on('ready', spy);
this.backoff.backoff();
this.clock.tick(10);

@@ -39,2 +51,27 @@

"the backoff event should be passed the backoff delay": function(test) {
this.backoffStrategy.next.returns(989);
var spy = new sinon.spy();
this.backoff.on('backoff', spy);
this.backoff.backoff();
test.equal(spy.getCall(0).args[1], 989);
test.done();
},
"the ready event should be passed the backoff delay": function(test) {
this.backoffStrategy.next.returns(989);
var spy = new sinon.spy();
this.backoff.on('ready', spy);
this.backoff.backoff();
this.clock.tick(989);
test.equal(spy.getCall(0).args[1], 989);
test.done();
},
"calling backoff while a backoff is in progress should throw an error": function(test) {

@@ -57,3 +94,3 @@ this.backoffStrategy.next.returns(10);

var spy = new sinon.spy();
this.backoff.on('backoff', spy);
this.backoff.on('ready', spy);

@@ -63,3 +100,3 @@ this.backoff.backoff();

this.backoff.reset();
this.clock.tick(100); // 'backoff' should not be emitted.
this.clock.tick(100); // 'ready' should not be emitted.

@@ -70,3 +107,3 @@ test.equals(spy.callCount, 0);

"reset should reset the backoff delay generator": function(test) {
"reset should reset the backoff strategy": function(test) {
this.backoff.reset();

@@ -73,0 +110,0 @@ test.ok(this.backoffStrategy.reset.calledOnce);

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