-
Notifications
You must be signed in to change notification settings - Fork 53
feat: support reusePort on server listen #115
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
Merged
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3d8d116
feat: support reusePort on server listen
fengmk2 dd45194
f
fengmk2 cb897b6
f
fengmk2 eadbdc9
Merge branch '2.x' into reuse-port
fengmk2 4a526b2
Merge branch '2.x' into reuse-port
fengmk2 0153ec5
Update test/app_worker.test.js
fengmk2 efdc99b
Update test/fixtures/apps/app-listen-reusePort/config/config.default.js
fengmk2 8aea270
support reuse port on worker_threads
fengmk2 d1495cc
FIXUP
fengmk2 60f08cd
FIXUP
fengmk2 16b546a
FIXUP
fengmk2 9bd82df
FIXUP
fengmk2 d1c0d2d
Update lib/app_worker.js
fengmk2 6ef351c
cleanup on worker thread exit
fengmk2 1c8b6fe
FIXUP
fengmk2 35bb21a
FIXUP
fengmk2 cd94e19
FIXUP
fengmk2 bd3c36a
FIXUP
fengmk2 43841b7
FIXUP
fengmk2 6490b75
Apply suggestions from code review
fengmk2 6789f2b
FIXUP
fengmk2 fe60af0
FIXUP
fengmk2 72133ca
Merge remote-tracking branch 'origin/reuse-port' into reuse-port
fengmk2 ca5d801
FIXUP
fengmk2 545a442
FIXUP
fengmk2 0da63a6
FIXUP
fengmk2 4a33697
FIXUP
fengmk2 144b559
FIXUP
fengmk2 cf121f1
FIXUP
fengmk2 2b065f9
FIXUP
fengmk2 063012c
FIXUP
fengmk2 9e99e6f
FIXUP
fengmk2 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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,6 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = app => { | ||
| // don't use the port that egg-mock defined | ||
| app._options.port = undefined; | ||
| }; |
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,9 @@ | ||
| module.exports = app => { | ||
| app.get('/', ctx => { | ||
| ctx.body = 'done'; | ||
| }); | ||
|
|
||
| app.get('/port', ctx => { | ||
| ctx.body = ctx.app._options.port; | ||
| }); | ||
| }; |
11 changes: 11 additions & 0 deletions
11
test/fixtures/apps/app-listen-reusePort/config/config.default.js
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,11 @@ | ||
| 'use strict'; | ||
|
|
||
fengmk2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| module.exports = { | ||
| keys: '123', | ||
| cluster: { | ||
| listen: { | ||
| port: 17010, | ||
| reusePort: true, | ||
| }, | ||
| }, | ||
| }; | ||
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,3 @@ | ||
| { | ||
| "name": "app-listen-reusePort" | ||
| } |
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,70 @@ | ||
| const cluster = require('node:cluster'); | ||
| const http = require('node:http'); | ||
| const numCPUs = require('node:os').availableParallelism(); | ||
| const process = require('node:process'); | ||
|
|
||
| function request(index) { | ||
| http.get('http://localhost:17001/', res => { | ||
| const { statusCode } = res; | ||
| console.log(index, res.statusCode, res.headers); | ||
| let error; | ||
| // Any 2xx status code signals a successful response but | ||
| // here we're only checking for 200. | ||
| if (statusCode !== 200) { | ||
| error = new Error('Request Failed.\n' + | ||
| `Status Code: ${statusCode}`); | ||
| } | ||
| if (error) { | ||
| console.error(error.message); | ||
| // Consume response data to free up memory | ||
| res.resume(); | ||
| return; | ||
| } | ||
| res.setEncoding('utf8'); | ||
| let rawData = ''; | ||
| res.on('data', chunk => { rawData += chunk; }); | ||
| res.on('end', () => { | ||
| try { | ||
| console.log(rawData); | ||
| } catch (e) { | ||
| console.error(e.message); | ||
| } | ||
| }); | ||
| }).on('error', e => { | ||
| console.error(`Got error: ${e.stack}`); | ||
| }); | ||
| } | ||
fengmk2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (cluster.isPrimary) { | ||
| console.log(`Primary ${process.pid} is running`); | ||
|
|
||
| // Fork workers. | ||
| for (let i = 0; i < numCPUs; i++) { | ||
| cluster.fork(); | ||
| } | ||
|
|
||
| cluster.on('exit', (worker, code, signal) => { | ||
| console.log(`worker ${worker.process.pid} died, code: ${code}, signal: ${signal}`); | ||
| }); | ||
|
|
||
| setTimeout(() => { | ||
| for (let i = 0; i < 20; i++) { | ||
| request(i); | ||
| } | ||
| }, 2000); | ||
| setTimeout(() => { | ||
| process.exit(0); | ||
| }, 5000); | ||
| } else { | ||
fengmk2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Workers can share any TCP connection | ||
| // In this case it is an HTTP server | ||
| http.createServer((req, res) => { | ||
| res.writeHead(200); | ||
| res.end('hello world\n'); | ||
| }).listen({ | ||
| port: 17001, | ||
| reusePort: true, | ||
| }); | ||
|
|
||
| console.log(`Worker ${process.pid} started`); | ||
| } | ||
fengmk2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.