From 222767591e1a059fbc5327ee0d7fc8bb6b793398 Mon Sep 17 00:00:00 2001 From: Maddy Date: Tue, 12 Oct 2021 17:05:24 +1100 Subject: ...actually fix windows usernames breaking npm run setup ( I forgot to quote second argument :/ ) --- bundle/scripts/build.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bundle') diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index e8eb24b8..dfbaec15 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -36,8 +36,8 @@ console.log( execSync( "node \"" + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + - "\" -p " + - path.join(__dirname, ".."), + "\" -p \"" + + path.join(__dirname, "..") + "\"", { cwd: path.join(__dirname, ".."), shell: true, -- cgit 1.5.1 From 270ad5ee385a55f2160cbf76471aa8d4052c2a89 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 12 Oct 2021 21:52:35 +0200 Subject: :zap: benchmark tests --- bundle/scripts/benchmark/connections.js | 58 +++++++++++++++++++++++++++++++++ bundle/scripts/benchmark/index.js | 4 +++ bundle/scripts/benchmark/messages.js | 1 + 3 files changed, 63 insertions(+) create mode 100644 bundle/scripts/benchmark/connections.js create mode 100644 bundle/scripts/benchmark/index.js create mode 100644 bundle/scripts/benchmark/messages.js (limited to 'bundle') diff --git a/bundle/scripts/benchmark/connections.js b/bundle/scripts/benchmark/connections.js new file mode 100644 index 00000000..efc1bcb6 --- /dev/null +++ b/bundle/scripts/benchmark/connections.js @@ -0,0 +1,58 @@ +const cluster = require("cluster"); +const WebSocket = require("ws"); +const endpoint = process.env.GATEWAY || "ws://localhost:3001"; +const connections = Number(process.env.CONNECTIONS) || 50; +const threads = Number(process.env.THREADS) || require("os").cpus().length || 1; +const token = process.env.TOKEN; + +if (!token) { + console.error("TOKEN env var missing"); + process.exit(); +} + +if (cluster.isMaster) { + for (let i = 0; i < threads; i++) { + cluster.fork(); + } + + cluster.on("exit", (worker, code, signal) => { + console.log(`worker ${worker.process.pid} died`); + }); +} else { + for (let i = 0; i < connections; i++) { + connect(); + } +} + +function connect() { + const client = new WebSocket(endpoint); + client.on("message", (data) => { + data = JSON.parse(data); + + switch (data.op) { + case 10: + client.interval = setInterval(() => { + client.send(JSON.stringify({ op: 1 })); + }, data.d.heartbeat_interval); + + client.send( + JSON.stringify({ + op: 2, + d: { + token, + properties: {}, + }, + }) + ); + + break; + } + }); + client.once("close", (code, reason) => { + clearInterval(client.interval); + connect(); + }); + client.on("error", (err) => { + // console.log(err); + }); +} diff --git a/bundle/scripts/benchmark/index.js b/bundle/scripts/benchmark/index.js new file mode 100644 index 00000000..37ac5633 --- /dev/null +++ b/bundle/scripts/benchmark/index.js @@ -0,0 +1,4 @@ +require("dotenv").config(); + +require("./connections"); +require("./messages"); diff --git a/bundle/scripts/benchmark/messages.js b/bundle/scripts/benchmark/messages.js new file mode 100644 index 00000000..70b786d1 --- /dev/null +++ b/bundle/scripts/benchmark/messages.js @@ -0,0 +1 @@ +// TODO -- cgit 1.5.1 From b0385e0fcbf6ac1cae46979cc495081319fd4dec Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 12 Oct 2021 21:53:57 +0200 Subject: :zap: improve memory managment --- bundle/src/stats.ts | 3 ++- gateway/src/events/Close.ts | 9 ++++++--- gateway/src/util/Send.ts | 3 +++ util/src/util/Event.ts | 2 ++ 4 files changed, 13 insertions(+), 4 deletions(-) (limited to 'bundle') diff --git a/bundle/src/stats.ts b/bundle/src/stats.ts index 18bb85ca..7928de89 100644 --- a/bundle/src/stats.ts +++ b/bundle/src/stats.ts @@ -31,5 +31,6 @@ export function initStats() { process.memoryUsage().rss / 1024 / 1024 )}mb/${memory.totalMemMb.toFixed(0)}mb ${networkUsage}` ); - }, 1000 * 10); + // TODO: node-os-utils might have a memory leak, more investigation needed + }, 1000 * 60 * 5); } diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts index 1299ad5c..5c1bd292 100644 --- a/gateway/src/events/Close.ts +++ b/gateway/src/events/Close.ts @@ -1,10 +1,13 @@ import { WebSocket } from "@fosscord/gateway"; -import { Message } from "./Message"; import { Session } from "@fosscord/util"; export async function Close(this: WebSocket, code: number, reason: string) { console.log("[WebSocket] closed", code, reason); if (this.session_id) await Session.delete({ session_id: this.session_id }); - // @ts-ignore - this.off("message", Message); + if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout); + if (this.readyTimeout) clearTimeout(this.readyTimeout); + + this.deflate?.close(); + + this.removeAllListeners(); } diff --git a/gateway/src/util/Send.ts b/gateway/src/util/Send.ts index 4defa898..196d4205 100644 --- a/gateway/src/util/Send.ts +++ b/gateway/src/util/Send.ts @@ -18,6 +18,9 @@ export async function Send(socket: WebSocket, data: Payload) { } return new Promise((res, rej) => { + if (socket.readyState !== 1) { + return rej("socket not open"); + } socket.send(buffer, (err: any) => { if (err) return rej(err); return res(null); diff --git a/util/src/util/Event.ts b/util/src/util/Event.ts index bf9547b1..8ed009d5 100644 --- a/util/src/util/Event.ts +++ b/util/src/util/Event.ts @@ -46,7 +46,9 @@ export async function listenEvent(event: string, callback: (event: EventOpts) => } else { const cancel = () => { events.removeListener(event, callback); + events.setMaxListeners(events.getMaxListeners() - 1); }; + events.setMaxListeners(events.getMaxListeners() + 1); events.addListener(event, (opts) => callback({ ...opts, cancel })); return cancel; -- cgit 1.5.1