|
| 1 | +# tokio-ws |
| 2 | + |
| 3 | +A WebSocket echo server written directly on **raw tokio**, with the WebSocket |
| 4 | +protocol **hand-rolled** — no `tokio-tungstenite`, no `wtx`, no WS library at all. |
| 5 | + |
| 6 | +It exists as an `engine`-tier reference point: the lowest-level way to serve the |
| 7 | +`echo-ws` profile on the tokio reactor, so the leaderboard shows what the runtime |
| 8 | +itself can do once the framing is reduced to the minimum. |
| 9 | + |
| 10 | +## What's implemented by hand |
| 11 | + |
| 12 | +- **RFC 6455 handshake** — request parsing, `Sec-WebSocket-Accept` derivation, |
| 13 | + and the `101 Switching Protocols` reply. SHA-1 and base64 are both written |
| 14 | + from scratch (`src/main.rs`), so the only dependencies are `tokio` and |
| 15 | + `socket2`. |
| 16 | +- **Frame codec** — a streaming parser that handles 7/16/64-bit lengths, |
| 17 | + client→server unmasking, and partial frames split across `read()`s. Echoes are |
| 18 | + re-emitted as unmasked server frames, preserving FIN + opcode (so fragmented |
| 19 | + messages pass through transparently). |
| 20 | +- **Control frames** — `Ping` is answered with `Pong`; `Close` is echoed and the |
| 21 | + connection ends. |
| 22 | + |
| 23 | +## Serving model |
| 24 | + |
| 25 | +One `current_thread` tokio runtime per core, each binding `0.0.0.0:8080` with |
| 26 | +`SO_REUSEPORT` so the kernel shards new connections across cores — no shared |
| 27 | +accept queue, no cross-core work-stealing. `TCP_NODELAY` is set per connection, |
| 28 | +and outgoing echoes are batched per read so a pipelined burst flushes in one |
| 29 | +write. This mirrors the other one-thread-per-core engine entries (e.g. |
| 30 | +`rust-epoll`). |
| 31 | + |
| 32 | +## Endpoint |
| 33 | + |
| 34 | +| Method | Path | Behavior | |
| 35 | +|--------|-------|-------------------------------------------| |
| 36 | +| GET | `/ws` | WebSocket upgrade, then echo every frame | |
| 37 | + |
| 38 | +A non-upgrade `GET /ws` is rejected with `400`; other paths return `404`. |
| 39 | + |
| 40 | +## Build & run |
| 41 | + |
| 42 | +```bash |
| 43 | +cargo build --release |
| 44 | +./target/release/httparena-tokio-ws # listens on :8080 |
| 45 | +python3 ../../scripts/validate-ws.py localhost 8080 /ws |
| 46 | +``` |
0 commit comments