Repository navigation
fix(xhttp): skip expired entries when picking reusable connections - #3479
Merged
Merged
Conversation
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.
Summary
With
reuse-settings, a connection that has passedh-max-reusable-secskeeps receiving new proxy requests as long as at least one request is still open on it. The documentation says the opposite:— https://github.com/MetaCubeX/Meta-Docs/blob/0ec4dad4496d905b42db3794c49b2ce690a13a5b/docs/config/proxies/transport.en.md?plain=1#L504-L506
Since every new request reuses the expired connection, its open-request count rarely reaches zero, so on a busy client the connection is not rotated at all and
h-max-reusable-secshas no effect. Onlyh-max-request-times, if set, still bounds it. This is most visible when a long-lived connection goes bad (for example, its path starts dropping packets): new requests keep landing on it until the client is restarted or the proxy is reselected. Idle connections are not affected; they are already closed on expiry.Root cause
pickLocked()skips entries that are closed, out ofleftRequests, pastmaxReuseTimesor atmaxConcurrency, but does not look atunreusableAt—mihomo/transport/xhttp/reuse.go
Lines 142 to 162 in 3c0d10a
Expiry is only checked in
cleanupLocked()andrelease(), and both act only whenopenUsage == 0—mihomo/transport/xhttp/reuse.go
Lines 103 to 140 in 3c0d10a
With
max-connectionsset, the expired entry also still counts towardlen(m.entries), soshouldCreateLocked()returns false and the expired entry is picked again —mihomo/transport/xhttp/reuse.go
Lines 164 to 172 in 3c0d10a
Xray-core removes clients past
UnreusableAtfrom the pool before picking, regardless of how many requests are running on them, and closes them once those requests finish — https://github.com/XTLS/Xray-core/blob/f781c68715cda8a896dde7a19966980a15e53071/transport/internet/splithttp/mux.go#L89-L105Change
pickLocked()takes thenowalready computed inGetTransport()and skips entries pastunreusableAt, next to the existingleftRequestsandmaxReuseTimeschecks.What stays the same:
Dialgets oneReuseTransportfromgetTransport()and uses it for the whole session (all POSTs and the GET), so an entry expiring mid-session does not split a session across connections —mihomo/transport/xhttp/client.go
Lines 333 to 339 in 3c0d10a
release()when its last request finishes, and removed bycleanupLocked().download-settings) expire independently, as before.h-max-reusable-secsis unset or0, or whenreuse-settingsis not used.Tests
TestManagerRotateOnReusableSecscloses the first transport before asking for the second one, so it only covers the idle path. The newTestManagerSkipExpiredBusyEntrykeeps the first transport open across expiry and checks that the nextGetTransport()returns a different connection, withmax-concurrency: 16and withmax-connections: 1.3c0d10a: both subtests fail (expected new transport after reusable timeout while busy, got same id 1).go test ./transport/xhttp/,go test -race ./transport/xhttp/,go vet ./transport/xhttp/andgofmt -lare clean (Go 1.27, darwin/arm64).I also ran the XHTTP tests in
listener/inbound(-run XHTTP). On this machine someConcurrentsubtests fail intermittently withconnection reset by peer/broken pipeon loopback, both on Alpha (54 of 1759) and on this branch (43 of 1759), with a different set each run and including subtests that do not usereuse-settings. I did not see a difference attributable to this change.Limitations
While an expired entry is draining it still counts toward
max-connections, so during that time fewer thanmax-connectionsfresh connections may be opened. Xray-core drops it from the count immediately. This does not affect correctness and I left it out to keep the change small.