Node.JS event loop socket delayed or thread blocked

I have a problem with node.js when sending a lot of concurrent request. The problem is that sometimes it seems it puts some request at the end of the event pool and give me the response after some serious time (60 seconds+, normal is under 10 seconds).

The story goes like this, i have 3 scripts, a CONSOLE , a SERVER and 10 CLIENTS .

console.js

// sending message
client.connect(port, host, function(connect_socket)
{
    client.sendMessage(code:301,... some json-socket message ...);

    client.on('message', function(message)
    {
        if(message.code == 304)
        {
            console.log(... print data ...)
        }
    });
}

server.js

server = net.createServer(function(socket)
{
    socket = new JsonSocket(socket);

    socket.on('message', function(message)
    {
        if(message.code == 301)
        {
            var client_random = get_random_client();
            client_random.sendMessage(code:302,... some json-socket message ...);
        }

        if(message.code == 303)
        {
            var client_return = get_client_by_id(message.return_client_id);
            client_return.sendMessage(code:304,... some json-socket message ...);
        }
    });
    });

});

client.js

client.connect(port, host, function(connect_socket)
{
    client.on('message', function(message)
    {
        if(message.code == 302)
        {
            execute_command(message.data, function(result_command)
            {
                client.sendMessage(code:303,... some json-socket message (good or bad check) ...)
            });


        }
    });
}

Arhitecture concept, console sends message to server, server to a random client, client executes an external program and sends output back to server, server sends response back to the console and console prints it.

console.js => server.js => client.js => server.js => console.js

I open the server, clients are connecting no problem. I open the console and type the command, i get every time the response under 10 seconds.

Now, i made another PHP script that would simulate 600 requests per second. I do the same thing, open console, send command, and once every 10 runs (1 of 10), the console waits , waits and waits, and after 60 seconds it gives me the result (10 was normal) .

I made some debug and it seems that server.js do not trigger message event when reciving from client.js and somehow puts it at the very end of the event pool , but never forget it, runs eventually.

I have double check :

  • console.js every time sends message to server.js (always instant)
  • server.js every time sends message to client.js (always instant)
  • client.js every time sends message to server.js (always instant)
  • [server.js do not fire the event message event instant, and put it on the very end of the event pool ]
  • server.js every time sends message to client.js (always instant)
  • Also i have checked for the possible I/O main thread block , everything is fine. All operations are async, no sync functions.

    It is that kind of bug that sometime it is manifesting, sometimes not. Like after a console.js waiting, you can open another terminal and console.js and send messages and see how it responds right away. As i already told, it is like a probability of 1 from 10.

    How can i solve this? I had made a lot of debugging.

    链接地址: http://www.djcxy.com/p/52704.html

    上一篇: 基于Node.js事件循环关闭Redis连接

    下一篇: Node.JS事件循环套接字延迟或线程阻塞