From ea9ad2dd920837a2825635318417d6f66e28a2d6 Mon Sep 17 00:00:00 2001 From: Maksym Shenderuk Date: Wed, 15 Nov 2023 13:45:27 +0300 Subject: [PATCH] Fix ip-sticky example hanging To prevent master process reading from socket which cause hanging we have to pause connection and manualy resume when child_process will be ready to process it. Mentions in Node.js documentation here [net.createSerer](https://nodejs.org/api/net.html#netcreateserveroptions-connectionlistener) ``` If pauseOnConnect is set to true, then the socket associated with each incoming connection will be paused, and no data will be read from its handle. This allows connections to be passed between processes without any data being read by the original process. To begin reading data from a paused socket, call socket.resume(). ``` The issue resolved with a help of following issue: https://github.com/nodejs/node/issues/13435 --- ip-sticky/server.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ip-sticky/server.js b/ip-sticky/server.js index eac2526..302908a 100644 --- a/ip-sticky/server.js +++ b/ip-sticky/server.js @@ -27,7 +27,7 @@ if (cluster.isPrimary) { if (worker) worker.send({ name: 'socket' }, socket); }; - const server = new net.Server(balancer); + const server = new net.Server({ pauseOnConnect: true }, balancer); server.listen(2000); } else { console.log(`Worker pid: ${process.pid}`); @@ -45,6 +45,7 @@ if (cluster.isPrimary) { if (message.name === 'socket') { socket.server = server; server.emit('connection', socket); + socket.resume(); } }); }