Skip to content

Commit

Permalink
Bun cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Jun 20, 2024
1 parent 8af90ad commit 75ab697
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"main": "src/server.ts",
"type": "module",
"scripts": {
"start": "bun run src/server.ts",
"start": "bun run src/cluster.ts",
"lint": "eslint ./src/**/*.ts --color --cache --cache-strategy content --cache-location .eslintcache",
"typecheck": "tsc"
},
Expand All @@ -22,7 +22,7 @@
},
"devDependencies": {
"@eslint/js": "9.5.0",
"@types/bun": "1.1.4",
"@types/bun": "1.1.5",
"eslint": "9.5.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
Expand Down
23 changes: 23 additions & 0 deletions server/src/cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isDeployed } from '@app/config/env';
import { spawn } from 'bun';

const cpus = isDeployed ? navigator.hardwareConcurrency : 1; // Number of CPU cores
const buns = new Array(cpus);

for (let i = 0; i < cpus; i++) {
buns[i] = spawn({
cmd: ['bun', './src/server.ts'],
stdout: 'inherit',
stderr: 'inherit',
stdin: 'inherit',
});
}

function kill() {
for (const bun of buns) {
bun.kill();
}
}

process.on('SIGINT', kill);
process.on('exit', kill);
2 changes: 2 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ log.info({ msg: `Server initialized on port ${serverConfig.port}` });
serve({
port: serverConfig.port,
fetch: app.fetch,
development: !isDeployed,
reusePort: isDeployed,
});

0 comments on commit 75ab697

Please sign in to comment.