-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.js
67 lines (56 loc) · 1.55 KB
/
utils.js
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
"use strict";
const mutex = require('ocore/mutex.js');
let watchedKeys = {};
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function die(msg) {
throw Error(msg);
}
async function checkForDeadlock(key) {
const t = setTimeout(die, 10 * 60 * 1000, `possible deadlock on ${key}`);
const unlock = await mutex.lock(key);
unlock();
clearTimeout(t);
}
function watchForDeadlock(key) {
if (watchedKeys[key])
return console.log('already watching for deadlock on ' + key);
watchedKeys[key] = true;
setInterval(() => checkForDeadlock(key), 10 * 60 * 1000);
}
function getVersion(versions, aa) {
for (let v in versions)
if (versions[v] === aa)
return v;
return null;
}
async function asyncCallWithTimeout(asyncPromise, timeLimit = 60000) {
let timeoutHandle;
const timeoutPromise = new Promise((_resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error(`async call timeout limit ${timeLimit} reached`)),
timeLimit
);
});
return Promise.race([asyncPromise, timeoutPromise]).then(result => {
clearTimeout(timeoutHandle);
return result;
});
}
function isRateLimitError(errMsg) {
return (
errMsg.includes("Your app has exceeded its compute units per second capacity")
||
errMsg.includes("rate-limit")
||
errMsg.includes("project ID request rate exceeded")
||
errMsg.includes("Too Many Requests")
);
}
exports.asyncCallWithTimeout = asyncCallWithTimeout;
exports.wait = wait;
exports.watchForDeadlock = watchForDeadlock;
exports.getVersion = getVersion;
exports.isRateLimitError = isRateLimitError;