|
@@ -1,10 +1,13 @@
|
|
|
import { createApp, type Response, type Request, type App, type NextFunction, type Handler } from "brahma-firelight";
|
|
import { createApp, type Response, type Request, type App, type NextFunction, type Handler } from "brahma-firelight";
|
|
|
|
|
+import cluster from "node:cluster";
|
|
|
|
|
+import os from "node:os";
|
|
|
|
|
|
|
|
const app: App = createApp();
|
|
const app: App = createApp();
|
|
|
|
|
|
|
|
// Server Config Middleware
|
|
// Server Config Middleware
|
|
|
const serverInfo: Handler = (_req: Request, res: Response, next?: NextFunction) => {
|
|
const serverInfo: Handler = (_req: Request, res: Response, next?: NextFunction) => {
|
|
|
res.setHeader("Server", "brahma-firelight");
|
|
res.setHeader("Server", "brahma-firelight");
|
|
|
|
|
+ res.setHeader("Connection", "keep-alive")
|
|
|
next?.();
|
|
next?.();
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -19,19 +22,39 @@ app.get("/plaintext", serverInfo, (_req: Request, res: Response) => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// Port & Host
|
|
// Port & Host
|
|
|
-
|
|
|
|
|
-app.listen("0.0.0.0", 8080);
|
|
|
|
|
-
|
|
|
|
|
-// Enable built in Graceful Shutdown (optional for production use)
|
|
|
|
|
-
|
|
|
|
|
-process.on('SIGINT', async () => {
|
|
|
|
|
- console.log('SIGINT → shutting down...');
|
|
|
|
|
- await app.close(2000); // wait up to 2s for requests
|
|
|
|
|
- process.exit(0);
|
|
|
|
|
-});
|
|
|
|
|
-
|
|
|
|
|
-process.on('SIGTERM', async () => {
|
|
|
|
|
- console.log('SIGTERM → shutting down...');
|
|
|
|
|
- await app.close(2000);
|
|
|
|
|
- process.exit(0);
|
|
|
|
|
-});
|
|
|
|
|
|
|
+const PORT = process.env.PORT || 8080;
|
|
|
|
|
+const HOST = process.env.HOST || "0.0.0.0";
|
|
|
|
|
+
|
|
|
|
|
+// Single CORE
|
|
|
|
|
+//app.listen(HOST, +PORT);
|
|
|
|
|
+
|
|
|
|
|
+// Multi CORE (cluster with max cpus available)
|
|
|
|
|
+if (cluster.isPrimary) {
|
|
|
|
|
+ const cpuCount = os.cpus().length;
|
|
|
|
|
+ console.log(`Primary ${process.pid} running → forking ${cpuCount} workers...`);
|
|
|
|
|
+
|
|
|
|
|
+ for (let i = 0; i < cpuCount; i++) {
|
|
|
|
|
+ cluster.fork();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ cluster.on("exit", (worker) => {
|
|
|
|
|
+ console.log(`Worker ${worker.process.pid} died → restarting...`);
|
|
|
|
|
+ cluster.fork();
|
|
|
|
|
+ });
|
|
|
|
|
+} else {
|
|
|
|
|
+ app.listen(HOST, +PORT);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// // Enable built in Graceful Shutdown (optional for production use)
|
|
|
|
|
+
|
|
|
|
|
+// process.on('SIGINT', async () => {
|
|
|
|
|
+// console.log('SIGINT → shutting down...');
|
|
|
|
|
+// await app.close(2000); // wait up to 2s for requests
|
|
|
|
|
+// process.exit(0);
|
|
|
|
|
+// });
|
|
|
|
|
+
|
|
|
|
|
+// process.on('SIGTERM', async () => {
|
|
|
|
|
+// console.log('SIGTERM → shutting down...');
|
|
|
|
|
+// await app.close(2000);
|
|
|
|
|
+// process.exit(0);
|
|
|
|
|
+// });
|