backoff-strategies
v1.1.0
Published
Collection of backoff strategies with a common API
Downloads
1,339
Readme
node-backoff-strategies
A genericised set of backoff-suitable algorithms.
Installation
npm install backoff-strategies
General Options
minValue
(default: 0) - the smallest value that may be returnedmaxValue
(default: Infinity) - the largest value that may be returnedmultiplier
(default: 1) - the scaling factor of the return valuerandomisationFactor
(default: 0) - increase the returned value by a random amountzeroMeansZero
(default: true) - overrides all other settings to always return 0 when i=0`i
(default: 0) - the initial i value (when usingstrategy.next()
for generation)
General Methods
get(i)
- returns the computed value for this step according to the strategynext()
- returnsget(i)
for the current i value, then increments ireset()
- resets i to zero
Usage
Linear Strategy
A monotonically increasing strategy, e.g. 0,1,2,3,4,5
const Strategies = require('backoff-strategies');
var linearBackoff = new Strategies.Linear();
console.log(linearBackoff.next()); // 0
console.log(linearBackoff.next()); // 1
console.log(linearBackoff.next()); // 2
console.log(linearBackoff.get(6)); // 6
Defined Strategy
A predefined strategy for granular control
const Strategies = require('backoff-strategies');
var definedBackoff = new Strategies.Defined({values: [1, 5, 6, 3]});
console.log(definedBackoff.next()); // 1
console.log(definedBackoff.next()); // 5
console.log(definedBackoff.next()); // 6
console.log(definedBackoff.next()); // 3
console.log(definedBackoff.next()); // 3
console.log(definedBackoff.get(6)); // 3
Fibonnaci Strategy
A strategy increasing according to a Fabonacci sequence (1,1,2,3,5,8,13)
const Strategies = require('backoff-strategies');
var fibonacciBackoff = new Strategies.Fibonacci({zeroMeansZero: false});
console.log(fibonacciBackoff.next()); // 1
console.log(fibonacciBackoff.next()); // 1
console.log(fibonacciBackoff.next()); // 2
console.log(fibonacciBackoff.next()); // 3
console.log(fibonacciBackoff.get(6)); // 13
Exponential Strategy
An exponentially increasing strategy: Math.pow(this.factor, i - 1)
This accepts an additional factor
option.
const Strategies = require('backoff-strategies');
var expBackoff = new Strategies.Exponential({
randomisationFactor: 0.5,
multiplier: 10,
maxValue: 300
});
console.log(expBackoff.next()); // 0
console.log(expBackoff.next()); // 13 - between 10 and 15
console.log(expBackoff.next()); // 26 - between 20 and 30
console.log(expBackoff.next()); // 57 - between 40 and 60
console.log(expBackoff.get(5)); // 167 - between 160 and 240
Polynomial Strategy
A polynomially increasing strategy: Math.pow(i, this.factor)
This accepts an additional factor
option.
const Strategies = require('backoff-strategies');
var polyBackoff = new Strategies.Polynomial({
factor: 3
});
console.log(polyBackoff.next()); // 0
console.log(polyBackoff.next()); // 1
console.log(polyBackoff.next()); // 2 * 2 * 2 =8
console.log(polyBackoff.next()); // 3 * 3 * 3 = 27
console.log(polyBackoff.get(5)); // 5 * 5 * 5 = 125