mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Upgrade socket before closing with 1008 (ratelimit)
This commit is contained in:
parent
e2f23e51d2
commit
f72fcdbd65
1 changed files with 11 additions and 10 deletions
|
|
@ -45,6 +45,17 @@ const connections = new Set<WebSocket>();
|
|||
function connectStream(socket: WebSocket, ip: string | undefined, conf: DittoConf) {
|
||||
const controllers = new Map<string, AbortController>();
|
||||
|
||||
if (ip) {
|
||||
const remaining = Object
|
||||
.values(limiters)
|
||||
.reduce((acc, limiter) => Math.min(acc, limiter.client(ip).remaining), Infinity);
|
||||
|
||||
if (remaining < 0) {
|
||||
socket.close(1008, 'Rate limit exceeded');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
socket.onopen = () => {
|
||||
connections.add(socket);
|
||||
relayConnectionsGauge.set(connections.size);
|
||||
|
|
@ -206,16 +217,6 @@ const relayController: AppController = (c, next) => {
|
|||
ip = undefined;
|
||||
}
|
||||
|
||||
if (ip) {
|
||||
const remaining = Object
|
||||
.values(limiters)
|
||||
.reduce((acc, limiter) => Math.min(acc, limiter.client(ip).remaining), Infinity);
|
||||
|
||||
if (remaining < 0) {
|
||||
return c.json({ error: 'Rate limit exceeded' }, 429);
|
||||
}
|
||||
}
|
||||
|
||||
const { socket, response } = Deno.upgradeWebSocket(c.req.raw, { idleTimeout: 30 });
|
||||
connectStream(socket, ip, conf);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue