Repository navigation
XHTTP client: Define Request.GetBody() for packet-up so h2 can replay after GOAWAY - #6632
Merged
Merged
Conversation
Member
|
这个重试看起来还行 |
Fangliding
approved these changes
Aug 25, 2026
Member
|
涉及到了之前关注过的 Go HTTP client 会不会自动缓存 body 并在没收到 200 response 时自动重发的问题(理论上对于 POST 不该有这种行为),此外还涉及客户端内存占用问题 现在看起来的确没有无脑自动重新 POST,而是需要自定义 GetBody() 来决定?不过它是否只适用于 h2 GOAWAY?此外这样改的话确实解决了一个边缘的内存泄漏问题,代价是多 copy 了一次,以后可以优化掉,并且占用内存的时间看起来会更长一些,但要考虑重发的话也只能这样了;仍然不确定的是 Go HTTP client 内部的 body 何时释放,期待你的讨论 |
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.
Problem
In
packet-upmode every chunk is sent as a separate POST carrying a sequencenumber.
FillPacketRequestassignsrequest.Bodyby hand, after the requestwas created with a nil body, so
Request.GetBodyis never populated.When the peer sends GOAWAY while a request body is being written,
golang.org/x/net/http2cannot replay the request and gives up with:For
packet-upthis is not a retriable loss of bytes. Thatseqneverarrives, and the server's reassembly queue waits for it forever — it matches
nextSeqexactly and never skips (upload_queue.go) — so the session is torndown.
dialer.gointerrupts the upload pipe on anyPostPacketerror, whichkills the uplink outright.
Fix
Copy the payload into a plain slice once, release the pooled buffers
immediately, and serve both
BodyandGetBodyfrom that copy. Theelsebranch of the same function already did exactly this copy, so it is hoisted and
shared instead of duplicated.
Duplicate
seqvalues produced by a replay are harmless:uploadQueuesilently drops any packet whose
Seqis belownextSeq.Side effect: this also closes a buffer-pool leak.
io.NopClosersuppressedMultiBufferContainer.Close(), so when a request was aborted mid-write theunread buffers were never returned to the pool. Release is now explicit and
unconditional.
Evidence
Measured against Timeweb CDN, which closes
each HTTP/2 connection after ~200 requests.
A minimal probe reproducing the pre-patch code path (
Bodyassigned by hand,GetBodyleft nil), 8 concurrent requests, 400 KB bodies, 5 minutes, routedthrough the Xray tunnel itself:
A patched client carrying that same traffic in the same window, instrumented to
log every
GetBodycall:cannot retry, 0failed to send uploadover ~530 MBuploaded
Under test,
GetBody()returns the full payload even afterBodyhas alreadybeen read to completion, and the
splithttpsuite passes unchanged.Cost
One copy of the chunk per in-flight request, bounded by
scMaxEachPostBytes.The copy is released by GC once the request completes.
GetBodyitself is onlycalled on the retry path, so there is no cost in the normal case.