Skip to content

Commit e7579d4

Browse files
MDA2AVclaude
andcommitted
[Rust] Add tokio-ws - WebSocket
Hand-rolled WebSocket echo server on raw tokio, with no WebSocket library. The RFC 6455 handshake (including from-scratch SHA-1 + base64), frame parser/masking, and echo write path are implemented directly on a tokio TcpStream. Serving model is one current_thread runtime per core with SO_REUSEPORT sharding, matching the other engine-tier entries. Subscribes to echo-ws and echo-ws-pipeline. Passes validate-ws.py (7/7) locally and via the Docker build. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b418f06 commit e7579d4

8 files changed

Lines changed: 659 additions & 0 deletions

File tree

frameworks/tokio-ws/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
.git

frameworks/tokio-ws/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

frameworks/tokio-ws/Cargo.lock

Lines changed: 168 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/tokio-ws/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "httparena-tokio-ws"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
socket2 = { version = "0.5", features = ["all"] }
8+
tokio = { version = "1", default-features = false, features = ["io-util", "net", "rt"] }
9+
10+
[profile.release]
11+
codegen-units = 1
12+
lto = "thin"
13+
opt-level = 3
14+
panic = "abort"

frameworks/tokio-ws/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM rust:1.95 AS build
2+
WORKDIR /app
3+
COPY Cargo.toml Cargo.lock ./
4+
RUN mkdir src && echo "fn main() {}" > src/main.rs \
5+
&& cargo build --release \
6+
&& rm -rf src target/release/httparena-tokio-ws target/release/deps/httparena_tokio_ws*
7+
COPY src ./src
8+
RUN RUSTFLAGS="-C target-cpu=native" cargo build --release
9+
10+
FROM debian:bookworm-slim
11+
COPY --from=build /app/target/release/httparena-tokio-ws /server
12+
EXPOSE 8080
13+
CMD ["/server"]

frameworks/tokio-ws/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
```

frameworks/tokio-ws/meta.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"display_name": "tokio-ws",
3+
"language": "Rust",
4+
"type": "engine",
5+
"engine": "tokio",
6+
"description": "Hand-rolled WebSocket echo server on raw tokio — no WebSocket library. A current-thread runtime per core with SO_REUSEPORT sharding, a from-scratch RFC 6455 handshake (hand-written SHA-1 + base64) and a manual frame parser/masking + echo write path.",
7+
"repo": "https://github.com/tokio-rs/tokio",
8+
"enabled": true,
9+
"tests": [
10+
"echo-ws",
11+
"echo-ws-pipeline"
12+
],
13+
"maintainers": []
14+
}

0 commit comments

Comments
 (0)