1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const INITIAL_BACKOFF_MS = 1000;
|
19 | const BACKOFF_MULTIPLIER = 1.6;
|
20 | const MAX_BACKOFF_MS = 120000;
|
21 | const BACKOFF_JITTER = 0.2;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function uniformRandom(min: number, max: number) {
|
29 | return Math.random() * (max - min) + min;
|
30 | }
|
31 |
|
32 | export interface BackoffOptions {
|
33 | initialDelay?: number;
|
34 | multiplier?: number;
|
35 | jitter?: number;
|
36 | maxDelay?: number;
|
37 | }
|
38 |
|
39 | export class BackoffTimeout {
|
40 | |
41 |
|
42 |
|
43 | private readonly initialDelay: number = INITIAL_BACKOFF_MS;
|
44 | |
45 |
|
46 |
|
47 | private readonly multiplier: number = BACKOFF_MULTIPLIER;
|
48 | |
49 |
|
50 |
|
51 | private readonly maxDelay: number = MAX_BACKOFF_MS;
|
52 | |
53 |
|
54 |
|
55 |
|
56 | private readonly jitter: number = BACKOFF_JITTER;
|
57 | |
58 |
|
59 |
|
60 | private nextDelay: number;
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | private timerId: NodeJS.Timer;
|
67 | |
68 |
|
69 |
|
70 | private running = false;
|
71 | |
72 |
|
73 |
|
74 |
|
75 | private hasRef = true;
|
76 | |
77 |
|
78 |
|
79 |
|
80 | private startTime: Date = new Date();
|
81 |
|
82 | constructor(private callback: () => void, options?: BackoffOptions) {
|
83 | if (options) {
|
84 | if (options.initialDelay) {
|
85 | this.initialDelay = options.initialDelay;
|
86 | }
|
87 | if (options.multiplier) {
|
88 | this.multiplier = options.multiplier;
|
89 | }
|
90 | if (options.jitter) {
|
91 | this.jitter = options.jitter;
|
92 | }
|
93 | if (options.maxDelay) {
|
94 | this.maxDelay = options.maxDelay;
|
95 | }
|
96 | }
|
97 | this.nextDelay = this.initialDelay;
|
98 | this.timerId = setTimeout(() => {}, 0);
|
99 | clearTimeout(this.timerId);
|
100 | }
|
101 |
|
102 | private runTimer(delay: number) {
|
103 | clearTimeout(this.timerId);
|
104 | this.timerId = setTimeout(() => {
|
105 | this.callback();
|
106 | this.running = false;
|
107 | }, delay);
|
108 | if (!this.hasRef) {
|
109 | this.timerId.unref?.();
|
110 | }
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 | runOnce() {
|
117 | this.running = true;
|
118 | this.startTime = new Date();
|
119 | this.runTimer(this.nextDelay);
|
120 | const nextBackoff = Math.min(
|
121 | this.nextDelay * this.multiplier,
|
122 | this.maxDelay
|
123 | );
|
124 | const jitterMagnitude = nextBackoff * this.jitter;
|
125 | this.nextDelay =
|
126 | nextBackoff + uniformRandom(-jitterMagnitude, jitterMagnitude);
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 |
|
133 | stop() {
|
134 | clearTimeout(this.timerId);
|
135 | this.running = false;
|
136 | }
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 | reset() {
|
143 | this.nextDelay = this.initialDelay;
|
144 | if (this.running) {
|
145 | const now = new Date();
|
146 | const newEndTime = this.startTime;
|
147 | newEndTime.setMilliseconds(newEndTime.getMilliseconds() + this.nextDelay);
|
148 | clearTimeout(this.timerId);
|
149 | if (now < newEndTime) {
|
150 | this.runTimer(newEndTime.getTime() - now.getTime());
|
151 | } else {
|
152 | this.running = false;
|
153 | }
|
154 | }
|
155 | }
|
156 |
|
157 | |
158 |
|
159 |
|
160 | isRunning() {
|
161 | return this.running;
|
162 | }
|
163 |
|
164 | |
165 |
|
166 |
|
167 |
|
168 | ref() {
|
169 | this.hasRef = true;
|
170 | this.timerId.ref?.();
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 |
|
177 | unref() {
|
178 | this.hasRef = false;
|
179 | this.timerId.unref?.();
|
180 | }
|
181 | }
|