Allow multiple Node.js processes to listen on same port, with SO_REUSEPORT
.
(With this library, SO_REUSEPORT
will be setsockopt
-ed on all TCP sockets, before bind
call).
Only Linux is supported.
The npm package contains prebuilt binaries for linux-x64
linux-arm64
linux-armv7
.
If your platform is not listed above, SO_REUSEPORT
will not be set. Feel free to create an issue if other Linux variant is required.
I have no plan to support other OSes. PRs welcome.
For BSD / MacOS a similar trick should be doable, like what they did in wolfcw/libfaketime.
npm install --save publicaddr
# OR
yarn add publicaddr
Prepend publicaddr
wrapper to your node
binary (or other interpreter like ts-node
). Example:
// package.json
"scripts": {
"start": "publicaddr node demo-server.js"
},
And prebuild/prebuildify prebuild/prebuildify-cross prebuild/node-gyp-build, they made shipping multiarch prebuilt native modules incredibly simple.
Clone this repo and run multiple instances like:
git clone https://github.com/jokester/publicaddr
cd publicaddr/demo
# run version a with 4 containers
docker-compose up -d
# start a HTTP benchmark
wrk -t6 -c2000 -d120s http://127.0.0.1:3000 &
# switch between version a / b, for a few times
./switch-version.sh a b a b
# after benchmark ends
docker-compose down
BSD