CNode

请教这段代码cluster worker和net模块一起使用为何会出现阻塞?

问答
Ggastrodia发布于11 年前最后回复11 年前6 回复5361 浏览0 收藏

很神奇的是,在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);
  });
}

查看回复

回复 (6)

N
NoahZhang#1·11 年前

不是阻塞了,socket没有关闭导致的

G
gastrodia#2·11 年前
G
gastrodia#3·11 年前
引用 NoahZhang不是阻塞了,socket没有关闭导致的

@NoahZhang 为什么在master里调用的时候就不会出问题呢?

N
NoahZhang#4·11 年前

-c为2时,可以尝试调用一下socket.end() 就不会出现超时的情况了,我再试一下其他的状况

G
gastrodia#6·11 年前

我试了加了req.socket.end(),还是不行 应该不是socket没关。ab -n 10 -c 2 url 发起测试 收到了10个socket链接,但只产生了9个http链接

参与回复
登录后即可参与回复。登录