mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Tell clients when the gateway is full; default limit 60
A browser can't see why a WebSocket upgrade was refused, so a full gateway now accepts, sends a gateway_full error and closes. The client shows a translated hint that includes self-hosting. Default MAX_CONNECTIONS drops from 300 to 60 after measuring ~8-9 % of a core per listening client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
999262b6df
commit
e02a41d855
8 files changed
+17
-6
No files matched your search
+10
-4
@@ -93,7 +93,9 @@ const ALLOWED_ORIGINS = (process.env.ALLOWED_ORIGINS ?? "")
|
||||
// Each /ws socket can spawn a connector process holding a live TeamSpeak
|
||||
// session, so both need a ceiling. 0 disables the respective limit.
|
||||
const MAX_CONNECTIONS_PER_IP = Number(process.env.MAX_CONNECTIONS_PER_IP ?? 10);
|
||||
const MAX_CONNECTIONS = Number(process.env.MAX_CONNECTIONS ?? 300);
|
||||
// Measured 2026-10-25: ~8-9 % of a core per client listening to constant audio
|
||||
// (connector + node + cloudflared), ~10-15 MB RAM. 60 suits a 2-core VPS.
|
||||
const MAX_CONNECTIONS = Number(process.env.MAX_CONNECTIONS ?? 60);
|
||||
const connectionsPerIp = new Map<string, number>();
|
||||
// Every "connect" spawns a fresh connector, and a socket can send any number
|
||||
// of them - without this, 10 sockets were enough to join-flood a TS server
|
||||
@@ -570,9 +572,6 @@ const wss = new WebSocketServer({
|
||||
if (ALLOWED_ORIGINS.length > 0 && origin && !ALLOWED_ORIGINS.includes(origin.toLowerCase())) {
|
||||
return done(false, 403, "Origin not allowed");
|
||||
}
|
||||
if (MAX_CONNECTIONS > 0 && wss.clients.size >= MAX_CONNECTIONS) {
|
||||
return done(false, 503, "Gateway is full");
|
||||
}
|
||||
const ip = clientIp(req);
|
||||
if (MAX_CONNECTIONS_PER_IP > 0 && (connectionsPerIp.get(ip) ?? 0) >= MAX_CONNECTIONS_PER_IP) {
|
||||
return done(false, 429, "Too many connections");
|
||||
@@ -679,6 +678,13 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
// multiple /ws connections in parallel (one per server tab).
|
||||
let connection: Ts3Connection | undefined;
|
||||
|
||||
// Full: accept and say so, since a browser can't read why an upgrade was refused.
|
||||
if (MAX_CONNECTIONS > 0 && wss.clients.size > MAX_CONNECTIONS) {
|
||||
socket.send(JSON.stringify({ type: "error", code: "gateway_full", message: "This gateway is full right now. Please try again in a few minutes." }));
|
||||
socket.close(1013, "Gateway is full");
|
||||
return;
|
||||
}
|
||||
|
||||
const ip = clientIp(req);
|
||||
connectionsPerIp.set(ip, (connectionsPerIp.get(ip) ?? 0) + 1);
|
||||
|
||||
|
||||
Reference in new issue
Block a user