UNPKG

3.36 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.BackoffTimeout = void 0;
20const INITIAL_BACKOFF_MS = 1000;
21const BACKOFF_MULTIPLIER = 1.6;
22const MAX_BACKOFF_MS = 120000;
23const BACKOFF_JITTER = 0.2;
24/**
25 * Get a number uniformly at random in the range [min, max)
26 * @param min
27 * @param max
28 */
29function uniformRandom(min, max) {
30 return Math.random() * (max - min) + min;
31}
32class BackoffTimeout {
33 constructor(callback, options) {
34 this.callback = callback;
35 this.initialDelay = INITIAL_BACKOFF_MS;
36 this.multiplier = BACKOFF_MULTIPLIER;
37 this.maxDelay = MAX_BACKOFF_MS;
38 this.jitter = BACKOFF_JITTER;
39 this.running = false;
40 this.hasRef = true;
41 if (options) {
42 if (options.initialDelay) {
43 this.initialDelay = options.initialDelay;
44 }
45 if (options.multiplier) {
46 this.multiplier = options.multiplier;
47 }
48 if (options.jitter) {
49 this.jitter = options.jitter;
50 }
51 if (options.maxDelay) {
52 this.maxDelay = options.maxDelay;
53 }
54 }
55 this.nextDelay = this.initialDelay;
56 this.timerId = setTimeout(() => { }, 0);
57 clearTimeout(this.timerId);
58 }
59 /**
60 * Call the callback after the current amount of delay time
61 */
62 runOnce() {
63 var _a, _b;
64 this.running = true;
65 this.timerId = setTimeout(() => {
66 this.callback();
67 this.running = false;
68 }, this.nextDelay);
69 if (!this.hasRef) {
70 (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
71 }
72 const nextBackoff = Math.min(this.nextDelay * this.multiplier, this.maxDelay);
73 const jitterMagnitude = nextBackoff * this.jitter;
74 this.nextDelay =
75 nextBackoff + uniformRandom(-jitterMagnitude, jitterMagnitude);
76 }
77 /**
78 * Stop the timer. The callback will not be called until `runOnce` is called
79 * again.
80 */
81 stop() {
82 clearTimeout(this.timerId);
83 this.running = false;
84 }
85 /**
86 * Reset the delay time to its initial value.
87 */
88 reset() {
89 this.nextDelay = this.initialDelay;
90 }
91 isRunning() {
92 return this.running;
93 }
94 ref() {
95 var _a, _b;
96 this.hasRef = true;
97 (_b = (_a = this.timerId).ref) === null || _b === void 0 ? void 0 : _b.call(_a);
98 }
99 unref() {
100 var _a, _b;
101 this.hasRef = false;
102 (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
103 }
104}
105exports.BackoffTimeout = BackoffTimeout;
106//# sourceMappingURL=backoff-timeout.js.map
\No newline at end of file