Skip to content

Allow multiple node.js processes to listen to same TCP port (via SO_REUSEPORT)

License

Notifications You must be signed in to change notification settings

jokester/publicaddr

Repository files navigation

publicaddr

npm version

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).

Supported OS

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.

Installation

npm install --save publicaddr
# OR
yarn add publicaddr

Usage

Prepend publicaddr wrapper to your node binary (or other interpreter like ts-node). Example:

  // package.json
  "scripts": {
    "start": "publicaddr node demo-server.js"
  },

Refs

And prebuild/prebuildify prebuild/prebuildify-cross prebuild/node-gyp-build, they made shipping multiarch prebuilt native modules incredibly simple.

Demo: Rolling update between 2 versions without downtime

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

License

BSD