Эх сурвалжийг харах

[Bun] use Bun.spawn to increase performance (#8454)

* [Bun] use Bun.spawn to increase performance

* [Bun] Simple way to get pathname
Imam Fahrur Rofi 1 жил өмнө
parent
commit
9c5800eaa7

+ 1 - 1
frameworks/TypeScript/bun/bun.dockerfile

@@ -10,4 +10,4 @@ COPY ./src .
 
 ENV NODE_ENV=production
 
-CMD ["bun", "index.ts"]
+CMD ["bun", "spawn.ts"]

+ 2 - 1
frameworks/TypeScript/bun/src/index.ts

@@ -3,8 +3,9 @@ const options: ResponseInit = { headers: { "Server": "Bun" } };
 
 const server = Bun.serve({
   port: 8080,
+  reusePort: true,
   fetch(req: Request) {
-    const { pathname } = new URL(req.url);
+    const pathname = req.url.slice(req.url.indexOf("/", 8));
 
     if (pathname === "/json") {
       return Response.json({ message: HELLO_WORLD_STR }, options);

+ 9 - 0
frameworks/TypeScript/bun/src/spawn.ts

@@ -0,0 +1,9 @@
+import os from "node:os";
+
+const numCPUs = os.cpus().length;
+for (let i = 0; i < numCPUs; i++) {
+  Bun.spawn(["bun", "index.ts"], {
+    stdio: ["inherit", "inherit", "inherit"],
+    env: { ...process.env },
+  });
+}