You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
perf: replace 8 KB copy loops with 64 KB copy_bidirectional_with_sizes
The default tokio::io::copy() buffer is 8 KB. On a 200ms relay RTT that
caps throughput at ~40 KB/s — well below even Iran's ~1 MB/s cable.
Replacing all three bidirectional pipe sites with
copy_bidirectional_with_sizes(65536, 65536) raises the per-connection
ceiling to ~320 KB/s at the same RTT.
The switch also fixes half-close handling: the previous tokio::select!
pattern cancelled the other copy direction when one side closed, which
could silently drop in-flight data. copy_bidirectional_with_sizes handles
each FIN independently, matching TCP half-close semantics.
Changes:
- plain-tcp passthrough (do_plain_tcp_tunnel): drop manual split, use
copy_bidirectional_with_sizes on the full TcpStream pair.
- SNI-rewrite TLS tunnel (do_sni_rewrite_tunnel_from_tcp): same — no
split needed, TlsStream implements AsyncRead+AsyncWrite.
- plain-HTTP passthrough (do_plain_http_tunnel): write the rewritten
request first, then reunite the OwnedReadHalf/OwnedWriteHalf before
calling copy_bidirectional_with_sizes (reunite is infallible here
since the halves came from the same split).
- read_http_head / read_http_head_io: stack tmp buffer 4 KB -> 16 KB so
large cookie/auth-token headers are read in one syscall.
- TLS-detect peek timeout: 300ms -> 100ms (browsers send ClientHello
within 10-50ms; saves 200ms per new inbound connection).
Adds copy_bidirectional_large_buf_roundtrip test to verify the duplex
relay path completes cleanly with large buffer sizes.
0 commit comments