1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ExponentialBackoff = void 0;
|
4 | class ExponentialBackoff {
|
5 | constructor(opts = {}) {
|
6 | this.factor = 2;
|
7 | this.jitter = 0;
|
8 | this.attempts = 0;
|
9 | this.ms = opts.min || 100;
|
10 | this.max = opts.max || 10000;
|
11 | }
|
12 | duration() {
|
13 | var ms = this.ms * Math.pow(this.factor, this.attempts++);
|
14 | if (this.jitter) {
|
15 | var rand = Math.random();
|
16 | var deviation = Math.floor(rand * this.jitter * ms);
|
17 | ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
|
18 | }
|
19 | return Math.min(ms, this.max) | 0;
|
20 | }
|
21 | reset() {
|
22 | this.attempts = 0;
|
23 | }
|
24 | }
|
25 | exports.ExponentialBackoff = ExponentialBackoff;
|
26 |
|
\ | No newline at end of file |