mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6931] [MERGED] perf(traffic): skip unchanged global writes between heartbeats #6529
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#6529
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/6931
Author: @HamedSanaei
Created: 10/25/2026
Status: ✅ Merged
Merged: 10/25/2026
Merged by: @MHSanaei
Base:
main← Head:perf/global-traffic-heartbeat-6928📝 Commits (2)
5509c0dperf(traffic): skip unchanged global writes between heartbeats70f082adocs(traffic): describe persisted global heartbeat timestamps📊 Changes
10 files changed (+699 additions, -38 deletions)
View changed files
📝
.github/workflows/ci.yml(+10 -0)📝
docs/architecture.md(+11 -0)📝
internal/database/model/client_global_traffic.go(+2 -10)📝
internal/nodee2e/node_sync_test.go(+116 -0)📝
internal/web/service/client_paging.go(+1 -1)➕
internal/web/service/global_traffic_heartbeat_test.go(+374 -0)➕
internal/web/service/global_traffic_postgres_test.go(+39 -0)➕
internal/web/service/global_traffic_scale_test.go(+118 -0)📝
internal/web/service/inbound_disable.go(+8 -10)📝
internal/web/service/inbound_traffic_global.go(+20 -17)📄 Description
Summary
Avoid replacing identical
client_global_trafficsrow versions on every push. Directional counter changes—including lower values and zero resets—still persist immediately; identical totals refresh at an inclusive one-hour heartbeat.Why
Every incoming hosted-client total previously updated
up,downandupdated_ateven when usage was unchanged. This creates avoidable row-version/WAL churn on large installations.The existing
(master_guid,email)UPSERT now has an actionWHEREpredicate, not a conflict-target predicate. Explicit NULL branches repair legacy nullable counters/timestamps; any directional difference writes immediately. Rows insert immediately when missing. Known-email filtering, duplicate-email last-wins behavior, queued writing, one transaction and 200-row batches remain unchanged.Ingestion and freshness readers use the existing GORM clock. The persisted-row freshness window remains inclusive at 24 hours; local-only counters/full-list snapshots, per-master componentwise-MAX read overlays, downstream forwarding, actual reset/delete cleanup and quota-disable subscription invalidation remain intact. Ordinary ingestion does not invalidate subscription settings caches.
Bounded liveness tradeoff: after a master stops, coarsened timestamps can make its idle totals expire up to one hour earlier relative to its final unchanged push. Active hourly refresh leaves roughly 23 hours of missed-push margin. This is an internal fixed interval, with no new setting, clock abstraction, cache, service, schema or API change.
Closes #6928.
Type of change
Areas affected
How was this tested?
Passed on Windows amd64, Go 1.27.3, CGO/GCC, Node 26.12.1/npm 11.20.0, SQLite 3.53.4, and disposable PostgreSQL 18.6 and 16.15:
The full gate passed generation/lint/format/type checks, all Go tests, 214 frontend test files/2,000 tests, full build, Storybook and the docs-vs-panel OpenAPI check. Package serialization retains all tests and race instrumentation.
Regression-first evidence: before the production change, the new boundary test observed an affected row before one hour when it required zero. Tests use real databases and synchronized affected-row counts after
gorm:create, not merely unchanged timestamps. The atomic fake GORM clock is installed on the global DB after stopping/draining the writer and restored only after another stop/drain.Shared SQLite/PostgreSQL cases cover before/exactly-at/after the heartbeat, up-only/down-only increases and decreases, zero resets, legacy NULLs, 405-client mixed batches, unknown and duplicate emails, 48 hours of active unchanged pushes versus an absent-master control, missed cycles/24-hour absence/recovery, inclusive display/quota/paging freshness, real attached-client disable plus settings/cache invalidation, reset/delete/real
CreateOnerecreation and downstream forwarding. Same/different-master concurrency runs both queued and inline; same-master results are complete submitted pairs and an ordered final lower reset persists. Existing subscription-cache same-output/fresh-inbound/host-on-hit tests remain in the full suite.A newly built disposable panel also passed HTTP smoke: local counters 10/20 stayed unchanged; repeated 900/800 pushes kept the side-row timestamp; slim/detail displayed the overlay while full-list snapshots remained local; 50/60 then 0/0 overwrote the side row immediately; an identical push refreshed a deliberately aged row.
Real master/node scenarios and a genuine root→middle→leaf chain passed, with 90-second waits for unchanged 30-second global-push cadence. The chain covers combined usage, lower totals, real producer resets, deletion/recreation at each independently seeded origin and offline recovery. The fixture keeps one UUID/SubID identity across origins and does not assume that resetting a global overlay erases independently produced local usage. Existing two-panel root reset/delete scenarios remain unchanged. It does not claim live-Xray or firewall coverage; 48-hour freshness is proven with deterministic database time, not wall-clock sleeps.
Repeatable measurements
Before/after workloads used 6,708 hosted local traffic rows and 240 simulated 30-second ticks over two hours. Initial local/global seed and one warmup push were outside timing. Unchanged values stay fixed; sparse change selects
(clientIndex+tick)%100 == 0; all-change increments every client's directional counters each tick; reset overwrites all counters with zero at tick 120 and stays idle. Quotas/expiry are disabled in these ingestion fixtures.Both SQLite and PostgreSQL produced these affected-row counts:
Statements/batches are not reduced. Conflict checks may still lock rows and advance sequences; fewer affected row versions is not zero database work or zero WAL.
Supplemental PostgreSQL 18 runs controlled checkpoint conditions in isolated original/changed source snapshots with identical fixtures. Both used
full_page_writes=on,max_wal_size=8GB,checkpoint_timeout=1h, one warmup and an untimedCHECKPOINTbefore each workload. Timed/requested checkpoint counters were unchanged during every measured interval. Temporary settings were restored to their original 1GB/5-minute values; this instrumentation/configuration is not added to committed tests or production.The benefit is unchanged/sparse/reset ingestion; all-change showed no benefit and had higher measured WAL. No universal speedup or statement-count reduction is claimed. Server
calls,rows, execution time, WAL records/FPI/bytes and buffer counters were captured before fixture teardown across all INSERT batch shapes. Statistics reset was unchanged anddealloc=0; no cluster-wide statistics reset was used. Initial supplemental intervals containing checkpoints were rejected rather than counted as controlled evidence.Review follow-up
Approved documentation-only commit
70f082a6931b656410028f22f1f00d08b2fff051corrects the model's stale every-push comment:UpdatedAttracks a persisted change or hourly unchanged heartbeat. Local-counter separation, delta accounting, MAX folding and reset propagation remain documented within the two-line comment limit. Production fix5509c0dd7d3e38ce57db24b3bd6463adc7a6f937remains unchanged; no published history was rewritten. Model-packagegolangci-lintand the six SQLite heartbeat regression cases passed again; no executable code or schema changed.Breaking changes
None. The bounded heartbeat/freshness tradeoff is described above and in the existing architecture documentation.
Checklist
go build ./...and the test suite pass locally.npm run lint,npm run typecheck, andnpm run buildpass. (Exercised by the full gate; backend-only PR.)🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.