mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 19:11:53 +07:00
[GH-ISSUE #7007] [Performance]: Reduce recurring full inbound snapshots during 5-second node traffic sync #4617
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#4617
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?
Originally created by @HamedSanaei on GitHub (Oct 28, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/7007
Summary
On PostgreSQL-backed multi-node deployments, the five-second node traffic synchronization path requests the full inbound list from each node, even though most inbound configuration/identity data is unchanged from the previous tick. This causes repeated large database reads, identity JOINs, JSON decoding/encoding, and network transfers while the master mostly needs fresh counters, online state and enough information to safely reconcile lifecycle/structural changes.
This issue proposes measuring and reducing snapshot read amplification at the node-sync boundary. It is not a request to reimplement the already-merged client identity batching from #6926, change the five-second traffic cadence, or weaken node reconciliation correctness.
Verified code path (current
main)internal/web/web.go:cadenceNodeTraffic = "@every 5s".internal/web/job/node_traffic_sync_job.go:NodeTrafficSyncJob.RuncallssyncOnefor each enabled/online node;syncOnecallsrt.FetchTrafficSnapshot(ctx)and thenFilterNodeSnapshot,SnapshotHasUnadoptedInboundsandSetRemoteTraffic.internal/web/runtime/remote.go:Remote.FetchTrafficSnapshotmakesGET panel/api/inbounds/listand unmarshals the entire response intosnap.Inbounds; the method also calls online-by-GUID, last-online and active-inbounds endpoints.internal/web/controller/inbound.go:getInboundscallsInboundService.GetInbounds.internal/web/service/inbound.go:GetInboundsperforms a full inbound query withPreload("ClientStats"), followed byenrichClientStatsand annotations.backfillClientStatsis already batched since #6926, but the entire path still runs on each request.The full response is important to some existing consumers (node adoption, structural sync, backward-compatible snapshot behavior). Do not simply remove settings/identity fields from the existing endpoint.
Reproducible production evidence
PostgreSQL 18, 2-vCPU Linux VPS, about 14,774 rows in
clients, 11,372 rows inclient_inbounds, 9,774 rows inclient_traffics, and 8 inbounds. The combinedinbounds.settingspayload is about 6.00 MiB (largest 2.58 MiB).pg_stat_statementswas enabled/reset at 2026-10-28 15:16:54 UTC; measurements below were sampled around 2026-10-28 22:08:42 UTC (roughly 6 h 52 min). These are database-wide cumulative query-shape counters, not endpoint-specific tracing:SELECT * FROM client_traffics WHERE email IN (...)clients JOIN client_inbounds ... WHERE inbound_id IN (...)SELECT * FROM client_traffics WHERE inbound_id IN (...)SELECT * FROM inbounds WHERE user_id = ? ORDER BY idThe last three shapes each run ~4,800 times in the sample, consistent with a ~5-second recurring operation. One identity JOIN returns an average of ~11.4k rows per execution. Together with the email-IN traffic shape they account for ~60% of the recorded
xuiSQL execution time. However,pg_stat_statementsaggregates all callers of a normalized SQL shape, so attribution to node traffic snapshots must be verified with targeted instrumentation. SQL elapsed time is not identical to CPU time.Existing indexes include
client_inbounds(inbound_id)andclient_traffics(inbound_id); this is not a request to add duplicate indexes.Proposed investigation / implementation direction
FetchTrafficSnapshot, inbound list loading, stats enrichment andSetRemoteTraffic: SQL calls, rows, DB time, JSON marshal/unmarshal time, bytes transferred, Go allocations, end-to-end p50/p95 and per-node rate. Avoid logging client credentials or sensitive payloads./panel/api/inbounds/listsemantics for existing API users.Critical correctness invariants
justPushedguard against re-applying a lagging snapshot).Acceptance criteria
Related work / non-duplication
backfillClientStats; this issue targets the frequency and scope of the full snapshot after that optimization.I am willing to implement this improvement and submit a PR after maintainer feedback.