From 74600ffc7c7d7d8d7d2b52b255b107fd28c0eb22 Mon Sep 17 00:00:00 2001 From: Tsuyoshi-Ishikawa Date: Wed, 15 Mar 2023 22:27:18 +0900 Subject: [PATCH 1/2] enable to set worker_count to use multi_processing --- README.md | 10 +++++++++- src/main.ts | 24 +++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b0e48c..ef6d3d4 100644 --- a/README.md +++ b/README.md @@ -275,4 +275,12 @@ The default body parse limit is 512kb. ```bash MAX_BODY_BYTE_SIZE=1048576 # 1048576 byte is 1MB. -``` \ No newline at end of file +``` + +## Multi Processing +If you want to build proxy in multi-process, set worker count to environment variables. +The default worker count is 1. + +```bash +CLUSTER_PROCESS=4 +``` diff --git a/src/main.ts b/src/main.ts index fbfb42d..9d7429e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,13 @@ import { NestFactory } from '@nestjs/core'; import { ValidationPipe } from '@nestjs/common'; import { AppModule } from 'src/app.module'; import { json } from 'body-parser'; +import * as _cluster from 'cluster'; +import { cpus } from 'os'; + +const cluster = _cluster as unknown as _cluster.Cluster; +const workerCount = process.env.CLUSTER_PROCESS + ? parseInt(process.env.CLUSTER_PROCESS, 10) + : 1; async function bootstrap() { const app = await NestFactory.create(AppModule); @@ -20,4 +27,19 @@ async function bootstrap() { }); await app.listen(process.env.PORT ? parseInt(process.env.PORT, 10) : 3000); } -bootstrap(); + +if (cluster.isPrimary) { + const workerLimit = cpus().length; + if (workerCount > workerLimit) + throw new Error( + `cluster process limit is ${workerLimit}. CLUSTER_PROCESS is over ${workerLimit}`, + ); + + for (let i = 0; i < workerCount; i++) { + console.log('cluster fork :', i); + cluster.fork(); + } +} else { + console.log('worker pid is', cluster.worker?.process.pid); + bootstrap(); +} From 9861c60e797da009488b3e3225c441412d5d230b Mon Sep 17 00:00:00 2001 From: Tsuyoshi-Ishikawa Date: Thu, 16 Mar 2023 12:24:26 +0900 Subject: [PATCH 2/2] restart when worker died --- src/main.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.ts b/src/main.ts index 9d7429e..364db82 100644 --- a/src/main.ts +++ b/src/main.ts @@ -39,6 +39,13 @@ if (cluster.isPrimary) { console.log('cluster fork :', i); cluster.fork(); } + + cluster.on('exit', (worker, code, signal) => { + console.log( + `Worker ${worker.process.pid} died with code(${code}) and signal(${signal}). Restarting...`, + ); + cluster.fork(); + }); } else { console.log('worker pid is', cluster.worker?.process.pid); bootstrap();