-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple-backoff.js
More file actions
137 lines (123 loc) · 3.61 KB
/
simple-backoff.js
File metadata and controls
137 lines (123 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
var inherits = require('util').inherits;
function Backoff(opts) {
opts = opts || { };
var min = parseInt(opts.min, 10),
max = parseInt(opts.max, 10),
jitter = parseFloat(opts.jitter);
this.min = !isNaN(min) ? min : 10;
this.max = !isNaN(max) ? max: 10*1000;
this.jitter = !isNaN(jitter) && jitter > 0 && jitter <= 1 ? jitter : 0;
this.reset();
}
Backoff.prototype.next = function () {
if (this.jitter) {
var spread = this._spread() * this.jitter;
this.cur += (Math.random() * spread) - (spread / 2);
}
this.cur = Math.max(0, Math.min(this.max, Math.floor(this.cur)));
var cur = this.cur;
this._step();
return cur;
};
Backoff.prototype.reset = function () {
this._reset();
};
function LinearBackoff(opts) {
opts = opts || { };
Backoff.call(this, opts);
var step = parseInt(opts.step, 10);
this.step = !isNaN(step) && step > 0 ? step : 50;
}
inherits(LinearBackoff, Backoff);
LinearBackoff.prototype._spread = function () {
return this.step;
};
LinearBackoff.prototype._step = function () {
this.cur = this.cur + this.step;
};
LinearBackoff.prototype._reset = function () {
this.cur = this.min;
};
function FibonacciBackoff(opts) {
opts = opts || { };
Backoff.call(this, opts);
this.last = 0;
}
inherits(FibonacciBackoff, Backoff);
FibonacciBackoff.prototype._spread = function () {
return (this.cur === this.last) ? this.cur : this.cur - this.last;
};
FibonacciBackoff.prototype._step = function () {
var next = this.last + this.cur;
if (next === 0) { next = 1; }
this.last = this.cur;
this.cur = next;
};
FibonacciBackoff.prototype._reset = function () {
this.cur = this.min;
this.last = 0;
};
function ExponentialBackoff(opts) {
opts = opts || { };
Backoff.call(this, opts);
var factor = parseFloat(opts.factor);
this.factor = !isNaN(factor) && factor > 1 ? factor : 2;
}
inherits(ExponentialBackoff, Backoff);
ExponentialBackoff.prototype._spread = function () {
return this.cur - (this.cur / this.factor);
};
ExponentialBackoff.prototype._step = function () {
if (this.cur === 0) { this.cur = 1; }
else { this.cur *= this.factor; }
};
ExponentialBackoff.prototype._reset = function () {
this.cur = this.min;
};
var SEQUENCE_ERROR = 'FixedBackoff: `sequence` is required, and must be an array of one or more integers >= 0';
function FixedBackoff(opts) {
opts = opts || { };
var sequence = opts.sequence;
if (!sequence || !Array.isArray(sequence) || sequence.length === 0) {
throw new Error(SEQUENCE_ERROR);
}
sequence.forEach(function (num) {
var parsed = parseInt(num, 10);
if (isNaN(parsed) || parsed < 0 || parsed !== num) {
throw new Error(SEQUENCE_ERROR);
}
});
if (opts.hasOwnProperty('min')) {
throw new Error('FixedBackoff: `min` is invalid for this strategy');
}
if (opts.hasOwnProperty('max')) {
throw new Error('FixedBackoff: `max` is invalid for this strategy');
}
this.idx = 0;
this.lastIdx = sequence.length - 1;
this.sequence = sequence;
Backoff.call(this, opts);
}
inherits(FixedBackoff, Backoff);
FixedBackoff.prototype._spread = function () {
return (this.idx === 0 ?
this.sequence[this.idx] :
Math.abs(this.sequence[this.idx] - this.sequence[this.idx - 1])
);
};
FixedBackoff.prototype._step = function () {
this.idx = Math.min(this.idx + 1, this.lastIdx);
this.cur = this.sequence[this.idx];
};
FixedBackoff.prototype._reset = function () {
this.idx = 0;
this.cur = this.sequence[this.idx];
};
module.exports = {
Backoff: Backoff,
LinearBackoff: LinearBackoff,
FibonacciBackoff: FibonacciBackoff,
ExponentialBackoff: ExponentialBackoff,
FixedBackoff: FixedBackoff
};