Repository navigation
Description
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,sendTextis synchronous and only hands the data todart: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 (thedart:ioWebSocketclass in SDK 3.13.7,lib/_http/websocket.dart:288, has nobufferedAmount). - Server input: relic's
IORelicWebSocketforwards incoming frames into aStreamControllerwithoutonPause/onResume, and never pauses its subscription on thedart:iosocket (relic_io/lib/src/adapter/io_relic_web_socket.dart:11,:54-80). Serverpod processes messages one by one withawait 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._listenToWebSocketStreamadds 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.dartknow open, close, ping/pong, message, error andBadRequest; 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
MethodStreamCreditmessage; the sender pauses its subscription to the producing stream when the credit is used up. A pause of the receivingStreamControllerstops granting credit. Default credit large enough that existing apps see no change. - In relic, wire
onPause/onResumeof the events controller to thedart:iosubscription 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.0andrelic_io 2.0.0-rc.2(the version in our lockfile);relic_io 2.0.0-rc.6is 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInbox 📫
Activity