Repository navigation
Description
Goal
Add configurable bidirectional slow-consumer protection to the existing WebSocket plugin, preserving reactive forwarding and WebSocket protocol semantics.
This is a separate task from #8148 (AI Proxy streaming responses). It applies to proxied WebSocket sessions, including non-AI traffic. No separate backpressure plugin or SSE encoder is required.
Current implementation and review boundary
Baseline: master aab453f06dbd135da9b329ee153758ceac0e1834.
WebSocketPlugin.ShenyuWebSocketHandler.handle() already bridges both directions using WebSocketSession.send(peer.receive().doOnNext(WebSocketMessage::retain)). It composes the two sends with Mono.zip(...) and separately subscribes to the close-status propagation publishers.
The reactive transport already provides backpressure support. This task must measure that behavior and add missing application policy, rather than assuming no flow control exists. No production memory leak or deadlock is claimed by this issue.
The lifecycle to verify includes both send subscriptions, retained payloads, and the independently subscribed close propagation. Handshake completion is not session completion.
Scope
Protect both directions
- Client -> gateway -> upstream: handle an upstream service that stops consuming messages.
- Upstream -> gateway -> client: handle a slow or stopped client reader.
- Track pending data and sustained lack of write progress separately for each direction. An idle connection with no pending output is not a slow consumer.
- Preserve existing reactive
receive()/send()ownership. Do not insert an unbounded queue, per-message detached subscription, or an unbounded EventLoop task backlog.
Define configurable bounds and termination
- Bound pending payload bytes, message count/prefetch where buffering exists, and oversized-message behavior. Distinguish queued bytes from total bytes sent over a healthy long-lived connection.
- Define a configurable timeout for continuous write blockage with pending data, with explicit defaults and compatibility. A transient watermark transition must not immediately terminate a session.
- Do not silently drop or replace messages. Define deterministic overflow/stall behavior and a protocol-valid close status; document that a close frame may not be deliverable to a non-reading peer.
- On timeout, overflow, disconnect, write failure, cancellation, or session completion, terminate the associated relay subscriptions and peer session exactly once. Verify the separately subscribed close propagation cannot outlive its owning session.
- Clean up timers, queued payloads, and retained references on every terminal path. Do not rely solely on physical connection closure or HTTP upgrade completion.
Preserve WebSocket semantics
- Forward text/binary messages and fragmentation correctly; do not re-frame messages as SSE or assume one network chunk equals one application message.
- Verify Ping/Pong and Close behavior under data pressure. Normal idle/heartbeat behavior must remain distinguishable from a blocked writer.
- Avoid blocking either EventLoop. If batched draining is introduced, a writable non-empty queue must continue draining after a batch without requiring a new message or writability transition.
- A reconnect must not replay already forwarded messages automatically.
Observability and configuration
Use existing plugin/selector/rule configuration as appropriate. Expose bounded direction/reason metrics for pending bytes, stall duration, overflow, and terminated sessions. Record one session terminal outcome; do not produce a new terminal event for every dropped message. No payloads, raw session/request IDs, or arbitrary endpoints as metric labels.
A transport close must not directly implement AI refund or Kafka settlement logic; those belong to the corresponding application/accounting owner.
Acceptance criteria
- Real loopback WebSocket tests exercise a fast peer, throttled reader, stopped reader, and recovery in each direction, including simultaneous pressure.
- Pending memory and asynchronous handoff backlog remain within documented bounds under concurrency; include measurements using [Task][Performance] Establish reproducible benchmarks and track gateway optimizations #7650's benchmark principles.
- Temporary pressure recovers without truncating/reordering messages; multi-batch queues drain even after the producer finishes.
- Stall timeout, overflow, disconnect, write failure, cancellation, and normal completion release retained buffers and cancel timers/subscriptions without double release or duplicate close/terminal events.
- Tests cover text, binary, fragmented messages, Ping/Pong, and Close while data is backpressured.
- An idle healthy session survives; the timeout measures blocked output with pending data, not absence of business messages.
- One affected session does not disturb other sessions; session accounting follows the actual relay lifetime rather than the handshake.
- Documentation defines configuration, close behavior, compatibility, and measured resource bounds.
Related boundaries
#8148 owns AI Proxy streaming-response protection. #8062 concerns stream retirement during configuration reloads, whereas this issue concerns active-session slow consumers. Coordinate terminal ownership and cleanup without expanding this task into reload policy or a new shared networking stack.
Activity
zephyr45 commentedon Nov 21, 2026
Hi @Aias00, would an outside contribution to this WebSocket slow-consumer task be welcome? I’d begin with the required IssueSpec proposal/design, then scope per-direction pending-output limits, stalled-write detection and session-owned cleanup while preserving reactive backpressure and healthy idle sessions. I’d verify both directions with real loopback peers and buffer/timer/subscription cleanup checks, keeping #8513’s handshake/idle timeouts and #8195’s SSE work separate. I’ll wait for your confirmation before starting implementation. Thank you.
Aias00 commentedon Nov 21, 2026
sure, be free to submit your pr