Merge branch 'fosscord:master' into master
This commit is contained in:
commit
51274df43a
58
bundle/scripts/benchmark/connections.js
Normal file
58
bundle/scripts/benchmark/connections.js
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
}
|
4
bundle/scripts/benchmark/index.js
Normal file
4
bundle/scripts/benchmark/index.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
require("dotenv").config();
|
||||||
|
|
||||||
|
require("./connections");
|
||||||
|
require("./messages");
|
1
bundle/scripts/benchmark/messages.js
Normal file
1
bundle/scripts/benchmark/messages.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
// TODO
|
@ -36,8 +36,8 @@ console.log(
|
|||||||
execSync(
|
execSync(
|
||||||
"node \"" +
|
"node \"" +
|
||||||
path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") +
|
path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") +
|
||||||
"\" -p " +
|
"\" -p \"" +
|
||||||
path.join(__dirname, ".."),
|
path.join(__dirname, "..") + "\"",
|
||||||
{
|
{
|
||||||
cwd: path.join(__dirname, ".."),
|
cwd: path.join(__dirname, ".."),
|
||||||
shell: true,
|
shell: true,
|
||||||
|
@ -31,5 +31,6 @@ export function initStats() {
|
|||||||
process.memoryUsage().rss / 1024 / 1024
|
process.memoryUsage().rss / 1024 / 1024
|
||||||
)}mb/${memory.totalMemMb.toFixed(0)}mb ${networkUsage}`
|
)}mb/${memory.totalMemMb.toFixed(0)}mb ${networkUsage}`
|
||||||
);
|
);
|
||||||
}, 1000 * 10);
|
// TODO: node-os-utils might have a memory leak, more investigation needed
|
||||||
|
}, 1000 * 60 * 5);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,6 @@ export class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.server.on("upgrade", (request, socket, head) => {
|
this.server.on("upgrade", (request, socket, head) => {
|
||||||
console.log("socket requests upgrade", request.url);
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.ws.handleUpgrade(request, socket, head, (socket) => {
|
this.ws.handleUpgrade(request, socket, head, (socket) => {
|
||||||
this.ws.emit("connection", socket, request);
|
this.ws.emit("connection", socket, request);
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
import { WebSocket } from "@fosscord/gateway";
|
import { WebSocket } from "@fosscord/gateway";
|
||||||
import { Message } from "./Message";
|
|
||||||
import { Session } from "@fosscord/util";
|
import { Session } from "@fosscord/util";
|
||||||
|
|
||||||
export async function Close(this: WebSocket, code: number, reason: string) {
|
export async function Close(this: WebSocket, code: number, reason: string) {
|
||||||
console.log("[WebSocket] closed", code, reason);
|
console.log("[WebSocket] closed", code, reason);
|
||||||
if (this.session_id) await Session.delete({ session_id: this.session_id });
|
if (this.session_id) await Session.delete({ session_id: this.session_id });
|
||||||
// @ts-ignore
|
if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
|
||||||
this.off("message", Message);
|
if (this.readyTimeout) clearTimeout(this.readyTimeout);
|
||||||
|
|
||||||
|
this.deflate?.close();
|
||||||
|
|
||||||
|
this.removeAllListeners();
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ export async function Connection(
|
|||||||
socket.on("close", Close);
|
socket.on("close", Close);
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
socket.on("message", Message);
|
socket.on("message", Message);
|
||||||
|
console.log(`[Gateway] Connections: ${this.clients.size}`);
|
||||||
|
|
||||||
const { searchParams } = new URL(`http://localhost${request.url}`);
|
const { searchParams } = new URL(`http://localhost${request.url}`);
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -37,8 +37,6 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[Gateway] Opcode " + OPCODES[data.op]);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await OPCodeHandler.call(this, data);
|
return await OPCodeHandler.call(this, data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -214,8 +214,6 @@ export async function onIdentify(this: WebSocket, data: Payload) {
|
|||||||
// application // TODO for applications
|
// application // TODO for applications
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log("Send ready");
|
|
||||||
|
|
||||||
// TODO: send real proper data structure
|
// TODO: send real proper data structure
|
||||||
await Send(this, {
|
await Send(this, {
|
||||||
op: OPCODES.Dispatch,
|
op: OPCODES.Dispatch,
|
||||||
|
@ -18,6 +18,9 @@ export async function Send(socket: WebSocket, data: Payload) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => {
|
||||||
|
if (socket.readyState !== 1) {
|
||||||
|
return rej("socket not open");
|
||||||
|
}
|
||||||
socket.send(buffer, (err: any) => {
|
socket.send(buffer, (err: any) => {
|
||||||
if (err) return rej(err);
|
if (err) return rej(err);
|
||||||
return res(null);
|
return res(null);
|
||||||
|
@ -46,7 +46,9 @@ export async function listenEvent(event: string, callback: (event: EventOpts) =>
|
|||||||
} else {
|
} else {
|
||||||
const cancel = () => {
|
const cancel = () => {
|
||||||
events.removeListener(event, callback);
|
events.removeListener(event, callback);
|
||||||
|
events.setMaxListeners(events.getMaxListeners() - 1);
|
||||||
};
|
};
|
||||||
|
events.setMaxListeners(events.getMaxListeners() + 1);
|
||||||
events.addListener(event, (opts) => callback({ ...opts, cancel }));
|
events.addListener(event, (opts) => callback({ ...opts, cancel }));
|
||||||
|
|
||||||
return cancel;
|
return cancel;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user