-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #55408 Refs: libuv/libuv#4407 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
- Loading branch information
Showing
7 changed files
with
156 additions
and
9 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,23 @@ | ||
'use strict'; | ||
const net = require('net'); | ||
|
||
const options = { port: 0, reusePort: true }; | ||
|
||
function checkSupportReusePort() { | ||
return new Promise((resolve, reject) => { | ||
const server = net.createServer().listen(options); | ||
server.on('listening', () => { | ||
server.close(resolve); | ||
}); | ||
server.on('error', (err) => { | ||
console.log('The `reusePort` option is not supported:', err.message); | ||
server.close(); | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
checkSupportReusePort, | ||
options, | ||
}; |
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,35 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const { checkSupportReusePort, options } = require('../common/net'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const net = require('net'); | ||
|
||
if (!process.env.isWorker) { | ||
checkSupportReusePort().then(() => { | ||
const server = net.createServer(); | ||
server.listen(options, common.mustCall(() => { | ||
const port = server.address().port; | ||
const workerOptions = { env: { ...process.env, isWorker: 1, port } }; | ||
let count = 2; | ||
for (let i = 0; i < 2; i++) { | ||
const worker = child_process.fork(__filename, workerOptions); | ||
worker.on('exit', common.mustCall((code) => { | ||
assert.strictEqual(code, 0); | ||
if (--count === 0) { | ||
server.close(); | ||
} | ||
})); | ||
} | ||
})); | ||
}, () => { | ||
common.skip('The `reusePort` option is not supported'); | ||
}); | ||
return; | ||
} | ||
|
||
const server = net.createServer(); | ||
|
||
server.listen({ ...options, port: +process.env.port }, common.mustCall(() => { | ||
server.close(); | ||
})).on('error', common.mustNotCall()); |
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,41 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const { checkSupportReusePort, options } = require('../common/net'); | ||
const assert = require('assert'); | ||
const cluster = require('cluster'); | ||
const net = require('net'); | ||
|
||
if (cluster.isPrimary) { | ||
checkSupportReusePort().then(() => { | ||
cluster.fork().on('exit', common.mustCall((code) => { | ||
assert.strictEqual(code, 0); | ||
})); | ||
}, () => { | ||
common.skip('The `reusePort` option is not supported'); | ||
}); | ||
return; | ||
} | ||
|
||
let waiting = 2; | ||
function close() { | ||
if (--waiting === 0) | ||
cluster.worker.disconnect(); | ||
} | ||
|
||
const server1 = net.createServer(); | ||
const server2 = net.createServer(); | ||
|
||
// Test if the worker requests the main process to create a socket | ||
cluster._getServer = common.mustNotCall(); | ||
|
||
server1.listen(options, common.mustCall(() => { | ||
const port = server1.address().port; | ||
server2.listen({ ...options, port }, common.mustCall(() => { | ||
server1.close(close); | ||
server2.close(close); | ||
})); | ||
})); | ||
|
||
server1.on('error', common.mustNotCall()); | ||
server2.on('error', common.mustNotCall()); |
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,26 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const { checkSupportReusePort, options } = require('../common/net'); | ||
const net = require('net'); | ||
|
||
function test(host) { | ||
const server1 = net.createServer(); | ||
const server2 = net.createServer(); | ||
server1.listen({ ...options, host }, common.mustCall(() => { | ||
const port = server1.address().port; | ||
server2.listen({ ...options, host, port }, common.mustCall(() => { | ||
server1.close(); | ||
server2.close(); | ||
})); | ||
})); | ||
server1.on('error', common.mustNotCall()); | ||
server2.on('error', common.mustNotCall()); | ||
} | ||
|
||
checkSupportReusePort() | ||
.then(() => { | ||
test('127.0.0.1'); | ||
common.hasIPv6 && test('::'); | ||
}, () => { | ||
common.skip('The `reusePort` option is not supported'); | ||
}); |