-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster: respect listen backlog set by workers
PR-URL: #41623 Co-authored-by: Ouyang Yadong <[email protected]> Reviewed-By: Ouyang Yadong <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- Loading branch information
Showing
4 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
// Monkey-patch `net.Server.listen` | ||
const net = require('net'); | ||
const cluster = require('cluster'); | ||
|
||
// Force round-robin scheduling policy | ||
// as Windows defaults to SCHED_NONE | ||
// https://nodejs.org/docs/latest/api/cluster.html#clusterschedulingpolicy | ||
cluster.schedulingPolicy = cluster.SCHED_RR; | ||
|
||
// Ensures that the `backlog` is used to create a `net.Server`. | ||
const kExpectedBacklog = 127; | ||
if (cluster.isMaster) { | ||
const listen = net.Server.prototype.listen; | ||
|
||
net.Server.prototype.listen = common.mustCall( | ||
function(...args) { | ||
const options = args[0]; | ||
if (typeof options === 'object') { | ||
assert(options.backlog, kExpectedBacklog); | ||
} else { | ||
assert(args[1], kExpectedBacklog); | ||
} | ||
return listen.call(this, ...args); | ||
} | ||
); | ||
|
||
const worker = cluster.fork(); | ||
worker.on('message', () => { | ||
worker.disconnect(); | ||
}); | ||
} else { | ||
const server = net.createServer(); | ||
|
||
server.listen({ | ||
host: common.localhostIPv4, | ||
port: 0, | ||
backlog: kExpectedBacklog, | ||
}, common.mustCall(() => { | ||
process.send(true); | ||
})); | ||
} |