-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
84 lines (72 loc) · 2.51 KB
/
util.js
File metadata and controls
84 lines (72 loc) · 2.51 KB
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
function randomElementId() {
return Math.random().toString(36).substr(2, 9);
}
function versionCompare(v1, v2) {
const v1parts = v1.split(".").map((c) => parseInt(c, 10));
const v2parts = v2.split(".").map((c) => parseInt(c, 10));
while (v1parts.length > 0 && v2parts.length > 0) {
const c1 = v1parts.shift();
const c2 = v2parts.shift();
if (c1 > c2) {
return 1;
} else if (c1 < c2) {
return -1;
}
}
if (v1parts) {
return 1;
} else if (v2parts) {
return -1;
}
return 0;
}
/**
* Runs a function with exponential backoff retry logic
* @param {Function} fn - The function to execute (should return true for success, false for retry)
* @param {number} initialWaitMs - Initial wait time in milliseconds
* @param {number} maxWaitMs - Maximum wait time between retries (caps the exponential growth)
* @param {Function} [onRetry] - Optional callback called before each retry (receives current wait time)
* @returns {Promise<boolean>} - Resolves to true when function eventually succeeds (never rejects)
*/
function retryWithExponentialBackoff(fn, initialWaitMs, maxWaitMs, onRetry = null) {
return new Promise((resolve) => {
let currentWait = initialWaitMs;
function attempt() {
const result = fn();
if (result) {
resolve(result);
return;
}
if (onRetry) {
onRetry(currentWait);
}
setTimeout(() => {
// Double the wait time, but cap it at maxWaitMs
currentWait = Math.min(currentWait * 2, maxWaitMs);
attempt();
}, currentWait);
}
attempt();
});
}
// Alternative version that supports async functions
async function retryWithExponentialBackoffAsync(asyncFn, initialWaitMs, maxWaitMs, onRetry = null) {
let currentWait = initialWaitMs;
while (true) {
try {
const result = await asyncFn();
if (result) {
return result;
}
} catch (error) {
// Treat exceptions as failure and continue retrying
log.warn('init', 'Function threw error, retrying:', error);
}
if (onRetry) {
onRetry(currentWait);
}
await new Promise(resolve => setTimeout(resolve, currentWait));
// Double the wait time, but cap it at maxWaitMs
currentWait = Math.min(currentWait * 2, maxWaitMs);
}
}