Skip to content

Commit

Permalink
fix(cluster): respect the backlog from workers
Browse files Browse the repository at this point in the history
Currently, the master process would ignore `backlog` from worker
processes and use the default value instead. This commit will respect
the first `backlog` passed to the master process for a specific handle.

Refs: #4056
  • Loading branch information
oyyd committed Aug 18, 2020
1 parent 6726246 commit 5e339ec
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ server.listen({
});
```

When `exclusive` is `true` and the underlying handle is shared, it's
possible that several workers query a handle with different backlogs.
In this case, the first `backlog` passed to the master process will be used.

Starting an IPC server as root may cause the server path to be inaccessible for
unprivileged users. Using `readableAll` and `writableAll` will make the server
accessible for all users.
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { constants } = internalBinding('tcp_wrap');

module.exports = RoundRobinHandle;

function RoundRobinHandle(key, address, { port, fd, flags }) {
function RoundRobinHandle(key, address, { port, fd, flags, backlog }) {
this.key = key;
this.all = new Map();
this.free = new Map();
Expand All @@ -22,16 +22,17 @@ function RoundRobinHandle(key, address, { port, fd, flags }) {
this.server = net.createServer(assert.fail);

if (fd >= 0)
this.server.listen({ fd });
this.server.listen({ fd, backlog });
else if (port >= 0) {
this.server.listen({
port,
host: address,
// Currently, net module only supports `ipv6Only` option in `flags`.
ipv6Only: Boolean(flags & constants.UV_TCP_IPV6ONLY),
backlog,
});
} else
this.server.listen(address); // UNIX socket path.
this.server.listen(address, backlog); // UNIX socket path.

this.server.once('listening', () => {
this.handle = this.server._handle;
Expand Down
1 change: 1 addition & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ function listenInCluster(server, address, port, addressType,
addressType: addressType,
fd: fd,
flags,
backlog,
};

// Get the master's server handle, and listen on it
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-cluster-net-listen-backlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const common = require('../common');
const Countdown = require('../common/countdown');
const assert = require('assert');
// Monkey-patch `net.Server._listen2`
const net = require('net');
const cluster = require('cluster');

// Ensures that the `backlog` is used to create a `net.Server`.
const kExpectedBacklog = 127;
if (cluster.isMaster) {
let worker

const nativeListen = net.Server.prototype._listen2;
const countdown = new Countdown(2, () => {
worker.disconnect();
});

net.Server.prototype._listen2 = common.mustCall(
function(address, port, addressType, backlog, fd, flags) {
assert(backlog, kExpectedBacklog);
countdown.dec();
return nativeListen.call(this, address, port, addressType, backlog, fd, flags);
}
);

worker = cluster.fork();
worker.on('message', () => {
countdown.dec();
});
} else {
const server = net.createServer()

server.listen({
host: common.localhostIPv4,
port: 0,
backlog: kExpectedBacklog,
}, common.mustCall(() => {
process.send(true);
}));
}

0 comments on commit 5e339ec

Please sign in to comment.