XHTTP over HTTP/2: reducing per-stream memory
XHTTP over HTTP/2 uses noticeably more memory than RAW on both ends. This shows up most on memory-limited clients (#5944, #6268).
Previous discussions:
- #6268: the client allocates a per-stream upload scratch buffer of
min(peer SETTINGS_MAX_FRAME_SIZE, 512 KiB)and holds it for the whole life of the stream, although a single body read in Xray never returns more than 8 KiB. With REALITY/TLS straight to an Xray server the peer advertises 1 MiB, so every idle stream on a reused xmux connection holds 512 KiB. - golang/go#79831: proposal to lower that cap to 32 KiB in Go. Open since June with no decision.
- #6663: advertising 16 KiB from the Xray server. Not planned, because a CDN in front of the server advertises its own value.
There are two separate causes:
- Idle streams. The 512 KiB scratch buffer described above.
- Slow readers. Go's HTTP/2 flow-control windows are fixed: 1 MiB per stream on the server, 4 MiB on the client. When the reader is slower than the link (a video player with a full buffer, a relay whose next hop is slower, a slow upload target), the peer fills the whole window and the data sits unread in process memory. While the reader keeps up there is no difference. Kernel TCP avoids this with receive-buffer autotuning. Go's HTTP/2 has nothing like it: golang/go#63520 only proposes static window settings, and a small static window caps throughput on high-BDP paths.
Idle streams: options until Go changes
- Patch
frameScratchBufferLen(512 KiB → 32 KiB) in GOROOT and in vendored x/net in the release workflow. This is quick, but it only reaches official release binaries; mobile apps and distributions that build from source do not get it. - Cap the peer's
SETTINGS_MAX_FRAME_SIZEat 16 KiB inside Xray beforenet/httpreads it (part of the proposal below). This works for every build and behind any CDN.
Proposal
A frame-level shim between the TLS/REALITY conn and net/http that manages HTTP/2 flow control on its own side of the connection. It needs no x/net fork, no toolchain patch and no config fields.
- Frame size: the peer's
SETTINGS_MAX_FRAME_SIZEis rewritten to 16 KiB in both directions. On the client this caps the scratch buffer at 16 KiB whatever the server or a CDN advertises, which addresses the concern in #6663. On the server it does the same for stock clients. - Windows: each stream starts at the protocol default of 64 KiB. The window grows to twice what the reader consumed per RTT (RTT measured by PING, the same idea as gRPC-Go's BDP estimator) and shrinks by a quarter at a time once the reader stays well below it. It is bounded on both sides: never above what stock Go would grant, and never below 64 KiB or twice the current consumption per RTT.
- Roles: the server governs uploads and downloads; the client governs downloads only. It works with unmodified official Xray and mihomo peers. HTTP/1.1 and HTTP/3 are not touched. It can be disabled with
XRAY_XHTTP_FLOW=off.
Measurements
Idle upload streams: 40 streams on a reused connection, heap of both ends in one process.
| server | client | heap per stream |
|---|---|---|
| stock | stock | ~555 KiB |
| stock (same as behind a CDN) | shim | ~65 KiB |
| shim | stock | ~67 KiB |
Slow readers: 100 Mbit/s, 50 ms RTT, 30 sessions, REALITY.
| scenario | stock | shim |
|---|---|---|
| client, app reads downloads slowly | 146 MB | 31–65 MB |
| server, receiver of uploads is slow | 70 MB | 20 MB |
| fast then slow stream, unread data per stream | 4.2 MB | 0.17 MB |
| latency behind a bulk transfer | ~1.1 s | ~0.1 s |
Steady-state throughput is unchanged. The cost is a slower ramp for new streams: 5-second transfers are 3% slower at 30 ms RTT, 13% at 80 ms and 30% at 150 ms.
Tested with REALITY and TLS × stream-one, stream-up and packet-up against official Xray and mihomo clients, plus model, fuzz and -race tests and soak runs, with no leaks, stalls or data corruption.
Reference implementation:
- e6cbac3f: server, grow flow-control windows with the reader
- b74dc3e8: fix for (1), keep output that exceeds the caller's read buffer
- b8a534ab: client, the same for the downlink
- f3843677: shrink windows when the reader slows down
- fdbc1f21: 16 KiB max frame size
- 0e42c0a4:
XRAY_XHTTP_FLOW=off
Would you take the build-time patch as an interim fix for idle streams, and is a shim at this layer acceptable for Xray, or would you prefer the proposal done differently?
Source: XTLS/Xray-core