Skip to content

Flow control (backpressure) for method streams over the WebSocket #5986

Open

Description

@t-kietzmann

Problem to Solve

Method streams buffer without limit in both directions, and pausing a subscription is never signalled to the peer:

  • Server output: every value of the output stream is sent immediately (serverpod/lib/src/server/websocket_request_handlers/helpers/method_stream_manager.dart:376-388 → method_websocket_request_handler.dart:227-240 → tryAdd, which is _webSocket.sendText(s) without feedback, :549-555). In relic, sendText is synchronous and only hands the data to dart:io (relic_io/lib/src/adapter/io_relic_web_socket.dart:88-95, :147-151), which buffers outgoing data without a limit and without exposing a buffered amount (the dart:io WebSocket class in SDK 3.13.7, lib/_http/websocket.dart:288, has no bufferedAmount).
  • Server input: relic's IORelicWebSocket forwards incoming frames into a StreamController without onPause/onResume, and never pauses its subscription on the dart:io socket (relic_io/lib/src/adapter/io_relic_web_socket.dart:11, :54-80). Serverpod processes messages one by one with await for (method_websocket_request_handler.dart:40) and waits there, for example, for a stream to open (:69-76), while relic keeps reading. TCP backpressure is lost.
  • Client input: ClientMethodStreamManager._listenToWebSocketStream adds every message to the caller's controller at once (serverpod_client/lib/src/client_method_stream_manager.dart:514-571); a paused caller is not reported to the server.
  • Protocol: the message types in serverpod_serialization/lib/src/websocket_messages.dart know open, close, ping/pong, message, error and BadRequest; there is no pause, resume or credit message.

A slow client or link therefore makes the server hold everything produced for it, and the producing async* method is never paused.

Proposal

Goal: pausing the subscription of a method stream on one side eventually pauses the producer on the other side, with bounded buffers in between.

For discussion:

  • Credit-based flow control per stream: the receiver grants n messages (or bytes) with a MethodStreamCredit message; the sender pauses its subscription to the producing stream when the credit is used up. A pause of the receiving StreamController stops granting credit. Default credit large enough that existing apps see no change.
  • In relic, wire onPause/onResume of the events controller to the dart:io subscription so the read side keeps TCP backpressure.
  • If #5903 (JSON-RPC 2.0 for streaming) goes ahead, define the credit message as part of its streaming extension.

Use Case

A sync stream that fans events out to many clients. A client behind a slow VPN link makes the server hold everything produced for it in dart:io's socket buffer, invisible to the per-instance memory budget we enforce, which can only count what we hold ourselves.

Alternatives

Our own flow control on top of Serverpod: the client acknowledges, the server keeps a bounded backlog per channel and closes a channel once it holds too many bytes, and events are batched per tick. It works for our own stream but cannot see the socket buffer, and every app that streams to slow clients has to build it again.

Additional context

  • Checked against serverpod 4.1.0, serverpod_client 4.1.0 and relic_io 2.0.0-rc.2 (the version in our lockfile); relic_io 2.0.0-rc.6 is the same at the cited places.
  • Related: #5897 (open; a paused input stream stalls closing and the shared socket), #5903 (open; JSON-RPC 2.0 for endpoint calls and streaming, the right place for flow control).

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions