Skip to content
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

使用 SO_REUSEPORT 选项实现 worker_threads 模式多个 worker 监听同一个端口 #5231

Open
shizhx opened this issue Jun 28, 2023 · 2 comments

Comments

@shizhx
Copy link

shizhx commented Jun 28, 2023

请详细告知你的新点子(Nice Ideas):

背景

最近的 egg.js 终于支持了 worker_threads 模式,但是相较于 process 模式,该模式在启动多个 app_worker 时需要为每个 app_worker 指定一个监听端口,这导致使用者必须配置 nginx 之类的前置代理,行为也和 process 模式不一样。

想法

Linux 3.9 后可以通过 SO_REUSEPORT 选项让多个进程或线程监听同一个端口,并且自动负载均衡;因此,可以考虑:

  1. 让 Linux 平台使用 SO_REUSEPORT 实现类似 process 模式的效果。
  2. 通过一个命令行参数启用这个功能

示例代码

这里通过字节的 node-unix-socket 模块创建一个 SO_REUSEPORTfd

// worker.js
const http = require('node:http');
const { id } = require('node:worker_threads').workerData;
const { createReuseportFd } = require('node-unix-socket');

const server = http.createServer((req, res) => {
    res.end(`id=${id}`);
});

const fd = createReuseportFd(8000, '127.0.0.1');
server.listen({fd}, () => {
    console.log(`worker#${id} listening on 8000`);
});
@fengmk2
Copy link
Member

fengmk2 commented Jun 28, 2023

@shizhx 非常好的特性,来个 pr 贡献实现一下?

@fengmk2
Copy link
Member

fengmk2 commented Jun 28, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants