Skip to content

Commit

Permalink
Fix ip-sticky example hanging
Browse files Browse the repository at this point in the history
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: nodejs/node#13435
  • Loading branch information
JaoodxD authored and tshemsedinov committed Nov 15, 2023
1 parent 44976c9 commit 9c69cb2
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ip-sticky/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -45,6 +45,7 @@ if (cluster.isPrimary) {
if (message.name === 'socket') {
socket.server = server;
server.emit('connection', socket);
socket.resume();
}
});
}

0 comments on commit 9c69cb2

Please sign in to comment.