Limits and shutdown
Message and connection limits, close codes, keep-alive and how a WebSocket server drains.
Limits
Each limit is set on the gateway attribute and defaulted by WsModule (gateways on the HTTP port) or by ulo_ws_hyper::Server (gateways on its own port). A key left out of the attribute takes that default.
| Setting | Scope | Default | Over the limit |
|---|---|---|---|
max_message | bytes per message after reassembly | 64 MiB | the connection closes with 1009 |
max_connections | per gateway | none | the connection is accepted, then closed with 1013 unavailable: too many connections; under refuse = handshake the upgrade is refused 503 with Retry-After |
max_inflight | messages queued or running per connection | 64 | the gateway stops reading from that connection |
server_max_inflight | messages in flight across all connections of the server | 1,024 | every connection stops reading while no place is free |
max_outbound | queued outgoing messages per connection | 1,024 | closes with 1008 slow consumer, or drops the oldest with overflow = drop_oldest |
ping_interval | keep-alive Ping period | 30 seconds | Bound::Unbounded sends none |
pong_timeout | time for a Pong | 30 seconds | the connection ends as Lost |
max_message counts bytes per message; tungstenite also caps a frame at 16 MiB. Count::Max(0), a zero max_message and a zero Bound::After are refused in prepare, with the HTTP server's hint to write Unlimited or Unbounded instead.
max_inflight and server_max_inflight apply backpressure rather than refusing: the TCP connection carries it to the client. Answers to different messages, a stream's items among them, may interleave, each carrying its message's id. Only unary answers under a bound of one come back in the order their messages arrived.
server_max_inflight has no form on the gateway attribute. It is set on WsModule::server_max_inflight or ulo_ws_hyper::Server::server_max_inflight.
A connection turned away for capacity runs no connect hook and no on_disconnect.
Close codes
| Code | When |
|---|---|
| 1001 | the server is shutting down (the server is shutting down) |
| 1002 | a protocol violation |
| 1003 | a binary frame on a text-only gateway |
| 1007 | a text frame that is not UTF-8 |
| 1008 | BadRequest, NotFound, Conflict or Unprocessable at connect, and a slow consumer |
| 1009 | a message over max_message |
| 1011 | Internal or Unimplemented at connect, including a panic in OnConnect, and a panic in a hand-written gateway's on_message |
| 1013 | TooManyRequests or Unavailable at connect, and a gateway over max_connections |
| 3000 | Unauthorized at connect |
| 3003 | Forbidden at connect, a connect guard's Ok(false) included |
| 3008 | Timeout at connect |
| 3000 to 4999 | codes a gateway sets itself through ConnectRefused::code(CloseCode, reason) or CallError::with_close_code(CloseCode), as graphql-transport-ws does with 4401 |
A refusal by kind under 1008, 1011 or 1013 starts its reason with the kind as the envelope spells it, then : : not_found: no such room, unavailable: too many connections, internal: internal error. A client reads the kind back from the reason. A code one kind owns (3000, 3003, 3008) carries the reason alone.
Frames Ping, Pong and Close are handled by the protocol layer. A client that offers permessage-deflate is answered without it, since tungstenite has no RFC 7692 support, and messages travel uncompressed.
Keep-alive
The server sends a Ping every ping_interval. A Pong that misses pong_timeout, or a connection closed without a Close frame, ends the connection as DisconnectReason::Lost.
Shutdown
At the start of the drain, idle connections close with 1001 at once. Busy connections stop reading, finish their in-flight messages, then close with 1001. Each of these connections gets on_disconnect with DisconnectReason::Drain.
After sending 1001, the read loop waits for the client's Close under pong_timeout, and the app's drain window (AppBuilder::drain_timeout, ten seconds unset) ends the wait first if it is shorter. A client that never answers delays shutdown by the shorter of the two.
An upgrade request arriving once the drain has begun is refused 503. A connection admitted during the 101 but caught by the drain before its connect phase opened is refused with the drain's own 1001 and runs no hook.
examples/23-ws-gateway pins the close frame: a drain delivers (1001, "the server is shutting down") to an idle connection, and the journal then reads disconnected ada: Drain.
Both servers (the HTTP hand-off and ulo-ws-hyper) run the same drain, through the gateway table's drain and close. On the HTTP port the hand-off's drain and close are driven by the HTTP server's, since an upgraded connection has left the HTTP engine.