-
Notifications
You must be signed in to change notification settings - Fork 7
/
tcpWorker.js
71 lines (60 loc) · 1.43 KB
/
tcpWorker.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
var net = require('net');
var http = require('http');
function output(str){
console.log(str);
}
function main(fn){
fn();
}
var GRACE_EXIT_TIME = 1500;
var server = null;
var exitTimer = null;
var childReqCount = 0;
function aboutExit(){
if(exitTimer) return;
//server.close();
exitTimer = setTimeout(function(){
output('worker will exit...');
output('child req total : ' + childReqCount);
process.exit(0);
},GRACE_EXIT_TIME);
}
function onhandle(self, handle){
if(self.maxConnections && self.connections >= self.maxConnections){
handle.close();
return;
}
var socket = new net.Socket({
handle : handle,
allowHalfOpen : self.allowHalfOpen
});
socket.readable = socket.writable = true;
socket.resume();
self.connections++;
socket.server = self;
self.emit('connection', socket);
socket.emit('connect');
//output('get a connection');
}
void main(function(){
process.on('SIGINT' ,aboutExit)
process.on('SIGTERM' ,aboutExit)
server = http.createServer(function(req, res){
var r, i;
for(i=0; i<10000; i++){
r = Math.random();
}
res.writeHead(200 ,{'content-type' : 'text/html'});
res.end('hello,world');
childReqCount++;
});
process.on('message',function(m ,handle){
if(handle){
onhandle(server, handle);
}
if(m.status == 'update'){
process.send({'status' : process.memoryUsage()});
}
});
output('worker is running...');
});