mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 19:11:53 +07:00
[PR #6916] [MERGED] perf(runtime): pool the local xray gRPC connection #6519
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#6519
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/6916
Author: @muditagrawal2007
Created: 10/24/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei
Base:
main← Head:perf/local-xray-grpc-pool📝 Commits (6)
1ee1c73perf(runtime): pool the local xray gRPC connection5a48c92Merge remote-tracking branch 'origin/main' into pr/691676d82festyle(runtime): fit the pooled-client comments in two linescc3d140Merge remote-tracking branch 'origin/main' into perf/local-xray-grpc-pool2d412f6fix(runtime): drop the pooled client when the core is unreachable57cd898refactor(runtime): fold the core-restart test into the pool tests📊 Changes
3 files changed (+186 additions, -4 deletions)
View changed files
📝
internal/web/runtime/local.go(+17 -4)➕
internal/web/runtime/local_api.go(+52 -0)➕
internal/web/runtime/local_api_test.go(+117 -0)📄 Description
Summary
The local runtime opened a gRPC connection to the Xray core for every operation and closed it again afterwards. A bulk client delete calls the runtime once per client, so it paid a TCP + HTTP/2 handshake per client.
The connection is now pooled per API port.
Why
internal/web/runtime/local.go— every local inbound and user operation went through this:The removal loops call the runtime once per email —
internal/web/service/client_bulk.go:1216,internal/web/service/client_inbound_apply.go:236and:1225— so a bulk delete did onegrpc.NewClient+ handshake per client, all serialised behind the runtime's mutex.The fix keeps one client per API port, mirroring the node HTTP pool that already exists in this package (
internal/web/runtime/tls_client.go:157-231):A core restart can move the API port (
internal/xray/process.go:580recomputes it from the config), so the pool follows the port and closes the client it replaces rather than leaking it.The hazard this deliberately avoids
xray.XrayAPI.StatsLastValuesis written without a lock (internal/xray/api.go:846), so a second concurrent reader corrupts it — and a concurrent map write is a fatal runtime throw thatcron.Recovercannot catch (the panel's own comment atinternal/web/web.go:534-538describes exactly this). The pooled client is therefore used only for inbound and user operations; the traffic poll keeps its own client, as before.Evidence — red before, green after
Type of change
Areas affected
How was this tested?
Four new tests in
internal/web/runtime/local_api_test.go:TestLocalWithAPIKeepsTheClientOpen— after a call, the client must still be connected.XrayAPI.Close()nils the exported stub pointers, so a closed client is observable without reaching into the core.TestLocalWithAPIReusesOneClient— three calls must hand out the same client.TestLocalWithAPIEvictsTheClientWhenThePortChanges— a moved API port must not reuse the old client, and the old one must be closed.TestLocalWithAPIWithoutARunningCoreFails— the "local xray is not running" path is unchanged.Commands run locally:
Notes for the reviewer
UpdateInbound(remove + add) stays atomic against every other local operation — that guarantee is independent of the connection.ClientConnis safe for concurrent use, and the pooled client is only ever touched under the runtime's mutex, so the pool adds no new concurrency.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.