-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
net: add UV_TCP_REUSEPORT for tcp #55408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 1 commit into
nodejs:main
from
theanarkh:add_reuseport_for_tcp
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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'); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.