egg icon indicating copy to clipboard operation
egg copied to clipboard

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

Open shizhx opened this issue 2 years ago • 2 comments

请详细告知你的新点子(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`);
});

shizhx avatar Jun 28 '23 08:06 shizhx

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

fengmk2 avatar Jun 28 '23 09:06 fengmk2

https://github.com/bytedance/node-unix-socket

fengmk2 avatar Jun 28 '23 09:06 fengmk2