[PR #5795] [CLOSED] perf(database): batch traffic writes #5988

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/5795
Author: @rqzbeh
Created: 7/4/2026
Status: ❌ Closed

Base: main ← Head: perf/database-hardening


📝 Commits (2)

  • b34f286 perf(database): batch traffic writes
  • e14691c Potential fix for pull request finding

📊 Changes

14 files changed (+477 additions, -133 deletions)

View changed files

📝 .gitignore (+1 -1)
📝 docs/architecture.md (+2 -1)
📝 internal/database/db.go (+40 -6)
📝 internal/database/db_settled_test.go (+22 -0)
📝 internal/database/dialect.go (+165 -1)
➕ internal/database/dialect_test.go (+71 -0)
📝 internal/database/index_tags_test.go (+1 -0)
📝 internal/database/model/model.go (+1 -1)
📝 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_node.go (+4 -2)
📝 internal/web/service/inbound_traffic.go (+150 -109)

📄 Description

Summary

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

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.

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 test
  • make verify

New coverage:

  • SQLite batch increment helper behavior in internal/database/dialect_test.go
  • SQLite WAL pragma and remark/tg_id index creation in internal/database/db_settled_test.go
  • GORM tg_id index tag coverage in internal/database/index_tags_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/5795 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 7/4/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/database-hardening` --- ### 📝 Commits (2) - [`b34f286`](https://github.com/MHSanaei/3x-ui/commit/b34f286f5f624b242207b22cc78ccbdcdc951294) perf(database): batch traffic writes - [`e14691c`](https://github.com/MHSanaei/3x-ui/commit/e14691ce4346618d90fb255cd5fbad2611f0f4c4) Potential fix for pull request finding ### 📊 Changes **14 files changed** (+477 additions, -133 deletions) <details> <summary>View changed files</summary> 📝 `.gitignore` (+1 -1) 📝 `docs/architecture.md` (+2 -1) 📝 `internal/database/db.go` (+40 -6) 📝 `internal/database/db_settled_test.go` (+22 -0) 📝 `internal/database/dialect.go` (+165 -1) ➕ `internal/database/dialect_test.go` (+71 -0) 📝 `internal/database/index_tags_test.go` (+1 -0) 📝 `internal/database/model/model.go` (+1 -1) 📝 `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_node.go` (+4 -2) 📝 `internal/web/service/inbound_traffic.go` (+150 -109) </details> ### 📄 Description ## Summary Optimizes database hot paths for traffic accounting and client lookups across SQLite and PostgreSQL. Adds WAL mode for SQLite, dialect-aware batch updates, narrower transactions, targeted settings writes, and DB-specific indexes. ## 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. ## 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 test` - `make verify` New coverage: - SQLite batch increment helper behavior in `internal/database/dialect_test.go` - SQLite WAL pragma and remark/tg_id index creation in `internal/database/db_settled_test.go` - GORM tg_id index tag coverage in `internal/database/index_tags_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#5988
No description provided.