mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6548] [MERGED] perf(nodes): reuse one pooled client per node instead of rebuilding it #6346
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6346
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6548
Author: @BlindMaster24
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24
Base:
main← Head:fix/node-sync-scale📝 Commits (3)
e37af92perf(nodes): reuse one pooled client per node instead of rebuilding it31bc136perf(nodes): keep one client per node in the pooled cache7bc0588style(nodes): keep the eviction comment inside the two-line cap📊 Changes
2 files changed (+219 additions, -26 deletions)
View changed files
📝
internal/web/runtime/tls_client.go(+92 -26)📝
internal/web/runtime/tls_client_test.go(+127 -0)📄 Description
What this fixes
A 100-node panel polls each enabled node continuously: heartbeat and traffic sync are
@every 5scompile-time constants (web.go:303-304), so one node sees ~6 requests per 5 s tick type, ~153 HTTPS requests/second across 100 nodes. Two things made that far more expensive than it needs to be.A client per tick.
probe()(service/node.go:1274) calledruntime.HTTPClientForNodedirectly. Forskip,pinandmtlsmodes that function built a newhttp.Clientwith a newhttp.Transporton every call — empty pool, so every heartbeat paid a full TCP + TLS handshake, and the CPU in a TLS handshake is server-certificate signature verification, which is exactly what shows up in monitoring.A pool too small for the fleet.
MaxIdleConns: 64with one node per host: 100 hosts cannot fit in 64 global idle slots, so Go closed the excess and those connections re-handshook even where the client was already cached.What was already fine, so this does not overclaim: the sync path went through
Manager.RemoteFor, which cachesRemoteper node, soRemote.httpClient()'ssync.Oncealready pooled there; andverifymode plus plainhttpgot the shareddefaultNodeHTTPClient. The cost was the heartbeat probe (all non-verify nodes), the ad-hocruntime.NewRemote(n, nil)calls inservice/node.go:728, and the global idle cap on every path.The change
HTTPClientForNodenow caches the client per node identity —id, mode, scheme, address, port, pinned cert— with the proxy URL as a variant of it, because the heartbeat reaches a node directly while the sync path reaches it through its egress. A hit returns the pooled client.CloseIdleConnections) and dropped: no request can be served by a client built for a trust decision that no longer applies.InvalidateMasterClientConnectionsstill works unchanged — the credential-rotating transport observes the epoch on its nextRoundTrip, which is what its doc comment already promised.512global /8per host, in onenewNodeTransporthelper that replaces the same struct literal in three places. Arithmetic: one idle connection per node host, plus the sync and push requests, so 512 covers a 200-node fleet with room for concurrency; the memory is a few KB per idle connection.buildNodeHTTPClient; the now-unreachableverifyfallthrough at the end of the old function is gone, since the wrapper owns that decision.Rejected: caching keyed by node id alone, which would serve a
skipclient to a node the operator has since switched topin, and would hand a direct client to a proxied request.Red first, with the exact output
TestHTTPClientForNodeReusesOneConnectionAcrossCallsruns two calls against anhttptestTLS server countinghttp.StateNewconnections. Probe — build a client per call (the pre-change behaviour):Green after the change, and
TestHTTPClientForNodeRebuildsWhenNodeIdentityChangespins the cache key the security argument rests on: a node switched topin(or given a proxy) must never be served the client built to skip verification.The pre-existing tests in the same file still pass unchanged —
HTTPClientForNodeVerifyShared,HTTPClientForNodeMTLSRebuildsTLSConfigAfterCredentialInvalidation,HTTPClientForNodeProxyMTLSRebuildKeepsProxyAndNewCredential,HTTPClientForNodePinInvalid,HTTPClientForNode_ProxyPinPreservesPinEnforcement,HTTPClientForNode_ProxyVerifyNoPin. That is the check that the cache did not weaken credential rotation or pin enforcement.Gate
go test ./internal/web/runtime— green (local run through a-overlaythat supplies a pure-Go SQLite driver; the tree'sgo.mod/go.sumare unchanged).gofmt -landgo veton the package — clean.go-test,golangci,race.What I did not do, and why
http.DefaultTransport's caps (100 global / 2 per host), sincenetproxyowns that transport and only egress-tagged nodes use it. Sequential per-node ticks need one connection, so the per-host cap is not the constraint there; raising it would change every othernetproxycaller.GetAllInboundClientIps()is still called inside the per-node loop (node_traffic_sync_job.go:450) — one full table read per node per tick. Same family, different mechanism (DB reads and pushed bytes rather than handshakes) and a separate diff; it belongs in its own PR.panel/api/inbounds/listwith settings, streamSettings, sniffing and client stats, although it rarely changes. A conditional request on the existing route (node answers304when the body would be identical, master keeps the previous snapshot) would cut the bulk of the 1.8 KB average response. I have not written it — it changes master↔node wire behaviour and version compatibility.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.