Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jokester/publicaddr

Allow multiple node.js processes to listen to same TCP port (via SO_REUSEPORT)
https://github.com/jokester/publicaddr

Last synced: 14 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# publicaddr

[![npm version](https://badge.fury.io/js/publicaddr.svg)](https://badge.fury.io/js/publicaddr)

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](https://github.com/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:

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

## Refs

- [What does SO_REUSEPORT do](https://stackoverflow.com/a/14388707)
- [How to Expose Multiple Containers On the Same Port](https://iximiuz.com/en/posts/multiple-containers-same-port-reverse-proxy/)

And [prebuild/prebuildify](https://github.com/prebuild/prebuildify) [prebuild/prebuildify-cross](https://github.com/prebuild/prebuildify-cross) [prebuild/node-gyp-build](https://github.com/prebuild/node-gyp-build/blob/master/node-gyp-build.js), they made shipping multiarch prebuilt native modules incredibly simple.

## Demo: Rolling update between 2 versions without downtime

Clone [this repo](https://github.com/jokester/publicaddr) and run multiple instances like:

```shell
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