[PR #6169] [MERGED] fix(clients): stop recomputing the summary badges from the client_stats snapshot #6131

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6169
Author: @mrnickson-hue
Created: 8/1/2026
Status: ✅ Merged
Merged: 8/14/2026
Merged by: @MHSanaei

Base: main ← Head: fix/6116-client-summary-server-trust


📝 Commits (2)

  • aa28148 fix(clients): stop recomputing the summary badges from the client_stats snapshot
  • 81f7320 fix(clients): force a refetch on window focus and drop a stale comment

📊 Changes

4 files changed (+114 additions, -233 deletions)

View changed files

📝 frontend/src/hooks/useClients.ts (+3 -87)
📝 frontend/src/pages/inbounds/useInbounds.ts (+0 -4)
➖ frontend/src/test/clients-summary.test.ts (+0 -142)
➕ frontend/src/test/clients-summary.test.tsx (+111 -0)

📄 Description

Summary

Drops the client-side recompute in pickClientsSummary (frontend/src/hooks/useClients.ts) that could display a corrupted clients-page summary when the client_stats WS snapshot diverged from clients in a shape the coverage guard couldn't detect. The summary badges now always read the server's q.summary() result directly.

Why

Refs #6116.

pickClientsSummary's guard is a count comparison (serverSummary.total > allClientStats.length), not an identity/coverage check. allClientStats is the raw client_traffics table (GetAllClientTraffics(), no join back to clients), so it can carry orphaned rows (traffic with no matching client) and can be missing rows for clients that don't have one yet. A shortfall-only guard misses two real cases: an orphan and a gap netting out to the same count, and — per #6116's bot review — an orphan surplus alone, which is the more reachable one since it needs drift in only one direction.

Since #6116 was opened, client_paging.go's q.summary() (landed in f52c3c4, "make the clients page scale to large panels") already computes the same bucket counts as a bounded SQL aggregate with clients as the driving table (Table("clients AS c").Joins("LEFT JOIN client_traffics ...")). That query can't confuse an orphaned client_traffics row for a real client regardless of how or when the row got there — it's driven by clients, not client_traffics. It's already shipped on every /panel/api/clients/list/paged response, and listQuery already polls that endpoint every 5000ms (refetchInterval: 5000), which happens to match cadenceXrayTraffic = "@every 5s" exactly.

That means the "cheaper aggregate query" #6116's discussion was waiting on to justify a server-side summary already exists and already runs on a matching cadence — just over REST instead of the client_stats WS payload. The client-side recompute this PR removes wasn't buying any extra freshness over that poll, only a window to get the number wrong. Dropping it needs no new backend work and, if anything, removes work from the hot path (computeClientsSummary used to re-run on every client_stats push).

Multi-node panels are, if anything, more exposed to the underlying divergence than a single-node install: NodeTrafficSyncJob is a second, independent source of client_stats broadcasts on its own @every 5s timer (not phase-locked to the local traffic job's), reading the same unjoined GetAllClientTraffics(), and SetRemoteTraffic merges up to 8 nodes' snapshots into the shared clients/client_traffics tables concurrently. q.summary()'s correctness doesn't depend on that timing at all, since it always reflects whatever's actually in the database at request time.

Mutation responsiveness (enable/disable/delete/etc.) is unaffected — those already call invalidateAll(), which forces an immediate listQuery refetch independent of the 5s poll.

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?

  • frontend/src/test/clients-summary.test.tsx: two new tests reproduce #6116's exact shapes against the live hook (renderHook + applyClientStatsEvent) — an orphan row with no compensating gap, and an orphan+gap pair that nets to the server's total — and assert summary stays pinned to the server's value in both, where the old guard let at least one of these through.
  • allClientStats, computeClientsSummary, pickClientsSummary and sameSummaryInputs are removed as dead code (their only caller was the recompute this PR drops); sameSpeedMap's tests are kept as-is since that function is unrelated and untouched.
  • make verify (gen-check, golangci-lint, ESLint, tsc --noEmit, go test -shuffle=on -count=1 ./..., frontend npm run test, go build ./... + frontend build, build-storybook) — all green except two pre-existing, unrelated client-form-flow.test.tsx / inbound-form-modal.test.tsx timeouts under this sandbox's load, which I confirmed also fail on an unmodified main checkout (git stash + rerun) before restoring this change, so they're not a regression from this diff.

Screenshots / recordings

N/A — no visual change, the displayed numbers are unaffected in the common case; this only changes what happens during the specific divergence window #6116 describes.

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/6169 **Author:** [@mrnickson-hue](https://github.com/mrnickson-hue) **Created:** 8/1/2026 **Status:** ✅ Merged **Merged:** 8/14/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/6116-client-summary-server-trust` --- ### 📝 Commits (2) - [`aa28148`](https://github.com/MHSanaei/3x-ui/commit/aa28148a2909beb2f3463b07b2d36a737241de1b) fix(clients): stop recomputing the summary badges from the client_stats snapshot - [`81f7320`](https://github.com/MHSanaei/3x-ui/commit/81f73203fe4959f0dd65818d6d488096ebc9f75b) fix(clients): force a refetch on window focus and drop a stale comment ### 📊 Changes **4 files changed** (+114 additions, -233 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/hooks/useClients.ts` (+3 -87) 📝 `frontend/src/pages/inbounds/useInbounds.ts` (+0 -4) ➖ `frontend/src/test/clients-summary.test.ts` (+0 -142) ➕ `frontend/src/test/clients-summary.test.tsx` (+111 -0) </details> ### 📄 Description ## Summary Drops the client-side recompute in `pickClientsSummary` (`frontend/src/hooks/useClients.ts`) that could display a corrupted clients-page summary when the `client_stats` WS snapshot diverged from `clients` in a shape the coverage guard couldn't detect. The summary badges now always read the server's `q.summary()` result directly. ## Why Refs #6116. `pickClientsSummary`'s guard is a count comparison (`serverSummary.total > allClientStats.length`), not an identity/coverage check. `allClientStats` is the raw `client_traffics` table (`GetAllClientTraffics()`, no join back to `clients`), so it can carry orphaned rows (traffic with no matching client) and can be missing rows for clients that don't have one yet. A shortfall-only guard misses two real cases: an orphan and a gap netting out to the same count, and — per #6116's bot review — an orphan surplus alone, which is the more reachable one since it needs drift in only one direction. Since #6116 was opened, `client_paging.go`'s `q.summary()` (landed in `f52c3c4`, "make the clients page scale to large panels") already computes the same bucket counts as a bounded SQL aggregate with `clients` as the driving table (`Table("clients AS c").Joins("LEFT JOIN client_traffics ...")`). That query can't confuse an orphaned `client_traffics` row for a real client regardless of how or when the row got there — it's driven by `clients`, not `client_traffics`. It's already shipped on every `/panel/api/clients/list/paged` response, and `listQuery` already polls that endpoint every 5000ms (`refetchInterval: 5000`), which happens to match `cadenceXrayTraffic = "@every 5s"` exactly. That means the "cheaper aggregate query" #6116's discussion was waiting on to justify a server-side summary already exists and already runs on a matching cadence — just over REST instead of the `client_stats` WS payload. The client-side recompute this PR removes wasn't buying any extra freshness over that poll, only a window to get the number wrong. Dropping it needs no new backend work and, if anything, removes work from the hot path (`computeClientsSummary` used to re-run on every `client_stats` push). Multi-node panels are, if anything, more exposed to the underlying divergence than a single-node install: `NodeTrafficSyncJob` is a second, independent source of `client_stats` broadcasts on its own `@every 5s` timer (not phase-locked to the local traffic job's), reading the same unjoined `GetAllClientTraffics()`, and `SetRemoteTraffic` merges up to 8 nodes' snapshots into the shared `clients`/`client_traffics` tables concurrently. `q.summary()`'s correctness doesn't depend on that timing at all, since it always reflects whatever's actually in the database at request time. Mutation responsiveness (enable/disable/delete/etc.) is unaffected — those already call `invalidateAll()`, which forces an immediate `listQuery` refetch independent of the 5s poll. ## Type of change - [x] Bug fix - [ ] New feature - [x] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [x] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? - `frontend/src/test/clients-summary.test.tsx`: two new tests reproduce #6116's exact shapes against the live hook (`renderHook` + `applyClientStatsEvent`) — an orphan row with no compensating gap, and an orphan+gap pair that nets to the server's total — and assert `summary` stays pinned to the server's value in both, where the old guard let at least one of these through. - `allClientStats`, `computeClientsSummary`, `pickClientsSummary` and `sameSummaryInputs` are removed as dead code (their only caller was the recompute this PR drops); `sameSpeedMap`'s tests are kept as-is since that function is unrelated and untouched. - `make verify` (gen-check, golangci-lint, ESLint, `tsc --noEmit`, `go test -shuffle=on -count=1 ./...`, frontend `npm run test`, `go build ./...` + frontend build, `build-storybook`) — all green except two pre-existing, unrelated `client-form-flow.test.tsx` / `inbound-form-modal.test.tsx` timeouts under this sandbox's load, which I confirmed also fail on an unmodified `main` checkout (`git stash` + rerun) before restoring this change, so they're not a regression from this diff. ## Screenshots / recordings N/A — no visual change, the displayed numbers are unaffected in the common case; this only changes what happens during the specific divergence window #6116 describes. ## 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. - [ ] 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:06:36 +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#6131
No description provided.