mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Merge branch 'fix-relay-close' into 'main'
Ensure relay close handler is called See merge request soapbox-pub/ditto!711
This commit is contained in:
commit
6c5574417d
1 changed files with 15 additions and 4 deletions
|
|
@ -60,7 +60,7 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS
|
|||
.reduce((acc, limiter) => Math.min(acc, limiter.client(ip).remaining), Infinity);
|
||||
|
||||
if (remaining < 0) {
|
||||
socket.close(1008, 'Rate limit exceeded');
|
||||
closeSocket(1008, 'Rate limit exceeded');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS
|
|||
if (rateLimited(limiters.msg)) return;
|
||||
|
||||
if (typeof e.data !== 'string') {
|
||||
socket.close(1003, 'Invalid message');
|
||||
closeSocket(1003, 'Invalid message');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -95,13 +95,24 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS
|
|||
};
|
||||
|
||||
socket.onclose = () => {
|
||||
handleSocketClose();
|
||||
};
|
||||
|
||||
// HACK: Due to a bug in Deno, we need to call the close handler manually.
|
||||
// https://github.com/denoland/deno/issues/27924
|
||||
function closeSocket(code?: number, reason?: string): void {
|
||||
socket.close(code, reason);
|
||||
handleSocketClose();
|
||||
}
|
||||
|
||||
function handleSocketClose() {
|
||||
connections.delete(socket);
|
||||
relayConnectionsGauge.set(connections.size);
|
||||
|
||||
for (const controller of controllers.values()) {
|
||||
controller.abort();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function rateLimited(limiter: Pick<RateLimiter, 'client'>): boolean {
|
||||
if (ip) {
|
||||
|
|
@ -109,7 +120,7 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS
|
|||
try {
|
||||
client.hit();
|
||||
} catch {
|
||||
socket.close(1008, 'Rate limit exceeded');
|
||||
closeSocket(1008, 'Rate limit exceeded');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue