mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #7008] [Performance]: Reduce repeated PostgreSQL work in global-aware client depletion checks #4616
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#4616
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/7008
Summary
Even after the previous fix for #5392, an active multi-node PostgreSQL deployment spends substantial time repeatedly evaluating the client expiry/quota predicate against thousands of traffic rows. Almost all executions find zero clients to disable, but the global-usage-aware
OR EXISTSpath remains relatively expensive.This is a follow-up, performance-only optimization, not a request to relax quota enforcement, change the polling schedule, or reimplement the already-fixed empty-global-table shortcut.
Confirmed implementation
internal/web/web.go:XrayTrafficJobruns atcadenceXrayTraffic = "@every 5s".internal/web/job/xray_traffic_job.go:RuncallsInboundService.AddTraffic.internal/web/service/inbound_traffic.go:addTrafficLockedcommits incoming traffic, then performs lifecycle maintenance in a separate transaction:autoRenewClients,disableInvalidClients, anddisableInvalidInbounds.internal/web/service/inbound_disable.go:depletedCondfirst checks whether anyclient_global_trafficsrow is fresh within 24 hours. If yes, it selectsdepletedClientsCond, combining local quota exhaustion, expiry, and a correlated EXISTS against global usage. On empty/stale global data it uses the cheaper local-only predicate (the #5392 improvement).Relevant SQL shape, simplified for readability:
The global path is essential: a node can exceed the global quota even when its local counters are below the limit. Do not replace this with local-only accounting.
Production measurements
PostgreSQL 18, 2 vCPUs, ~9,774
client_trafficsrows and ~6,857client_global_trafficsrows; source:pg_stat_statements, from 2026-10-28 15:16:54 UTC through approximately 22:08:42 UTC.The recurrent
SELECT count(*) FROM client_global_traffics WHERE updated_at >= ? LIMIT ?shape also registered 7,242 calls and ~31,685 ms cumulative execution time. Identify all its callers before attributing the entire count to depletion checking.The main predicate commonly returns no rows but still costs ~55 ms per call. This is a repeatable SQL hot path, not proof that each run wastes CPU: determining that no one is depleted is required. Query-level execution time is also not the same as OS CPU time.
Existing indexes (do not duplicate blindly)
client_traffics: unique(email),(inbound_id),(expiry_time,reset).client_global_traffics:(email), unique(master_guid,email).Proposed approach
EXPLAIN (ANALYZE, BUFFERS)on safe read-only queries; compare enabled/disabled populations, expiration distributions, and number of fresh masters.Correctness requirements
Acceptance criteria
EXPLAIN (ANALYZE, BUFFERS)on matched PostgreSQL fixtures, showing row/plan differences and query time.pg_stat_statementsdeltas (matched observation windows) if available.Prior art / boundaries
I can implement and benchmark a small focused PR once the direction is agreed.