mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6930] [MERGED] perf(ip-sync): avoid redundant client IP reads and writes #6528
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#6528
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/6930
Author: @HamedSanaei
Created: 10/25/2026
Status: ✅ Merged
Merged: 10/25/2026
Merged by: @MHSanaei
Base:
main← Head:perf/client-ip-sync-6927📝 Commits (6)
9145918perf(ip-sync): avoid redundant client IP reads and writes558d444chore(ci): resolve upstream merge conflict for PR #6930878bf3bMerge branch 'main' into perf/client-ip-sync-692743eebe3test(ip-sync): fold the client IP sync tests into the merge test file1f8a062docs(ip-limit): keep the #6722 marker on the collection-only branch06e0f0eMerge branch 'main' into perf/client-ip-sync-6927📊 Changes
22 files changed (+1273 additions, -247 deletions)
View changed files
📝
.github/workflows/ci.yml(+10 -0)📝
docs/real-client-ip.md(+6 -0)📝
internal/database/model/node_client_ip.go(+25 -4)📝
internal/nodee2e/harness_test.go(+27 -0)📝
internal/nodee2e/node_sync_test.go(+112 -0)📝
internal/web/job/check_client_ip_ban_commit_test.go(+5 -4)📝
internal/web/job/check_client_ip_cas_test.go(+21 -9)📝
internal/web/job/check_client_ip_frozen_ban_test.go(+4 -2)📝
internal/web/job/check_client_ip_job.go(+50 -73)📝
internal/web/job/check_client_ip_job_integration_test.go(+18 -19)📝
internal/web/job/check_client_ip_job_test.go(+14 -14)📝
internal/web/job/check_client_ip_scale_test.go(+137 -7)➕
internal/web/job/check_client_ip_sync_postgres_test.go(+188 -0)📝
internal/web/job/ip_limit_allowlist.go(+4 -2)📝
internal/web/job/ip_limit_allowlist_test.go(+7 -3)➕
internal/web/service/client_ip_sync_postgres_test.go(+36 -0)➕
internal/web/service/client_ip_sync_scale_test.go(+206 -0)📝
internal/web/service/client_ips_prune_test.go(+3 -3)📝
internal/web/service/inbound_client_ips.go(+47 -73)📝
internal/web/service/inbound_client_ips_merge_test.go(+339 -18)...and 2 more files
📄 Description
Summary
Use one canonical
model.ClientIpEntryencoder across client-IP writers and seed remote flat-row CAS merges from the existing preload. Skip identical attribution upserts and merge winning create conflicts instead of losing observations.Why
Remote sync previously re-read each preloaded flat row before its first CAS. The service and scanner also used different ordering, so equivalent observations could produce different blobs, and attribution upserted every reported email even when its canonical bytes were unchanged.
Canonical storage is timestamp ascending, then IP text ascending, with compact
[]for empty input. Encoding does not normalize addresses, filter expiry or stamp timestamps. Writers retain their existing union/filter behavior, differing empty-address treatment, 30-minute retention, GUID buckets and live local-attribution stamping.The service now compares the original stored snapshot on its first CAS, updates the map-backed snapshot on success, and rereads only after a miss: eight attempts and at most seven intervening rereads, with the existing exhaustion error and transaction rollback. Successful inserts update that same map; insert conflicts load and merge the winning row. Scanner first inserts remain record-only; conflicts enter its existing CAS/enforcement path. Its ban-selection, frozen-ban and commit-before-publication control flow is preserved.
The unchanged attribution path does no mutation and cannot overwrite a newer concurrent row. Changed attribution upserts are not redesigned here. No external cache, service, schema migration, setting or API field change is introduced; modern arrays and legacy JSON-string payloads remain accepted.
Closes #6927.
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:
make verifypassed all generation/lint/format/type checks, all Go packages, 214 frontend test files/2,000 tests, full build, Storybook and the docs OpenAPI check. Native package serialization retained every test and race instrumentation; an earlier parallel Windows run hit the unchanged MTProto fake-process test's one-second exit deadline. No MTProto source, assertion or timeout was changed.Regression-first failures captured the redundant first reads, needless identical attribution upserts, create-conflict/duplicate-new observation loss and extra exhaustion reread. Real-SQL callback injections—not mocked CAS results—prove original-raw-snapshot matching, one reread after a forced miss, exact union/max timestamps and eight-attempt/seven-reread rollback. Alternating the real scanner and service on equal-timestamp, historical, allowlisted and charged-live entries preserves exact bytes with zero repeat flat-row writes; advancing observations still persist. Existing retention, stale-GUID, allowlist, frozen-ban and commit-publication regressions remain exercised.
Actual-process HTTP smoke used a newly built panel and a fresh SQLite database: shuffled modern reports and a legacy JSON-string report retained identical canonical bytes; one timestamp advance persisted; two concurrent additional-address pushes retained their exact union/max timestamps. Real master/node E2E passed under both enrollment scopes, including an additional stable sync cycle, offline recovery and separate GUID-attribution buckets seeded only while panels were stopped. This does not claim firewall enforcement or live-Xray collection coverage.
Repeatable measurements
The same fixtures ran before production edits and afterward: 1k/10k/20k clients, each in four reports (
bench-node-0throughbench-node-3), one distinct documentation-range address per node/client. Seed/warmup were excluded. Unchanged reports reused timestamps; advancing reports changed observations; contention submitted independent report slices concurrently and verified the exact four-IP/max-timestamp union. Scanner fixtures covered single/spread inbounds and retained real live-attribution timestamp behavior.Representative PostgreSQL 18 result: 20k clients, four reports. Each cell is before → after; attempted SQL is separate from affected rows.
Tradeoff: preloaded snapshots become stale behind another full changed-report transaction. This worst-case contention fixture performs more failed CAS attempts and is slower. The fix targets unchanged/uncontended work; it does not claim a uniform speedup, and it retains exact union correctness and bounded retries rather than adding a new batching/cache subsystem.
pg_stat_statementsdeltas across both IP-table query shapes, captured inside the live fixtures before teardown:Server execution time aggregates concurrent statements and is not wall-clock cycle latency. Changed-workload WAL has different FPI counts, so those values are raw observations, not checkpoint-normalized improvements. The unchanged run has no affected row mutations but still measured residual WAL: zero row updates is not zero database work/WAL. Scanner live-attribution timestamps that genuinely advance remain real writes. Statistics reset stayed unchanged and deallocations were zero; no cluster-wide reset was used. Supplemental complete-counter scanner baselines used an isolated original-source snapshot with the same measurement fixture, not active-worktree source toggles.
Breaking changes
None. IP JSON field names and timestamp units are unchanged; persisted ordering is now shared and deterministic.
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.