-
Notifications
You must be signed in to change notification settings - Fork 9
/
withproxy.js
74 lines (68 loc) · 1.74 KB
/
withproxy.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
68
69
70
71
72
73
74
const got = require('got');
const serversList = require('./servers');
const tunnel = require('tunnel');
let si = 0;
const apiurl = 'https://api.binance.com/api/v3/ticker/24hr'; //weight = 40 => 30 req/min
const urlA = [...new Array(50)].map((u) => apiurl);
const getHost = () => {
si++;
if (si === serversList.length) si = 0;
const [host, port] = serversList[si].split(':');
return { host, port };
};
const procreq = async ({ url }) => {
try {
const request = got(url, {
agent: {
https: tunnel.httpsOverHttp({
proxy: getHost(),
}),
},
});
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
request.cancel();
resolve('');
}, 5 * 1000);
});
return await Promise.race([request, p2]);
} catch (err) {
throw err;
}
};
const processWithRetry = async ({ url }) => {
try {
const ra = [...new Array(20)].map((r) => 1); //retry 20 times
let apiresp;
let retryAttempt = 0;
const resp = await ra.reduce(async (previousPromise, nextID) => {
try {
const res = await previousPromise;
if (!res?.body && !apiresp) {
retryAttempt++;
return procreq({ url });
}
if (res?.body) apiresp = res?.body;
return Promise.resolve();
} catch (err) {
retryAttempt++;
return procreq({ url });
}
}, Promise.resolve());
return { retryAttempt, success: retryAttempt < 20 ? 'success' : 'error' };
} catch (err) {
console.log(err);
return '';
}
};
const tryall = async () => {
try {
const results = await Promise.all(
urlA.map((url) => processWithRetry({ url }))
);
console.table(results);
} catch (err) {
console.log(err);
}
};
tryall();