javascript - Nodejs Cluster with socket.io loses connections -
i using node.js cluster module in order create multiprocess socket.io server.
using this example have created following server-client application:
server:
var cluster = require('cluster'); // start master process if (cluster.ismaster) { // create slave workers (in example there one) var worker = cluster.fork(); // starts server delegate connection worker var routerserver = net.createserver(function(connection) { worker.send({type: 'new_connection'}, connection); }); routerserver.listen(443, "my-ip-address"); } else { // start server on random port var slavehttp = require("http").server(); slavehttp.listen(0, "localhost"); // connect socket.io server var io = require('socket.io')(slavehttp); io.on('connection', function (socket) { console.log("connected"); }); // on server messsage (new connection) emit server process.on('message', function(message, handle) { if (message.type == 'new_connection') { slavehttp.emit('connection', handle); }; }); }
client:
var io = require('socket.io-client'); function connect(index) { connection = io.connect("http://my-ip-address", { reconnection: true, reconnectiondelay: 1000, timeout: 3000, multiplex: false }); connection.on('connect', function (socket) { console.log(index + ': connected'); }); connection.on('connect_error', function (e) { console.log(index + ': connection error: ' + e); }); connection.on('message', function () { console.log(index + ': server message'); }); connection.on('disconnect', function () { console.log(index + ': disconnect'); }); } (var = 0; < 10; i++) { settimeout(function(index) { return function() { connect(index); } }(i), * 5000); };
the problem of clients in above example managed connect server , exchange messages, others failing on timeout, weirder can see in server's console received connections timed-out clients reason don't succeed communicate.
if replace server code on same process code runs perfectly.
can me?
i found out bug in node.js, problem there auto-read machanism in node.js sockets starts read out of every new socket automatically.
when pass socket child process can either been read or not, under heavy load there more change socket read master (which of course cuasing problem), therefore should explicitly ask stop reading socket.
the following line nice workaround (should added before worker.send call):
connection._handle.readstop();
Comments
Post a Comment