-
Notifications
You must be signed in to change notification settings - Fork 13
/
promises.ts
434 lines (372 loc) · 11 KB
/
promises.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import { debugLogger } from './logger';
import { toHumanTime } from './dates';
import {
isRetryableError,
TSError,
parseError,
isFatalError
} from './errors';
const logger = debugLogger('utils:promises');
/** promisified setTimeout */
export function pDelay<T = undefined>(delay?: number, arg?: T): Promise<T> {
return new Promise<T>((resolve) => {
setTimeout(resolve, delay ?? 0, arg);
});
}
let supportsSetImmediate = false;
try {
if (typeof globalThis.setImmediate === 'function') {
supportsSetImmediate = true;
}
} catch (err) {
supportsSetImmediate = false;
}
let supportsNextTick = false;
try {
if (typeof globalThis.process.nextTick === 'function') {
supportsNextTick = true;
}
} catch (err) {
supportsNextTick = false;
}
let supportsQueueMicroTask = false;
try {
if (typeof globalThis.queueMicrotask === 'function') {
supportsQueueMicroTask = true;
}
} catch (err) {
supportsQueueMicroTask = false;
}
/** promisified process.nextTick,setImmediate or setTimeout depending on your environment */
export function pImmediate<T = undefined>(arg?: T): Promise<T> {
return new Promise<T>((resolve) => {
if (supportsQueueMicroTask) {
if (typeof arg !== 'undefined') {
queueMicrotask(resolve.bind(resolve, arg));
} else {
queueMicrotask(resolve as () => T);
}
} else if (supportsNextTick) {
process.nextTick(resolve, arg);
} else if (supportsSetImmediate) {
if (typeof arg !== 'undefined') {
setImmediate(resolve, arg);
} else {
setImmediate(resolve as () => void);
}
} else {
setTimeout(resolve, 0, arg);
}
});
}
export interface PRetryConfig {
/**
* The number of retries to attempt before failing.
* This does not include the initial attempt
*
* @default 3
*/
retries: number;
/**
* The initial time to delay before retrying the function
*
* @default 500
*/
delay: number;
/**
* The maximum time to delay when retrying in milliseconds
*
* @default 60000
*/
maxDelay: number;
/**
* The backoff multiplier
*
* @default 2
*/
backoff: number;
/**
* If set to true, this will set fail with fatalError to true
*/
endWithFatal: boolean;
/**
* Set a error message prefix
*/
reason?: string;
/**
* Log function for logging any errors that occurred
*/
logError: (...args: any[]) => void;
/**
* If this not specified or is empty, all errors will be treated as retryable.
* If any of the items in the array match the error message,
* it will be considered retryable
*/
matches?: (string | RegExp)[];
}
/**
* A promise retry fn.
*/
export async function pRetry<T = any>(
fn: PromiseFn<T>,
options?: Partial<PRetryConfig>
): Promise<T> {
const config = Object.assign(
{
retries: 3,
delay: 500,
maxDelay: 60000,
backoff: 2,
matches: [],
_currentDelay: 0,
// @ts-expect-error
_context: undefined as PRetryContext,
},
options
);
if (!config._currentDelay) {
config._currentDelay = config.delay;
}
config._context = config._context || {
startTime: Date.now(),
attempts: 0,
};
config._context.attempts++;
try {
return await fn();
} catch (_err) {
let matches = true;
if (config.matches?.length) {
const rawErr = parseError(_err);
matches = config.matches.some((match) => {
const reg = new RegExp(match);
return reg.test(rawErr);
});
}
const context = {
...config._context,
duration: Date.now() - config._context.startTime,
};
const err = new TSError(_err, {
reason: config.reason,
context,
});
if (_err && _err.stack) {
err.stack += `\n${_err.stack
.split('\n')
.slice(1)
.join('\n')}`;
}
if (!isFatalError(err) && err.retryable == null) {
err.retryable = matches;
}
if (isRetryableError(err) && config.retries > 1) {
config.retries--;
config._currentDelay = getBackoffDelay(
config._currentDelay,
config.backoff,
config.maxDelay,
config.delay
);
await pDelay(config._currentDelay);
if (config.logError) {
config.logError(err, 'retry error, retrying...', {
...config,
_context: null,
});
}
return pRetry(fn, config);
}
err.retryable = false;
if (config.endWithFatal) {
err.fatalError = true;
}
throw err;
}
}
export type PWhileOptions = {
/** @defaults to -1 which is never */
timeoutMs?: number;
/** @defaults to "Request" */
name?: string;
/** enable jitter to stagger requests */
enabledJitter?: boolean;
/** the minimum the jitter wait time will be (in milliseconds),
* defaults to 100ms */
minJitter?: number
/** the maximum the jitter wait time will be (in milliseconds),
* defaults to 3x the minJitter setting, but less than timeoutMs
* */
maxJitter?: number
/** error that can be expressed if the timer ends because the function does not return true */
error?: string
};
/**
* Run a function until it returns true or throws an error
*/
export async function pWhile(fn: PromiseFn, options: PWhileOptions = {}): Promise<void> {
const {
timeoutMs = -1,
name = 'Request',
enabledJitter = false,
minJitter = 100,
} = options;
const maxJitter = options.maxJitter ?? minJitter * 3;
if (timeoutMs <= 2 && options.enabledJitter) {
throw new Error('Jitter cannot be enabled whe timeout is <=2');
}
const startTime = Date.now();
const endTime = timeoutMs > 0 ? startTime + timeoutMs : Number.POSITIVE_INFINITY;
let running = false;
let interval: any;
const promise = new Promise<void>((resolve, reject) => {
interval = setInterval(async () => {
if (running) return;
running = true;
const remaining = endTime - Date.now();
if (remaining <= 0) {
let errMsg = `${name} timeout after ${toHumanTime(timeoutMs)}`;
if (options.error) errMsg = `${errMsg}; ${options.error}`;
reject(
new TSError(errMsg, {
statusCode: 503,
})
);
return;
}
try {
if (enabledJitter) {
const jitterDelay = getBackoffDelay(
minJitter, 3, maxJitter, minJitter
);
// ensure we don't exceed the remaing time
const delay = Math.min(jitterDelay, remaining - 2);
if (delay > 2) {
await pDelay(delay);
}
}
const result = await fn();
if (result) {
resolve();
return;
}
running = false;
} catch (err) {
reject(err);
}
}, 1);
});
try {
await promise;
} finally {
clearInterval(interval);
}
}
/**
* Get backoff delay that will safe to retry and is slightly staggered
*/
export function getBackoffDelay(
current: number,
factor = 2,
max = 60000,
min = 500
): number {
// jitter is a floating point number between -0.2 and 0.8
const jitter = Math.random() * 0.8 + -0.2;
let n = current;
// ensure the number does not go below the min val
if (n < min) n = min;
// multiple the current backoff value by input factor and jitter
n *= factor + jitter;
// ensure the number does not exceed the max val
if (n > max) n = max;
// round it so it remains a whole number
return Math.round(n);
}
type PRetryContext = {
attempts: number;
startTime: number;
};
interface PromiseFn<T = any> {
(...args: any[]): Promise<T>;
}
/** Async waterfall function */
export function waterfall(input: unknown, fns: PromiseFn[], addBreak = false): Promise<any> {
let i = 0;
return fns.reduce(async (last, fn) => {
if (i++ === 0 && addBreak) await pImmediate();
return fn(await last);
}, input) as any;
}
/**
* Run multiple promises at once, and resolve/reject when the first completes
*/
export function pRace(promises: Promise<any>[], logError?: (err: any) => void): Promise<any> {
if (!promises || !Array.isArray(promises)) {
throw new Error('Invalid promises argument, must be an array');
}
return new Promise((resolve, reject) => {
let done = false;
promises.forEach(async (promise) => {
try {
const result = await promise;
resolve(result);
done = true;
} catch (err) {
if (done) {
if (logError) logError(err);
else logger.error(err, 'pRace reject with an error after complete');
}
reject(err);
}
});
});
}
/**
* Similar to pRace but with
*/
export async function pRaceWithTimeout(
promises: Promise<any>[] | Promise<any>,
timeout: number,
logError?: (err: any) => void
): Promise<any> {
if (!timeout || typeof timeout !== 'number') {
throw new Error('Invalid timeout argument, must be a number');
}
let timeoutId: any;
try {
const pTimeout = new Promise((resolve) => {
// add unref to avoid keeping the process open
timeoutId = setTimeout(resolve, timeout);
timeoutId.unref();
});
const _promises = Array.isArray(promises) ? promises : [promises];
await pRace([..._promises, pTimeout], logError);
} catch (err) {
if (logError) logError(err);
else console.error(err);
} finally {
clearTimeout(timeoutId);
}
}
/**
* An alternative to Bluebird.defer: http://bluebirdjs.com/docs/api/deferred-migration.html
* Considered bad practice in most cases, use the Promise constructor
*/
export function pDefer(): {
resolve: (value?: unknown) => void,
reject: (reason?: any) => void,
promise: Promise<any>
} {
let resolve: (value?: unknown) => void;
let reject: (reason?: any) => void;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
resolve: resolve!,
reject: reject!,
promise
};
}