-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCluster.js
88 lines (71 loc) · 1.88 KB
/
testCluster.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cluster from 'cluster'
import os from 'os'
const numCPUs = os.cpus().length;
import http from 'http'
import axios from 'axios'
let i = 0
async function requestRouter() {
await axios.get("http://localhost:2000").then((req) => { console.log(i + " | " + req.data) })
}
async function requestRouterAnt() {
await axios.get("http://localhost:2000").then((req) => { console.log(i + " | " + req.data) })
}
async function criaCluster() {
if (cluster.isPrimary) {
console.log('Primary process rodando');
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} morreu, o código é: ${code}, e sinal: ${signal}`);
console.log('Iniciando um novo Worker');
cluster.fork();
});
setTimeout(() => {
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
i++
}, 4000)
setTimeout(() => {
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
i++
}, 8000)
setTimeout(() => {
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
i++
}, 12000)
setTimeout(() => {
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
requestRouterAnt()
i++
}, 16000)
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end(`eu sou o worker ${process.pid}\n`);
}).listen(2000);
console.log('Rodando na porta 2000 processo ' + process.pid);
await requestRouter()
}
}
await criaCluster()