forked from AAMasters/ExchangeGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchangeUtils.js
57 lines (49 loc) · 1.27 KB
/
exchangeUtils.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
// generic low level reusuable utils for interacting with exchanges.
const retry = require('retry');
const retryInstance = (options, checkFn, callback) => {
if (!options) {
options = {
retries: 30,
factor: 1.5,
minTimeout: 1 * 1000,
maxTimeout: 8 * 1000
};
}
const operation = retry.operation(options);
operation.attempt(function (currentAttempt) {
checkFn((err, result) => {
if (!err) {
callback(global.DEFAULT_OK_RESPONSE, result);
return;
} else if (err.result === global.CUSTOM_OK_RESPONSE.result) {
callback(err, result);
return;
}
if (currentAttempt > options.retries) {
callback(global.DEFAULT_FAIL_RESPONSE);
return;
}
if (err.notFatal) {
if (err.backoffDelay) {
setTimeout(() => operation.retry(err), err.backoffDelay);
}
console.log("Retrying Exchange Operation at: " + new Date().toISOString())
operation.retry(err);
} else {
callback(err, result);
}
});
});
}
const includes = function (str, list) {
for (let i = 0; i < list.length; i++) {
const element = list[i];
if (element === str) {
return true;
}
}
}
module.exports = {
retry: retryInstance,
includes
}