[PR #5798] [CLOSED] perf(database): batch traffic writes #5990

Closed
opened 2026-11-02 04:05:33 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5798
Author: @rqzbeh
Created: 7/4/2026
Status: ❌ Closed

Base: main ← Head: perf/database-hardening-v3


📝 Commits (3)

  • e78c23b perf(database): batch traffic writes
  • 227f1f8 fix(database): address traffic batching review
  • 7f394fa fix(database): make WAL backups and PG batches safe

📊 Changes

15 files changed (+751 additions, -134 deletions)

View changed files

📝 .gitignore (+1 -1)
📝 README.md (+2 -0)
📝 docs/architecture.md (+4 -1)
📝 internal/database/db.go (+140 -9)
📝 internal/database/db_settled_test.go (+68 -0)
📝 internal/database/dialect.go (+202 -1)
➕ internal/database/dialect_test.go (+86 -0)
📝 internal/web/service/client_bulk.go (+3 -3)
📝 internal/web/service/client_groups.go (+2 -2)
📝 internal/web/service/client_inbound_apply.go (+4 -4)
📝 internal/web/service/client_lookup.go (+11 -3)
📝 internal/web/service/inbound_client_traffic_test.go (+41 -0)
📝 internal/web/service/inbound_node.go (+4 -2)
📝 internal/web/service/inbound_traffic.go (+181 -106)
📝 internal/web/service/server.go (+2 -2)

📄 Description

Summary

Optimizes database hot paths for traffic accounting and client lookups across SQLite and PostgreSQL. Adds SQLite WAL mode, dialect-aware batch updates, shorter traffic transactions, WAL-safe backups, targeted settings writes, and DB-specific indexes.

This is a fresh PR replacing closed PRs #5795 and #5796.

Why

Traffic polling currently grows into many per-client updates and keeps slow renew/disable work in the same write transaction. JSON LIKE scans also bypass indexed client columns. This lowers lock time and query cost as client count grows while keeping legacy data readable and raw SQLite backups complete under WAL.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

  • go test ./internal/database ./internal/web/service
  • make verify

New/updated coverage:

  • SQLite batch increment helper behavior and bind-variable chunk sizing in internal/database/dialect_test.go
  • SQLite WAL pragma, raw .db checkpoint backup safety, and post-migrate remark/tg_id index creation in internal/database/db_settled_test.go
  • Legacy tgId lookup fallback in internal/web/service/inbound_client_traffic_test.go

PostgreSQL live tests were not run locally because no XUI_DB_TYPE=postgres + XUI_DB_DSN was configured.

Screenshots / recordings

N/A

Breaking changes

None.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5798 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 7/4/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/database-hardening-v3` --- ### 📝 Commits (3) - [`e78c23b`](https://github.com/MHSanaei/3x-ui/commit/e78c23b4ad0fd7545ebdc09151a2df6d6baf8d5a) perf(database): batch traffic writes - [`227f1f8`](https://github.com/MHSanaei/3x-ui/commit/227f1f8c701ce65cc913166e359dc308554d6ef4) fix(database): address traffic batching review - [`7f394fa`](https://github.com/MHSanaei/3x-ui/commit/7f394fa53add1e514b627712c13842dd1e490d11) fix(database): make WAL backups and PG batches safe ### 📊 Changes **15 files changed** (+751 additions, -134 deletions) <details> <summary>View changed files</summary> 📝 `.gitignore` (+1 -1) 📝 `README.md` (+2 -0) 📝 `docs/architecture.md` (+4 -1) 📝 `internal/database/db.go` (+140 -9) 📝 `internal/database/db_settled_test.go` (+68 -0) 📝 `internal/database/dialect.go` (+202 -1) ➕ `internal/database/dialect_test.go` (+86 -0) 📝 `internal/web/service/client_bulk.go` (+3 -3) 📝 `internal/web/service/client_groups.go` (+2 -2) 📝 `internal/web/service/client_inbound_apply.go` (+4 -4) 📝 `internal/web/service/client_lookup.go` (+11 -3) 📝 `internal/web/service/inbound_client_traffic_test.go` (+41 -0) 📝 `internal/web/service/inbound_node.go` (+4 -2) 📝 `internal/web/service/inbound_traffic.go` (+181 -106) 📝 `internal/web/service/server.go` (+2 -2) </details> ### 📄 Description ## Summary Optimizes database hot paths for traffic accounting and client lookups across SQLite and PostgreSQL. Adds SQLite WAL mode, dialect-aware batch updates, shorter traffic transactions, WAL-safe backups, targeted settings writes, and DB-specific indexes. This is a fresh PR replacing closed PRs #5795 and #5796. ## Why Traffic polling currently grows into many per-client updates and keeps slow renew/disable work in the same write transaction. JSON LIKE scans also bypass indexed client columns. This lowers lock time and query cost as client count grows while keeping legacy data readable and raw SQLite backups complete under WAL. ## Type of change - [ ] Bug fix - [ ] New feature - [x] Refactoring (no behavior change) - [x] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [x] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? - `go test ./internal/database ./internal/web/service` - `make verify` New/updated coverage: - SQLite batch increment helper behavior and bind-variable chunk sizing in `internal/database/dialect_test.go` - SQLite WAL pragma, raw `.db` checkpoint backup safety, and post-migrate remark/tg_id index creation in `internal/database/db_settled_test.go` - Legacy `tgId` lookup fallback in `internal/web/service/inbound_client_traffic_test.go` PostgreSQL live tests were not run locally because no `XUI_DB_TYPE=postgres` + `XUI_DB_DSN` was configured. ## Screenshots / recordings N/A ## Breaking changes None. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:05:33 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5990
No description provided.