-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,43 @@ | ||
import cluster from 'node:cluster' | ||
import { cpus } from 'node:os' | ||
import worker_threads from "node:worker_threads"; | ||
Check failure on line 1 in benchmarks/websocket/server/echo.mjs GitHub Actions / Lint
Check failure on line 1 in benchmarks/websocket/server/echo.mjs GitHub Actions / Lint
|
||
import { cpus } from "node:os"; | ||
Check failure on line 2 in benchmarks/websocket/server/echo.mjs GitHub Actions / Lint
|
||
import url from "node:url"; | ||
Check failure on line 3 in benchmarks/websocket/server/echo.mjs GitHub Actions / Lint
|
||
import path from "node:path"; | ||
Check failure on line 4 in benchmarks/websocket/server/echo.mjs GitHub Actions / Lint
|
||
import uws from "uWebSockets.js"; | ||
|
||
if (cluster.isPrimary) { | ||
let cpu = cpus().length | ||
while (cpu-- > 0) { | ||
cluster.fork() | ||
var __filename = url.fileURLToPath(import.meta.url); | ||
var __dirname = path.dirname(__filename); | ||
|
||
if (worker_threads.isMainThread) { | ||
for (let i = cpus().length - 1; i >= 0; --i) { | ||
new worker_threads.Worker(__filename); | ||
} | ||
} else { | ||
// use uws? | ||
const app = uws.App(); | ||
|
||
app | ||
.ws("/*", { | ||
compression: uws.DISABLED, | ||
maxPayloadLength: 512 * 1024 * 1024, | ||
maxBackpressure: 128 * 1024, | ||
idleTimeout: 60, | ||
message: (ws, message, isBinary) => { | ||
/* Here we echo the message back, using compression if available */ | ||
const ok = ws.send(message, isBinary); | ||
}, | ||
}) | ||
.get("/*", (res, req) => { | ||
/* It does Http as well */ | ||
res | ||
.writeStatus("200 OK") | ||
.end("Hello there!"); | ||
}) | ||
.listen(5001, (listenSocket) => { | ||
if (listenSocket) { | ||
if (worker_threads.threadId === 0) { | ||
console.log("Listening to port 5001"); | ||
} else { | ||
console.log(`Listening to port 5001 from thread ${worker_threads.threadId}`) | ||
} | ||
} | ||
}); | ||
} |