Search code examples
node.jssocketsobservers

nodejs net socket starts dropping connections when on heavy load


I'm using net.createServer(host, port) + flash socket client. When my game works for several hours and get on high load - the server starts to drop connections. The client get the "connected" event and immediately after that - the "close" event. It loops like this 3-4-5-6 times and connects. The server is not under heavy load. There is plenty of RAM and CPU resources.

My code looks like this:

var net = require("net"); 
server = net.createServer(function(socket) {
                socket.setTimeout(15000);
                socket.setKeepAlive(true);
                //used to store string socket data untill packet delimiter is received
                socket.myBuffer = ""; //(and then process the JSON message)

                socket.on("data", onData); 
                socket.on("error", onError);            
                socket.on("end", onClientDisconnect);
                socket.on("timeout", onClientDisconnect); 
});  
server.listen(port);

How can I debug the issue ? Is there any kind of code (mine) that could cause such thing ?


Solution

  • It turned out to be because I did not checked for writable socket, before write something and becasuse I close the connection inside onError handler. So what is happening is: socket.write(..) triggers error because the socket is not writable (although is connected) and on error I close the socket, so the client starts reconnecting and this loops 3-7 times untill sending goes without exception.