Skip to content

Commit

Permalink
feat: support reusePort on server listen
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 26, 2024
1 parent d892960 commit 3d8d116
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 40 deletions.
24 changes: 0 additions & 24 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '14, 16, 18, 20, 22'
version: '14, 16, 18, 20, 22, 23'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ EGG_AGENT_CLOSE_TIMEOUT: agent worker boot timeout value

[MIT](LICENSE)

<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/6897780?v=4" width="100px;"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/5243774?v=4" width="100px;"/><br/><sub><b>ngot</b></sub>](https://github.com/ngot)<br/>|[<img src="https://avatars.githubusercontent.com/u/19908330?v=4" width="100px;"/><br/><sub><b>hyj1991</b></sub>](https://github.com/hyj1991)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/2170848?v=4" width="100px;"/><br/><sub><b>iyuq</b></sub>](https://github.com/iyuq)<br/>|[<img src="https://avatars.githubusercontent.com/u/2972143?v=4" width="100px;"/><br/><sub><b>nightink</b></sub>](https://github.com/nightink)<br/>|[<img src="https://avatars.githubusercontent.com/u/2160731?v=4" width="100px;"/><br/><sub><b>mansonchor</b></sub>](https://github.com/mansonchor)<br/>|
|[<img src="https://avatars.githubusercontent.com/u/10825163?v=4" width="100px;"/><br/><sub><b>ImHype</b></sub>](https://github.com/ImHype)<br/>|[<img src="https://avatars.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub>](https://github.com/gxcsoccer)<br/>|[<img src="https://avatars.githubusercontent.com/u/1763067?v=4" width="100px;"/><br/><sub><b>waitingsong</b></sub>](https://github.com/waitingsong)<br/>|[<img src="https://avatars.githubusercontent.com/u/7581901?v=4" width="100px;"/><br/><sub><b>sjfkai</b></sub>](https://github.com/sjfkai)<br/>|[<img src="https://avatars.githubusercontent.com/u/26563778?v=4" width="100px;"/><br/><sub><b>ahungrynoob</b></sub>](https://github.com/ahungrynoob)<br/>|[<img src="https://avatars.githubusercontent.com/u/3230673?v=4" width="100px;"/><br/><sub><b>qingdengyue</b></sub>](https://github.com/qingdengyue)<br/>|
[<img src="https://avatars.githubusercontent.com/u/16320597?v=4" width="100px;"/><br/><sub><b>wenjiasen</b></sub>](https://github.com/wenjiasen)<br/>|[<img src="https://avatars.githubusercontent.com/u/418820?v=4" width="100px;"/><br/><sub><b>czy88840616</b></sub>](https://github.com/czy88840616)<br/>|[<img src="https://avatars.githubusercontent.com/u/9213756?v=4" width="100px;"/><br/><sub><b>gxkl</b></sub>](https://github.com/gxkl)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Mon Jun 03 2024 10:59:15 GMT+0800`.

<!-- GITCONTRIBUTOR_END -->
[![contributors](https://contrib.rocks/image?repo=eggjs/egg-cluster&max=240&columns=26)](https://github.com/eggjs/egg-cluster/graphs/contributors)
20 changes: 16 additions & 4 deletions lib/app_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const clusterConfig = app.config.cluster || /* istanbul ignore next */ {};
const listenConfig = clusterConfig.listen || /* istanbul ignore next */ {};
const httpsOptions = Object.assign({}, clusterConfig.https, options.https);
const port = options.port = options.port || listenConfig.port;
const reusePort = options.reusePort = options.reusePort || listenConfig.reusePort;
const debugPort = options.debugPort;
const protocol = (httpsOptions.key && httpsOptions.cert) ? 'https' : 'http';

Expand Down Expand Up @@ -121,10 +122,21 @@ function startServer(err) {
exitProcess();
return;
}
const args = [ port ];
if (listenConfig.hostname) args.push(listenConfig.hostname);
debug('listen options %s', args);
server.listen(...args);
if (reusePort) {
const listenOptions = { port, reusePort };
if (listenConfig.hostname) {
listenOptions.host = listenConfig.hostname;
}

Check warning on line 129 in lib/app_worker.js

View check run for this annotation

Codecov / codecov/patch

lib/app_worker.js#L128-L129

Added lines #L128 - L129 were not covered by tests
debug('listen options %s', listenOptions);
server.listen(listenOptions);
} else {
const args = [ port ];
if (listenConfig.hostname) {
args.push(listenConfig.hostname);
}
debug('listen options %s', args);
server.listen(...args);
}
}
if (debugPortServer) {
debug('listen on debug port: %s', debugPort);
Expand Down
1 change: 1 addition & 0 deletions lib/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Master extends EventEmitter {
* - {Object} [plugins] - customized plugins, for unittest
* - {Number} [workers] numbers of app workers, default to `os.cpus().length`
* - {Number} [port] listening port, default to 7001(http) or 8443(https)
* - {Boolean} [reusePort] setting `reusePort` to `true` allows multiple sockets on the same host to bind to the same port. Incoming connections are distributed by the operating system to listening sockets. This option is available only on some platforms, such as Linux 3.9+, DragonFlyBSD 3.6+, FreeBSD 12.0+, Solaris 11.4, and AIX 7.2.5+. **Default:** `false`.
* - {Number} [debugPort] listening a debug port on http protocol
* - {Object} [https] https options, { key, cert, ca }, full path
* - {Array|String} [require] will inject into worker/agent process
Expand Down
1 change: 1 addition & 0 deletions lib/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function(options) {
framework: '',
baseDir: process.cwd(),
port: options.https ? 8443 : null,
reusePort: false,
workers: null,
plugins: null,
https: false,
Expand Down
29 changes: 29 additions & 0 deletions test/app_worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,35 @@ describe('test/app_worker.test.js', () => {
.expect(200);
});

it('should set reusePort=true in config', async () => {
app = utils.cluster('apps/app-listen-reusePort');
// app.debug();
await app.ready();

app.expect('code', 0);
app.expect('stdout', /egg started on http:\/\/127.0.0.1:17010/);

await request('http://0.0.0.0:17010')
.get('/')
.expect('done')
.expect(200);

await request('http://127.0.0.1:17010')
.get('/')
.expect('done')
.expect(200);

await request('http://localhost:17010')
.get('/')
.expect('done')
.expect(200);

await request('http://127.0.0.1:17010')
.get('/port')
.expect('17010')
.expect(200);
});

it('should use hostname in config', async () => {
const url = address.ip() + ':17010';

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/apps/app-listen-reusePort/app.js
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;
};
9 changes: 9 additions & 0 deletions test/fixtures/apps/app-listen-reusePort/app/router.js
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 test/fixtures/apps/app-listen-reusePort/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
keys: '123',
cluster: {
listen: {
port: 17010,
reusePort: true,
},
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-listen-reusePort/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "app-listen-reusePort"
}
11 changes: 11 additions & 0 deletions test/master.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('test/master.test.js', () => {
.end(done);
});

it('start success with reusePort=true', done => {
mm.env('local');
app = utils.cluster('apps/master-worker-started', { reusePort: true });

app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.notExpect('stdout', /\[master\] agent_worker#1:\d+ start with clusterPort:\d+/)
.expect('code', 0)
.end(done);
});

it('start success in prod env', done => {
mm.env('prod');
app = utils.cluster('apps/mock-production-app').debug(false);
Expand Down

0 comments on commit 3d8d116

Please sign in to comment.