-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathprobe.mjs
78 lines (66 loc) · 1.93 KB
/
probe.mjs
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
import IO from 'socket.io-client';
import TCPPing from 'tcp-ping';
import Pool from 'es6-promise-pool';
import config from './config.mjs';
const io = IO(config.probe.master_url + '?token=' + config.probe.token + '&probe=' + config.probe.probe);
io.on('ping', (data) => {
if(!Array.isArray(data))
return;
pingAll(data).then(results => {
io.emit('ping_results', results);
}).catch(console.error);
});
function pingAll(instances) {
return new Promise((resolve, reject) => {
let results = [];
function * it() {
for(let instance of instances) {
yield pingOne(instance);
}
}
const pool = new Pool(it(), config.probe.concurrency);
pool.addEventListener('fulfilled', (event) => {
results.push(event.data.result);
});
pool.start().then(() => {
resolve(results);
}).catch(reject);
});
}
function pingOne(instance) {
let p = new Promise((resolve) => {
TCPPing.ping({
address: instance.name,
port: 443,
timeout: 1000
}, (err, rs) => {
if(err) {
return resolve({
inst: instance.id,
date: new Date(),
err: err.message
});
}
let jit = 0;
for(let e of rs.results) {
if(e.err) {
return resolve({
inst: instance.id,
date: new Date(),
err: e.err.message
});
}
jit += Math.abs(rs.avg - e.time);
}
jit /= rs.attempts;
return resolve({
inst: instance.id,
date: new Date(),
avg: rs.avg,
jit
});
});
});
p.instance = instance;
return p;
}