Limit unacknowledged messages buffered for a connection

Every message sent over a link is kept in the send buffer until the
other side acknowledges it with a heartbeat, so it can be resent if
the session resumes. Nothing forced a peer to acknowledge, so a client
that kept heartbeating without a sequence number while subscribed to a
busy event made the controller buffer messages until it ran out of
memory.

Store buffered messages serialised, track their total size, and close
the connection once it goes over maxSendBufferSize. The controller
sets this to 64 MiB for the connections it accepts. A client that far
behind could not resume in a reasonable time anyway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-10-08 11:15:27 +00:00
1 parent ce6bfe282f
commit 235a15b9cc
3 files changed
+74 -9

No files matched your search

+5 -1
View File
@@ -13,6 +13,9 @@ import { logger } from "@clusterio/lib";
* @extends module:lib.WebSocketBaseConnector
* @alias module:controller/src/WsServerConnector
*/
// Clients that leave this much unacknowledged are dropped rather than buffered for.
const maxSendBufferSize = 64 * 1024 * 1024;
export default class WsServerConnector extends lib.WebSocketBaseConnector {
_timeoutId: ReturnType<typeof setTimeout> | null = null;
@@ -23,6 +26,7 @@ export default class WsServerConnector extends lib.WebSocketBaseConnector {
public _heartbeatInterval: number,
) {
super(new lib.Address(lib.Address.controller, 0), dst);
this.maxSendBufferSize = maxSendBufferSize;
// The following states are used in the server connector
// closed: Connection is closed
@@ -110,7 +114,7 @@ export default class WsServerConnector extends lib.WebSocketBaseConnector {
`Connector | resuming session with ${this.dst}, resending ${this._sendBuffer.length} buffered messages`
);
for (let message of this._sendBuffer) {
this._socket.send(JSON.stringify(message));
this._socket.send(message.text);
}
this.emit("resume");
}