6 module.exports = Backoff;
9 * Initialize backoff timer with `opts`.
11 * - `min` initial timeout in milliseconds [100]
12 * - `max` max timeout [10000]
16 * @param {Object} opts
20 function Backoff(opts) {
22 this.ms = opts.min || 100;
23 this.max = opts.max || 10000;
24 this.factor = opts.factor || 2;
25 this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
30 * Return the backoff duration.
36 Backoff.prototype.duration = function(){
37 var ms = this.ms * Math.pow(this.factor, this.attempts++);
39 var rand = Math.random();
40 var deviation = Math.floor(rand * this.jitter * ms);
41 ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
43 return Math.min(ms, this.max) | 0;
47 * Reset the number of attempts.
52 Backoff.prototype.reset = function(){
57 * Set the minimum duration
62 Backoff.prototype.setMin = function(min){
67 * Set the maximum duration
72 Backoff.prototype.setMax = function(max){
82 Backoff.prototype.setJitter = function(jitter){