perf(net): near-real-time sandbox for non-host viewers
Two causes made a shared shell's PTY stream lag for non-host viewers and then arrive "all at once": 1. Nagle's algorithm — PTY echo/keystrokes/chat are tiny frames; Nagle + delayed-ACK (amplified by Tailscale RTT) coalesced them into bursts. Set TCP_NODELAY on the client websocket (Plain/--no-tls path) and on each server connection's socket (via ws.io_proto.transport). 2. Head-of-line blocking — ConnectionManager.broadcast awaited each send() serially under a global lock, so the slowest viewer gated delivery to everyone AND stalled the server's read of the broker's next frame, backing the stream up. Give each connection its own bounded outbound queue + writer task; broadcast now only enqueues and never waits on a socket. A peer that overflows its backlog (4096) is evicted (close 1013) and resyncs on reconnect rather than buffering without bound or being fed a gapped terminal stream. Init snapshot is enqueued as the guaranteed-first frame (send_state -> state_frame). Adds tests/test_manager.py (FIFO, slow-client isolation, wedged eviction, reconnect replacement). Also fmt-clean sbx.rs/theme.rs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,13 +2,27 @@ import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import base64
|
||||
import socket
|
||||
from dataclasses import asdict
|
||||
|
||||
from sanic import Sanic, Request, response, Websocket
|
||||
from sanic.response import HTTPResponse, json as json_response
|
||||
|
||||
from .models import Message, UserSession
|
||||
from .helpers import get_client_ip, send_state, utcnow
|
||||
from .helpers import get_client_ip, state_frame, utcnow
|
||||
|
||||
|
||||
def _disable_nagle(ws: Websocket) -> None:
|
||||
"""Set TCP_NODELAY on a websocket's underlying socket. Small interactive
|
||||
frames (PTY echo, keystrokes, chat) must not wait on Nagle coalescing +
|
||||
delayed-ACK before reaching a viewer. Best-effort: any transport without a
|
||||
raw socket (or where the option can't be set) is simply left as-is."""
|
||||
try:
|
||||
sock = ws.io_proto.transport.get_extra_info("socket")
|
||||
if sock is not None:
|
||||
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
# Hard cap on a single relayed WS frame. The largest legitimate frame is one
|
||||
@@ -137,11 +151,12 @@ async def chat_ws(request: Request, ws: Websocket, app: Sanic) -> None:
|
||||
return
|
||||
|
||||
manager = app.ctx.connection_manager
|
||||
await manager.connect(user_id, ws)
|
||||
_disable_nagle(ws)
|
||||
# Enqueue this client's init snapshot as its first outbound frame, then
|
||||
# register it so broadcasts can target it — guaranteeing init arrives first.
|
||||
await manager.connect(user_id, ws, initial=state_frame(app))
|
||||
|
||||
try:
|
||||
await send_state(ws, app)
|
||||
|
||||
# Announce arrival to everyone already present, then a fresh roster.
|
||||
await manager.broadcast(
|
||||
json.dumps(
|
||||
|
||||
Reference in New Issue
Block a user