很神奇的是,在master里直接调用socketHandler的话ab测试下不会阻塞
在worker里调用,ab测试并发数为1的时候不会阻塞
但像下面这样写,ab测试
ab -n 10 -c 2 http://localhost:10086
请求直接就阻塞了。。。
var net = require('net');
var http = require('http');
var cluster = require('cluster');
var recevice_socket_count = 0;
var recevice_http_count = 0;
var httpServer = http.createServer(function(req,res){
console.log('recevice_http_count',++recevice_http_count);
res.write('wow');
res.end();
});
function socketHandler(socket){
console.log('recevice_socket_count',++recevice_socket_count);
socket.readable = socket.writeable = true;
httpServer.emit('connection',socket);
socket.emit('connect');
}
if(cluster.isMaster){
var worker = cluster.fork();
net.createServer(function(socket){
worker.send("socket", socket);
//socketHandler(socket);
}).listen(10086, function() {
console.log('netServer bound');
});
}else{
cluster.worker.on("message", function(data, socket) {
socketHandler(socket);
});
}