Repository navigation
fix(speedcap): count a user's download when the client side takes it - #15
Draft
Free-Guy-IR wants to merge 4 commits into
Draft
Free-Guy-IR wants to merge 4 commits into
Free-Guy-IR wants to merge 4 commits into
Conversation
The user downlink counter counted bytes when the outbound handed them to the dispatcher, before the speed cap's bucket wait and before the inbound wrote them to the client. When a client closed a capped download midway, the read parked in the bucket wait was counted and never sent: 65,536 bytes in every measured abort. On the Dispatch path the bytes still in the downlink pipe and the batch whose write failed were counted as well. - Dispatch path (Shadowsocks, Trojan, VMess, mux sub-sessions): the downlink pipe reader counts a batch when the inbound comes back for the next read, drops the batch it holds when the inbound interrupts it, and at the end of the connection counts a last batch only if the stream finished and the pipe is drained. - DispatchLink path (VLESS, socks, HTTP CONNECT): the user's SizeStatWriter counts after the bucket wait and after the client writer returned without an error. - Uplink is unchanged: it is counted when it arrives from the client. - xray-stats-register-race.patch keeps its changes; only the context of its dispatcher hunks follows the new lines. Evidence on Linux, a throwaway container on the dev box, with the scenario test binary built from this tree (8 Mbit/s cap, raw VLESS and Trojan clients that read part of an endless download, drain and reset): - xray from the previous patch: 8 of 8 aborts per protocol over-counted 65,536 bytes (VLESS 524,288 bytes, 10.000%; Trojan 524,288 bytes, 9.861%); 48 cap changes racing 63 aborts: +4,784,128 bytes. - xray from this patch: 0 bytes on all 16 aborts; 0 bytes in the interleaving run. macOS gave the same per-abort numbers before and after. Not changed: bytes the node's TCP send buffer and a relay still hold when a client aborts are counted as before.
NodeVersion goes 0.6.29 to 0.6.30, one patch step from the highest 0.6 value on any branch of the fork on GitHub (0.6.29 on main, feat/003-speed-cap and ci-003-speed-cap; checked on 2026-12-03), for the node whose core counts a user's download when the client side takes it. TestNodeVersionIsPinned failed against the 0.6.29 constant and passes after the bump. The three upstream-waiver rows that cite the constant follow.
2e4c3db dropped the batch an inbound held when the inbound interrupted the downlink pipe, and counted a held batch at the end of the connection only if the stream had finished and the pipe was empty. Both rules lost bytes that had reached the client: - a client that resets right after the last byte makes the inbound interrupt the pipe before its copy loop reads again, so the last delivered batch was never counted (Linux, 600 Trojan downloads of 300,000 bytes: 170,414,400 counted for 180,000,000 received); - a plain HTTP proxy stops reading after the response, so its last batch waited for an origin that keeps its connection open (Linux, 8 responses of 20,082 bytes: 120,492 counted for 160,656). Now a batch the inbound took from the pipe is counted when the inbound enters its next read, whatever happened in between, and is never counted if the inbound does not come back after it (its write failed). Interrupt is the upstream one again and the pipe keeps no context hook. The plain HTTP handler settles the batch it holds after it wrote the response. Linux, throwaway container, scenario test binary from this tree: - 0.6.29 core: the capped abort test over-counted 65,536 bytes on each VLESS abort and between 65,536 and 1,245,184 bytes on each Trojan abort (3,932,160 bytes, 74.4%, over 8 aborts); the reset-after-last-byte and plain HTTP tests counted exactly. - 2e4c3db: the abort tests counted exactly; the two new tests lost the bytes above. - this commit: all four tests count to the byte. The 65,536 bytes per abort in 2e4c3db's message are the smallest over-count seen on the 0.6.29 core, not the only one.
…in's response From the second review of 03ac8fe: - The plain HTTP handler also settled the batch it held when it answered 503 itself because the origin's response did not parse. Six broken origin responses of 20,013 bytes, the client got only xray's 503 (594 bytes): 120,078 bytes counted before, 0 now (macOS, review test with XRAY_RAY_BUFFER_SIZE=1; the 0.6.29 core also counted 120,078). Data an origin sends after Content-Length is still counted, as on 0.6.29. - The abort tests drained the client socket right after a read whose deadline had passed, so the drain never read; the deadline is cleared first now. - docs/upstream-parity.md says the stats register race patch's dispatcher hunks have the speed cap patch's code as context.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CI only for now; do not merge until the rollout is approved.
A user's download was counted when the outbound handed bytes to the dispatcher: before the speed cap's bucket wait and before the inbound wrote them to the client. When a client closed a download midway, the read parked in the bucket wait, the bytes left in the downlink pipe and the batch whose write failed were counted but never sent.
Changes (all in
patches/, plus guards, test lines and the version):Interruptis the upstream one.Content-Lengthis still counted, as on 0.6.29.SizeStatWritercounts after the bucket wait and after the client writer returned without an error.xray-stats-register-race.patchkeeps its changes; only the context of its dispatcher hunks follows the new lines.NodeVersion0.6.30.Tests with real xray processes, run on Linux in a throwaway container (scenario test binary built from this tree):
Not changed: bytes that the node's TCP send buffer and a relay still hold when a client aborts are counted as before; for mux over VMess, Trojan or Shadowsocks a sub-session batch counts once it is in the outer mux pipe; a write that fails midway is not counted at all, even the part the kernel took.