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.Timeout;
|
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 |
|
83 |
|
84 |
|
85 | private endTime: Date = new Date();
|
86 |
|
87 | constructor(private callback: () => void, options?: BackoffOptions) {
|
88 | if (options) {
|
89 | if (options.initialDelay) {
|
90 | this.initialDelay = options.initialDelay;
|
91 | }
|
92 | if (options.multiplier) {
|
93 | this.multiplier = options.multiplier;
|
94 | }
|
95 | if (options.jitter) {
|
96 | this.jitter = options.jitter;
|
97 | }
|
98 | if (options.maxDelay) {
|
99 | this.maxDelay = options.maxDelay;
|
100 | }
|
101 | }
|
102 | this.nextDelay = this.initialDelay;
|
103 | this.timerId = setTimeout(() => {}, 0);
|
104 | clearTimeout(this.timerId);
|
105 | }
|
106 |
|
107 | private runTimer(delay: number) {
|
108 | this.endTime = this.startTime;
|
109 | this.endTime.setMilliseconds(
|
110 | this.endTime.getMilliseconds() + this.nextDelay
|
111 | );
|
112 | clearTimeout(this.timerId);
|
113 | this.timerId = setTimeout(() => {
|
114 | this.callback();
|
115 | this.running = false;
|
116 | }, delay);
|
117 | if (!this.hasRef) {
|
118 | this.timerId.unref?.();
|
119 | }
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 | runOnce() {
|
126 | this.running = true;
|
127 | this.startTime = new Date();
|
128 | this.runTimer(this.nextDelay);
|
129 | const nextBackoff = Math.min(
|
130 | this.nextDelay * this.multiplier,
|
131 | this.maxDelay
|
132 | );
|
133 | const jitterMagnitude = nextBackoff * this.jitter;
|
134 | this.nextDelay =
|
135 | nextBackoff + uniformRandom(-jitterMagnitude, jitterMagnitude);
|
136 | }
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 | stop() {
|
143 | clearTimeout(this.timerId);
|
144 | this.running = false;
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 | reset() {
|
152 | this.nextDelay = this.initialDelay;
|
153 | if (this.running) {
|
154 | const now = new Date();
|
155 | const newEndTime = this.startTime;
|
156 | newEndTime.setMilliseconds(newEndTime.getMilliseconds() + this.nextDelay);
|
157 | clearTimeout(this.timerId);
|
158 | if (now < newEndTime) {
|
159 | this.runTimer(newEndTime.getTime() - now.getTime());
|
160 | } else {
|
161 | this.running = false;
|
162 | }
|
163 | }
|
164 | }
|
165 |
|
166 | |
167 |
|
168 |
|
169 | isRunning() {
|
170 | return this.running;
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 |
|
177 | ref() {
|
178 | this.hasRef = true;
|
179 | this.timerId.ref?.();
|
180 | }
|
181 |
|
182 | |
183 |
|
184 |
|
185 |
|
186 | unref() {
|
187 | this.hasRef = false;
|
188 | this.timerId.unref?.();
|
189 | }
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 | getEndTime() {
|
196 | return this.endTime;
|
197 | }
|
198 | }
|