Skip to content

feat(ws): prevent memory unbounded growth on slow WebSocket subscribers with backpressure buffer (#335) - #535

Closed
sparkysolutions10-pixel wants to merge 1 commit into
Divinity-Labs:mainfrom
sparkysolutions10-pixel:feat/ws-backpressure-buffer-335
Closed

sparkysolutions10-pixel wants to merge 1 commit into
Divinity-Labs:mainfrom
sparkysolutions10-pixel:feat/ws-backpressure-buffer-335

Conversation

@sparkysolutions10-pixel

Copy link
Copy Markdown
Contributor

Closes #335

Summary of Changes

  • Enforces WebSocket backpressure and memory bounds in src/ws/fanout.ts based on socket bufferedAmount.
  • Configurable two-tier threshold:
    1. Soft Threshold (WS_BUFFER_THRESHOLD_BYTES, default 64KB): When a subscriber's buffer exceeds this threshold, intermediate price updates are coalesced and dropped to prevent memory queue growth, incrementing ws_messages_dropped_total.
    2. Hard Ceiling (WS_BUFFER_MAX_BYTES, default 1MB): Stalled peers exceeding this ceiling are terminated with WebSocket close code 1008 ("Buffer overflow") and unregistered, incrementing ws_slow_clients_total.
  • Guarantees that slow or stalled subscribers never exhaust server heap or degrade delivery to healthy peers.
  • Only increments ws_messages_sent_total upon confirmed write dispatch.
  • Added automated unit tests in src/__tests__/fanout.test.ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

One slow websocket client can grow the server's memory without limit

1 participant