-
Notifications
You must be signed in to change notification settings - Fork 7
/
tcpMaster.js
80 lines (66 loc) · 1.49 KB
/
tcpMaster.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
var cp = require('child_process');
var TCP = process.binding('tcp_wrap').TCP;
var PORT = 3458;
var WORKER_NUMBER = 5;
var GRACE_EXIT_TIME = 2000;//2s
var WORKER_PATH = __dirname + '/tcpWorker.js';
var WORKER_HEART_BEAT = 10*1000;//10s, update memory ,etc
function main(fn){
fn();
}
function output(str){
console.log(str);
}
var childs = [];
var lastChildPos = 0;
function startWorker(){
for(var i=0; i<WORKER_NUMBER; i++){
var c = cp.fork(WORKER_PATH);
childs.push(c);
}
/*
setInterval(function(){
inspect(childMng.getStatus());
childMng.updateStatus();
},WORKER_HEART_BEAT);
*/
}
var server = null;
var exitTimer = null;
function aboutExit(){
if(exitTimer) return;
server.close();
childs.forEach(function(c){
c.kill();
})
exitTimer = setTimeout(function(){
output('master exit...');
//log.destroy();
process.exit(0);
}, GRACE_EXIT_TIME);
}
//var ADDRESS = '127.0.0.1';
var ADDRESS = '0.0.0.0';
var BACK_LOG = 128;
function onconnection(handle){
//output('master on connection');
lastChildPos++;
if(lastChildPos >= WORKER_NUMBER){
lastChildPos = 0;
}
childs[lastChildPos].send({'handle' : true}, handle);
handle.close();
}
function startServer(){
server = new TCP();
server.bind(ADDRESS, PORT);
server.onconnection = onconnection;
server.listen(BACK_LOG);
}
void main(function(){
startServer();
startWorker();
output('master is running...');
process.on('SIGINT' , aboutExit);
process.on('SIGTERM' , aboutExit);
});