-
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.
fix(cluster): respect the backlog from workers
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
Showing
4 changed files
with
51 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,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); | ||
})); | ||
} |