GeezerVison is showing github.com — cluster: respect listen backlog set by workers · nodejs/node@c41e402
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/net.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -464,6 +464,10 @@ server.listen({ | 464 | 464 | }); | 465 | 465 | ``` | 466 | 466 |
| | 467 | +When `exclusive` is `true` and the underlying handle is shared, it is | | 468 | +possible that several workers query a handle with different backlogs. | | 469 | +In this case, the first `backlog` passed to the master process will be used. | | 470 | + | 467 | 471 | Starting an IPC server as root may cause the server path to be inaccessible for | 468 | 472 | unprivileged users. Using `readableAll` and `writableAll` will make the server | 469 | 473 | accessible for all users. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/round_robin_handle.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,7 @@ const { constants } = internalBinding('tcp_wrap'); | 15 | 15 |
| 16 | 16 | module.exports = RoundRobinHandle; | 17 | 17 |
| 18 | | -function RoundRobinHandle(key, address, { port, fd, flags }) { | | 18 | +function RoundRobinHandle(key, address, { port, fd, flags, backlog }) { | 19 | 19 | this.key = key; | 20 | 20 | this.all = new SafeMap(); | 21 | 21 | this.free = new SafeMap(); | @@ -24,16 +24,17 @@ function RoundRobinHandle(key, address, { port, fd, flags }) { | 24 | 24 | this.server = net.createServer(assert.fail); | 25 | 25 |
| 26 | 26 | if (fd >= 0) | 27 | | - this.server.listen({ fd }); | | 27 | + this.server.listen({ fd, backlog }); | 28 | 28 | else if (port >= 0) { | 29 | 29 | this.server.listen({ | 30 | 30 | port, | 31 | 31 | host: address, | 32 | 32 | // Currently, net module only supports `ipv6Only` option in `flags`. | 33 | 33 | ipv6Only: Boolean(flags & constants.UV_TCP_IPV6ONLY), | | 34 | + backlog, | 34 | 35 | }); | 35 | 36 | } else | 36 | | - this.server.listen(address); // UNIX socket path. | | 37 | + this.server.listen(address, backlog); // UNIX socket path. | 37 | 38 |
| 38 | 39 | this.server.once('listening', () => { | 39 | 40 | this.handle = this.server._handle; | |
Collapse file Copy file name to clipboardExpand all lines: lib/net.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1386,6 +1386,7 @@ function listenInCluster(server, address, port, addressType, | 1386 | 1386 | addressType: addressType, | 1387 | 1387 | fd: fd, | 1388 | 1388 | flags, | | 1389 | + backlog, | 1389 | 1390 | }; | 1390 | 1391 |
| 1391 | 1392 | // Get the primary's server handle, and listen on it | |
Collapse file Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | +// Monkey-patch `net.Server.listen` | | 6 | +const net = require('net'); | | 7 | +const cluster = require('cluster'); | | 8 | + | | 9 | +// Force round-robin scheduling policy | | 10 | +// as Windows defaults to SCHED_NONE | | 11 | +// https://nodejs.org/docs/latest/api/cluster.html#clusterschedulingpolicy | | 12 | +cluster.schedulingPolicy = cluster.SCHED_RR; | | 13 | + | | 14 | +// Ensures that the `backlog` is used to create a `net.Server`. | | 15 | +const kExpectedBacklog = 127; | | 16 | +if (cluster.isMaster) { | | 17 | + const listen = net.Server.prototype.listen; | | 18 | + | | 19 | + net.Server.prototype.listen = common.mustCall( | | 20 | + function(...args) { | | 21 | + const options = args[0]; | | 22 | + if (typeof options === 'object') { | | 23 | + assert(options.backlog, kExpectedBacklog); | | 24 | + } else { | | 25 | + assert(args[1], kExpectedBacklog); | | 26 | + } | | 27 | + return listen.call(this, ...args); | | 28 | + } | | 29 | + ); | | 30 | + | | 31 | + const worker = cluster.fork(); | | 32 | + worker.on('message', () => { | | 33 | + worker.disconnect(); | | 34 | + }); | | 35 | +} else { | | 36 | + const server = net.createServer(); | | 37 | + | | 38 | + server.listen({ | | 39 | + host: common.localhostIPv4, | | 40 | + port: 0, | | 41 | + backlog: kExpectedBacklog, | | 42 | + }, common.mustCall(() => { | | 43 | + process.send(true); | | 44 | + })); | | 45 | +} |
|