[PR #5800] [CLOSED] perf(database): batch traffic writes #5993

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

📋 Pull Request Information

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

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


📝 Commits (4)

  • ac2a366 perf(database): batch traffic writes
  • b310352 fix(database): address traffic batching review
  • 8754733 fix(database): make WAL backups and PG batches safe
  • 1309718 fix(database): address WAL review followups

📊 Changes

16 files changed (+864 additions, -140 deletions)

View changed files

📝 .gitignore (+1 -1)
📝 README.md (+2 -0)
📝 docs/architecture.md (+4 -1)
📝 install.sh (+17 -6)
📝 internal/database/db.go (+145 -9)
📝 internal/database/db_settled_test.go (+68 -0)
📝 internal/database/dialect.go (+202 -1)
➕ internal/database/dialect_test.go (+177 -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, DB-specific indexes, and etckeeper ignores for SQLite WAL sidecars.

This is a fresh PR replacing closed PRs #5795, #5796, and #5798. It includes the latest review follow-ups from #5798.

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 (464 tests)
  • PATH="$HOME/go/bin:$PATH" make verify

New/updated coverage:

  • SQLite batch increment helper behavior and bind-variable chunk sizing in internal/database/dialect_test.go
  • Opt-in PostgreSQL batch traffic and index creation/repair coverage in internal/database/dialect_test.go when XUI_DB_TYPE=postgres and XUI_DB_DSN are set
  • 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's 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/5800 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 7/4/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/database-hardening-v4` --- ### 📝 Commits (4) - [`ac2a366`](https://github.com/MHSanaei/3x-ui/commit/ac2a366e3a8cbd6dc755d1e2ee5b54fe68daa7fa) perf(database): batch traffic writes - [`b310352`](https://github.com/MHSanaei/3x-ui/commit/b3103529683f7cb22dc9d520778d19ac420d2a17) fix(database): address traffic batching review - [`8754733`](https://github.com/MHSanaei/3x-ui/commit/87547339250bc4711657d46f4322bda37fa2f5c4) fix(database): make WAL backups and PG batches safe - [`1309718`](https://github.com/MHSanaei/3x-ui/commit/130971837c225340f67b9b904fe0bc55d4c4b10d) fix(database): address WAL review followups ### 📊 Changes **16 files changed** (+864 additions, -140 deletions) <details> <summary>View changed files</summary> 📝 `.gitignore` (+1 -1) 📝 `README.md` (+2 -0) 📝 `docs/architecture.md` (+4 -1) 📝 `install.sh` (+17 -6) 📝 `internal/database/db.go` (+145 -9) 📝 `internal/database/db_settled_test.go` (+68 -0) 📝 `internal/database/dialect.go` (+202 -1) ➕ `internal/database/dialect_test.go` (+177 -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, DB-specific indexes, and etckeeper ignores for SQLite WAL sidecars. This is a fresh PR replacing closed PRs #5795, #5796, and #5798. It includes the latest review follow-ups from #5798. ## 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 - [x] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? - `go test ./internal/database ./internal/web/service` (464 tests) - `PATH="$HOME/go/bin:$PATH" make verify` New/updated coverage: - SQLite batch increment helper behavior and bind-variable chunk sizing in `internal/database/dialect_test.go` - Opt-in PostgreSQL batch traffic and index creation/repair coverage in `internal/database/dialect_test.go` when `XUI_DB_TYPE=postgres` and `XUI_DB_DSN` are set - 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's 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:35 +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#5993
No description provided.