Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Jan 10, 2024
1 parent aa63f32 commit 573a9f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ Prepend `publicaddr` wrapper to your `node` binary (or other interpreter like `t

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

### Simple usage
## Demo: Rolling update between 2 versions without downtime

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

Expand All @@ -55,22 +53,19 @@ git clone https://github.com/jokester/publicaddr

cd publicaddr/demo

npm install
# run version a with 4 containers
docker-compose up -d

npm start &
npm start &
npm start &
npm start &
# start a HTTP benchmark
wrk -t6 -c2000 -d120s http://127.0.0.1:3000 &

wrk -t6 -c2000 -d10s http://127.0.0.1:3000 # or other HTTP benchmark tool
# switch between version a / b, for a few times
./switch-version.sh a b a b

kill %1 %2 %3 %4
# after benchmark ends
docker-compose down
```

### Inside containers

Clone [this repo](https://github.com/jokester/publicaddr) and run `docker-compose up` in `demo/`

## License

BSD
Expand Down
29 changes: 22 additions & 7 deletions demo/demo-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,34 @@ const waitRandom = (min, max) => wait(min + Math.random() * (max - min))
const processTag = `demo-server-version-${process.env.VERSION ?? '?'}-${Math.random().toString(16).slice(2, 10)}`

let reqCount = 0
let onFlight = 0

const server = http.createServer(async (req, res) => {
res.writeHead(200);
++onFlight;
res.writeHead(200, undefined, { connection: 'Close' });
await waitRandom(20, 100);
res.end(`reqCount: ${++reqCount} from ${processTag}`)
--onFlight;
})

function log(...args) {
console.info(`${Date.now().toFixed(2)} ${processTag}:`, ...args)
}

async function waitRequestsEnd() {
while (onFlight > 0) {
await wait(1e3)
}
}

function startReportCount() {
let lastReqCountReported = 0
const interval = 10 // s
const timer = setInterval(() => {
const delta = reqCount - lastReqCountReported
lastReqCountReported = reqCount
console.info(`PID ${process.pid} / ${processTag}: reqCount: ${reqCount} / delta: ${delta} in last ${interval}s`)
log(`reqCount: ${reqCount}`);
log(`mean RPS: ${(delta / interval).toFixed(2)}`);
}, interval * 1e3)
return () => clearInterval(timer)
}
Expand All @@ -32,14 +46,15 @@ async function main() {
const stopReportCount = startReportCount()

server.listen(port, () => {
console.info(`PID ${process.pid} / ${processTag}: listening on port ${port}`)
log(`listening on port ${port}`)
})
await gotSignal
await new Promise(f => server.close(f))
const error = await new Promise(f => server.close(f))
if (error) {
console.error('server close error:', error)
}
stopReportCount()
console.info(`PID ${process.pid} / ${processTag}: app closing. pretend to be cleaning up...`)
await wait(10e3);
console.info(`PID ${process.pid} / ${processTag}: app closed.`)
log(`app closing.`)
}

await main();
2 changes: 1 addition & 1 deletion demo/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
environment:
VERSION: 'a'
deploy:
replicas: ${VERSION_A_REPLICA:-0}
replicas: ${VERSION_A_REPLICA:-4}

demo-server-b: &demo-server
build: .
Expand Down

0 comments on commit 573a9f9

Please sign in to comment.