Skip to content

Commit efbba01

Browse files
committed
新增:-via direct/VPS 下 -parallel 开真正独立的 QUIC 连接,不再只是同一条连接的多个 stream
单条 QUIC 流在有丢包的链路上拥塞窗口涨不起来(实测案例:cwnd 卡在 5~14KB、丢包 ~5%,吞吐≈cwnd/RTT),靠复用同一条连接开多个 stream 绕不开这个上限——聚合吞吐 还是被同一个拥塞窗口封顶。现在 parallel>1 时每个分块各自打洞、各自握手,拿到 互相独立的连接(硬顶 4 条,不随文件大小增长),至少打通 1 条就继续传;relay 路径不变,仍是同一条 websocket 上的多个会话。 进度行加上每条连接各自的即时速率(conn1/conn2/...),文档同步更新两条路径的 真实区别与诊断方法。
1 parent 5746f2f commit efbba01

7 files changed

Lines changed: 241 additions & 51 deletions

File tree

‎anyproxy.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func init() {
100100
flag.StringVar(&gSendVia, "via", nat.ViaDirect, "-send/-recv: \"direct\" (punch through NAT, fails closed if no path), \"relay\" (through the server B, no punching needed), or the email of a public VPS (needs directRelay enabled) to blindly relay the NAT punch through -- for when the two peers cannot punch to each other directly (e.g. both behind CGNAT) but can each reach that VPS; the QUIC/TLS session still ends end-to-end between the two peers, the VPS only forwards opaque UDP packets. All are end-to-end encrypted. See docs/direct-relay-design.md. -wol: the subscriber's email who should broadcast instead of this machine, default (\"direct\") means broadcast locally")
101101

102102
flag.StringVar(&gRecv, "recv", "", "Fetch a file or directory from another subscriber and exit, scp-style EMAIL:PATH (PATH is relative to that peer's websocket.client.receive.dir, and this machine must already be listed in its receive.allow)")
103-
flag.IntVar(&gParallel, "parallel", 1, "-send/-recv: split each large file into up to N chunks and transfer them over N concurrent connections (default 1, today's single-connection behavior); small files are never split")
103+
flag.IntVar(&gParallel, "parallel", 1, "-send/-recv: split each large file into up to N chunks and transfer them over N concurrent connections (default 1, today's single-connection behavior); small files are never split. N is capped at 4 regardless of what's passed -- independent connections beyond that stop paying off and start hurting NAT punch reliability. -via direct/VPS: each connection is its own independently NAT-punched QUIC connection with its own congestion window, which is the point -- a single QUIC flow on a lossy path can get stuck with a small window no matter how large the file is, and a few independent flows recover the throughput a lossy path is actually capable of. -via relay: chunks are still concurrent logical sessions but multiplexed over the one already-open connection to the server, so this mainly helps by overlapping round-trips rather than opening more raw connections")
104104
flag.StringVar(&gConflict, "conflict", "", "-send/-recv: what to do about a same-name file or a leftover .part from an interrupted transfer. MODE is one of: ask (interactive, default on a terminal), rename (save under a new name, no probing; default when stdin is not a terminal), overwrite, skip, or resume (continue a matching .part automatically; skip if the name already holds an identical file, else save under a new name). Only MODE=ask additionally prompts at the terminal: for a same-name file, [r]ename/[o]verwrite(if identical)/[s]kip; for a matching .part, [c]ontinue/[r]estart/[s]kip -- those letters are answers to that prompt, not values for -conflict itself")
105105
flag.BoolVar(&gDirectPlainUDP, "direct-plain-udp", false, "direct (NAT punch) connections: skip quic-go's batched/ECN fast path for the UDP socket, always falling back to plain per-packet I/O. Try this if a direct transfer shows high loss and a congestion window stuck near its floor even on an otherwise healthy link -- on some machines (seen on Windows, likely a NIC driver/virtual adapter quirk) that fast path itself corrupts or delays packets, which quic-go then mistakes for congestion. Unlike -debug, this does not log every packet, so it's fine to leave on")
106106

‎docs/en/websocket.md‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,23 +464,25 @@ Troubleshooting: with `-debug 2`, quic-go prints `connection doesn't allow setti
464464

465465
#### Single large file chunked parallel transfer: `-parallel N`
466466

467-
By default each file occupies only one connection (one QUIC stream on `direct`, one relay session on `relay`), which is enough when a single connection's throughput already fills the link, but insufficient when limited by the single-stream congestion window climb or the relay flow-control window — the "single connection" ceiling. `-parallel N` (default 1) splits a **single** large file into at most `N` chunks by byte range, each opening an independent connection for parallel transfer. Both paths (`-via direct`/`-via relay`) and both directions (`-send`/`-recv`) are supported:
467+
By default each file occupies only one connection (one independently NAT-punched QUIC connection on `direct`/VPS blind-relay, one relay session on `relay`), which is enough when a single connection's throughput already fills the link, but insufficient when limited by the single-stream congestion window climb or the relay flow-control window — the "single connection" ceiling. `-parallel N` (default 1, hard-capped at 4 — anything higher is clamped down to 4) splits a **single** large file into at most `N` chunks for parallel transfer. Both paths (`-via direct`/VPS, `-via relay`) and both directions (`-send`/`-recv`) are supported:
468468

469469
```bash
470470
anyproxy -send bigfile.zip -to home@example.com -parallel 4
471471
anyproxy -recv home@example.com:backup/bigfile.zip -to /data/in -parallel 4
472472
```
473473

474474
- **Only splits a single file, no multi-file concurrency**. Batch `-send`/`-recv` of multiple files still transfers one after another — the purpose of chunked parallelism is to let a single large file use the bandwidth faster, not to let multiple files compete for the same bandwidth (that would actually lengthen each file's time).
475-
- **Files too small are not split**: files below 8MiB always take the single-connection path, the `-parallel` value is ignored, since the chunking handshake/header overhead isn't worth it on small files.
476-
- **Failure semantics identical to no-chunk**: if any chunk fails (network error, verification failure) the whole file errors out and the `.part` file is cleaned up, leaving no half-done file with only some chunks right, and no auto-retry.
475+
- **Files too small are not split**: files below 8MiB always take the single-connection path, the `-parallel` value is ignored, since the chunking handshake/header overhead isn't worth it on small files. Chunk count doesn't keep growing with file size either — it tops out at 4 (for files 16MiB and up); a multi-gigabyte file still only splits into 4, not more.
476+
- **Failure semantics identical to no-chunk, but tolerant of partial connection failure**: on `-via direct`/VPS, as long as at least 1 of the `N` independent connections punches through, the transfer proceeds on whichever ones succeeded (a few candidate paths not working is normal and shouldn't fail the whole transfer). Once connections are up, any chunk transfer failure (network error, verification failure) still errors out the whole file and cleans up the `.part` file — no half-done file, no auto-retry.
477477
- **On `-via relay`, each chunk negotiates its own encryption session** (independent random salt, independently derived AES-256-GCM key); the protocol has long supported "open a new encryption session anytime", so no handshake change is needed for chunking.
478+
- **The progress line now shows each connection's own instantaneous rate** (`conn1: ... conn2: ...`), so you can tell if one connection is dragging behind the others.
478479

479-
##### `-parallel` does not always speed up: understand the bottleneck first
480+
##### Whether `-parallel` actually opens multiple connections differs by path
480481

481-
**Chunking uses the same underlying connection** — on `direct`, the N chunks are N streams on the same QUIC connection; on `relay`, they are N relay sessions on the same websocket (same TCP connection). QUIC/TCP congestion control is computed per **connection**, not per stream/session, meaning these N concurrent paths share the same congestion window and look like **the same five-tuple** (same pair of source/dest IP+port) to network devices. If the bottleneck is operator per-flow rate limiting, or the link itself is congested with packet loss, `-parallel` in this same-connection-multiplexed implementation most likely won't help — the operator still sees "one flow", and won't allocate more bandwidth just because the app opened a few more streams.
482+
- **`-via direct`/`-via a VPS's email` (blind-relayed punch): genuinely independent QUIC connections.** Each chunk runs its own punch handshake and QUIC handshake, ending up with a fully independent connection that maintains its own congestion window — that's the whole point: on a link with random packet loss, a single QUIC flow's congestion window can get stuck repeatedly pinned low (a real measured case: cwnd stuck around 5~14KB with ~5% loss, throughput ≈ cwnd/RTT), while several independent connections each maintain their own window and the aggregate throughput can recover close to linearly — the same principle multi-threaded download managers rely on. The local UDP socket is still shared (no extra local ports needed), but the QUIC connections themselves, and the bindings on the VPS blind-relay side, are each independent.
483+
- **`-via relay` (through server B): still the same underlying connection.** The N chunks are N relay sessions on the same websocket (same TCP connection); QUIC/TCP congestion control is computed per **connection**, so these N concurrent paths share the same congestion window and look like the same five-tuple to network devices. If the bottleneck is operator per-flow rate limiting or link congestion, `-parallel` on the `relay` path most likely won't help; it truly helps when a single relay session's own flow-control window fills before the network bandwidth does, or when the bottleneck is actually CPU (hashing, encryption/decryption). To get genuinely independent multiple connections, switch to `-via direct` or `-via a VPS's email`.
482484

483-
The scenarios where `-parallel` truly helps are when a single stream/relay session's own flow-control window (not congestion window) fills before the network bandwidth — e.g. a single stream's window is too small on a high-latency long-distance link, or the bottleneck is actually CPU (hashing, encryption/decryption) rather than network. These two cases benefit from multiple concurrent paths; if the bottleneck is poor operator cross-network interconnection quality or per-flow/per-account rate limiting, opening more won't help.
485+
##### Use `iperf3` to diagnose the bottleneck first, especially for `-via relay`
484486

485487
**Use `iperf3` to diagnose before relying on feeling** (assuming A transfers slowly, C is the receiving machine, and the two can reach each other):
486488

@@ -508,9 +510,11 @@ Reading the results:
508510

509511
| Phenomenon | Conclusion |
510512
|---|---|
511-
| Single stream slow, multi-stream (`-P 4`) `[SUM]` much higher than single | per-flow rate limiting / single-stream window can't fill, `-parallel` is worth using |
512-
| Single stream slow, multi-stream `[SUM]` about the same, high packet loss | link itself congested (common with poor operator cross-network interconnection quality), `-parallel` has limited benefit |
513-
| TCP results ok, UDP noticeably worse | operator may have separately limited UDP; even reshaping into multiple independent QUIC connections may not help, consider switching to `-via relay` (TCP websocket) to bypass |
513+
| Single stream slow, multi-stream (`-P 4`) `[SUM]` much higher than single | per-flow rate limiting / single-stream window can't fill, `-parallel` is worth using (especially under `-via direct`/VPS, since those connections really are independent) |
514+
| Single stream slow, multi-stream `[SUM]` about the same, high packet loss | link itself congested (common with poor operator cross-network interconnection quality); independent flows each still eat their own share of the loss so this can't be fully bypassed, `-parallel` has limited benefit but is usually still somewhat better than a single stream (see the QUIC cwnd/loss stats under `-check`) |
515+
| TCP results ok, UDP noticeably worse | operator may have separately limited UDP; `-via direct`/VPS rides QUIC/UDP, so multiple independent connections may not help either — consider switching to `-via relay` (TCP websocket) to bypass |
516+
517+
With `-via direct`/VPS, adding `-debug 2` during transfer shows each connection's own `quic cwnd=... inflight=... sent=...pkt lost=...pkt`, plus a summary line at the end (no `-debug` needed for that one) — this is more direct than iperf3 since it reflects the actual connection this transfer is using, not an inference from a separate speed test.
514518

515519
### TCP and UDP: two protocols carried differently on QUIC
516520

‎docs/websocket.md‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,23 +477,25 @@ e2e QUIC 流首部对发送方做一次 uuid 挑战-应答(nonce 绑证书指
477477

478478
#### 单个大文件切块并行传输:`-parallel N`
479479

480-
默认每个文件只占一条连接(`direct` 下是一条 QUIC stream,`relay` 下是一次中继会话),这在单条连接吞吐已经打满链路时够用,但受限于单流拥塞窗口爬升、中继流控窗口这些"单条连接"自身的天花板时就不够了。`-parallel N`(默认 1)让**单个**大文件按字节区间切成最多 `N` 块,各开一条独立连接并行传,两条路径(`-via direct`/`-via relay`)和两个方向(`-send`/`-recv`)都支持:
480+
默认每个文件只占一条连接(`direct`/VPS 盲转发下是一条独立打洞的 QUIC 连接,`relay` 下是一次中继会话),这在单条连接吞吐已经打满链路时够用,但受限于单流拥塞窗口爬升、中继流控窗口这些"单条连接"自身的天花板时就不够了。`-parallel N`(默认 1,硬上限 4——超过的会被截到 4)让**单个**大文件按字节区间切成最多 `N` 块并行传,两条路径(`-via direct`/VPS、`-via relay`)和两个方向(`-send`/`-recv`)都支持:
481481

482482
```bash
483483
anyproxy -send bigfile.zip -to home@example.com -parallel 4
484484
anyproxy -recv home@example.com:backup/bigfile.zip -to /data/in -parallel 4
485485
```
486486

487487
- **只切单个文件,不做多文件并发**。批量 `-send`/`-recv` 多个文件时仍然一个接一个传——切块并行的目的是让单个大文件更快用满带宽,不是让多个文件抢同一份带宽(那样反而会拖长每一个文件的耗时)。
488-
- **文件太小不会被切**:低于 8MiB 的文件永远走单连接路径,`-parallel` 的值被忽略,切块的握手/首部开销在小文件上不划算。
489-
- **失败语义与不切块时一致**:任意一块传输失败(网络错误、校验不过)就让整份文件报错、`.part` 文件被清理,不会留下一个只传对了几块的半成品,也不会自动重试。
488+
- **文件太小不会被切**:低于 8MiB 的文件永远走单连接路径,`-parallel` 的值被忽略,切块的握手/首部开销在小文件上不划算;块数不随文件大小无限增长,最多也就是 4 块(16MiB 以上的文件),再大的文件一样切 4 块,不会因为是几个 GB 就多开洞。
489+
- **失败语义与不切块时一致,但容忍部分连接打不通**:`-via direct`/VPS 下, `N` 条独立连接只要至少打通 1 条就会用打通的那几条继续传(少数候选路径不通是正常现象, 不该因此让整个传输失败);连接建好之后, 任意一块传输失败(网络错误、校验不过)仍然让整份文件报错、`.part` 文件被清理, 不会留下半成品, 也不会自动重试。
490490
- **`-via relay` 下每一块各自协商一次加密会话**(各自独立的随机 salt、独立派生的 AES-256-GCM 密钥),协议本身早就支持"随时开一条新的加密会话",不需要为切块单独改握手。
491+
- **传输过程中的进度行会带上每条连接各自的即时速率**(`conn1: ... conn2: ...`),方便看出是不是有条连接明显拖后腿。
491492

492-
##### `-parallel` 未必总能提速:先搞清楚瓶颈在哪
493+
##### `-parallel` 是不是真的多开了连接,两条路径不一样
493494

494-
**分块用的是同一条底层连接**——`direct` 下 N 个分块是同一条 QUIC 连接上的 N 条 stream,`relay` 下是同一条 websocket(同一条 TCP 连接)上的 N 个中继会话。QUIC/TCP 的拥塞控制(congestion control)都是按**连接**算的,不是按 stream/会话算的,也就是说这 N 条并发路径共用同一个拥塞窗口、在网络设备眼里是**同一个五元组**(同一对源/目的 IP+端口)。如果瓶颈是运营商按流限速、或者链路本身拥塞丢包,`-parallel` 在这种同连接复用的实现下大概率没用——运营商看到的还是"一条流",不会因为应用层多开了几条 stream 就分配更多带宽。
495+
- **`-via direct`/`-via VPS的email`(盲转发打洞):是真的多条独立 QUIC 连接**。每一块各自走一遍打洞信令、各自的 QUIC 握手,拿到的是完全独立的连接,各自维护自己的拥塞窗口——这正是设计初衷:链路有随机丢包时,单条 QUIC 流的拥塞窗口会被反复摁在低位涨不起来(实测案例:cwnd 长期卡在 5~14KB、丢包率 ~5%,吞吐≈cwnd/RTT),而几条相互独立的连接各自维护自己的窗口,聚合吞吐能接近线性回升,这跟迅雷/多线程下载器靠开多条连接提速是同一个原理。真正打洞时开出的 UDP 映射还是复用同一个本地 socket(不需要额外本地端口),但 QUIC 连接本身、以及 VPS 盲转发那一端的绑定,都是各自独立的。
496+
- **`-via relay`(经服务端 B 中继):还是同一条底层连接**。N 个分块是同一条 websocket(同一条 TCP 连接)上的 N 个中继会话,QUIC/TCP 的拥塞控制是按**连接**算的,这 N 条并发路径共用同一个拥塞窗口、在网络设备眼里是**同一个五元组**。如果瓶颈是运营商按流限速、或者链路本身拥塞丢包,`relay` 路径下 `-parallel` 大概率没用;它真正能帮上忙的场景是单条中继会话自身的流控窗口先于网络带宽打满,或者瓶颈其实是 CPU(哈希计算、加解密)而不是网络。想要绕开这条限制、换成真正独立的多连接,请改用 `-via direct` 或 `-via VPS的email`。
495497

496-
`-parallel` 真正能帮上忙的场景,是单条 stream/中继会话自身的流控窗口(不是拥塞窗口)先于网络带宽打满——比如高延迟长距离链路上单流的窗口不够大、或者瓶颈其实是 CPU(哈希计算、加解密)而不是网络。这两种情况开多条并发路径能实打实提速;如果瓶颈是运营商跨网互联质量差或者按流/按账号限速,开再多条也没用。
498+
##### 先用 `iperf3` 诊断一下瓶颈在哪,尤其是 `-via relay`
497499

498500
**用之前先用 `iperf3` 诊断一下,别凭感觉猜**(假设 A 传得慢,C 是收数据的那台,两台要能相互访问):
499501

@@ -521,9 +523,11 @@ iperf3 -c <C的IP> -P 4 -t 20 -R
521523

522524
| 现象 | 结论 |
523525
|---|---|
524-
| 单流慢,多流(`-P 4`)的 `[SUM]` 明显比单流高很多 | 是按流限速/单流窗口撑不满,`-parallel` 值得用 |
525-
| 单流慢,多流 `[SUM]` 也差不多、丢包率高 | 链路本身拥塞(常见于运营商跨网互联质量问题),`-parallel` 收益有限 |
526-
| TCP 结果还行,UDP 明显更差 | 运营商可能单独限制了 UDP,这种情况即使改造成多条独立 QUIC 连接也未必顶得上去,可以考虑改用 `-via relay`(走 TCP 的 websocket)绕开 |
526+
| 单流慢,多流(`-P 4`)的 `[SUM]` 明显比单流高很多 | 是按流限速/单流窗口撑不满,`-parallel` 值得用(`-via direct`/VPS 下尤其明显,因为那边确实是独立连接) |
527+
| 单流慢,多流 `[SUM]` 也差不多、丢包率高 | 链路本身拥塞(常见于运营商跨网互联质量问题),几条独立流各自摊一份丢包也无法完全绕开,`-parallel` 收益有限,但通常还是比单流略好(参考 `-check` 里 QUIC cwnd/丢包统计一节的实测数据) |
528+
| TCP 结果还行,UDP 明显更差 | 运营商可能单独限制了 UDP,`-via direct`/VPS 走的是 QUIC/UDP,即使开多条独立连接也未必顶得上去,可以考虑改用 `-via relay`(走 TCP 的 websocket)绕开 |
529+
530+
`-via direct`/VPS 下传输过程中带 `-debug 2` 能看到每条连接自己的 `quic cwnd=... inflight=... sent=...pkt lost=...pkt`,传完还有一行汇总(不需要 `-debug`);这组数据比 iperf3 更直接——它就是这次传输实际在用的那条连接的真实状况,不是另起一次测速的间接推断。
527531

528532
### TCP 与 UDP:两种协议在 QUIC 上的承载不同
529533

0 commit comments

Comments
 (0)