mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6954] [CLOSED] feat(clients): persist depletion timestamp and derive current reasons #6539
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#6539
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/6954
Author: @HamedSanaei
Created: 10/26/2026
Status: ❌ Closed
Base:
main← Head:codex/persist-client-depletion📝 Commits (1)
b6ec3bdfeat(clients): persist depletion timestamp and derive current reasons📊 Changes
45 files changed (+3341 additions, -296 deletions)
View changed files
📝
.github/workflows/ci.yml(+13 -3)📝
docs/content/docs/en/reference/api/clients.mdx(+17 -6)📝
docs/custom-subscription-templates.md(+56 -0)📝
docs/public/openapi.json(+307 -31)📝
frontend/public/openapi.json(+307 -31)📝
frontend/src/env.d.ts(+2 -0)📝
frontend/src/generated/examples.ts(+68 -0)📝
frontend/src/generated/schemas.ts(+122 -0)📝
frontend/src/generated/types.ts(+27 -0)📝
frontend/src/generated/zod.ts(+29 -0)📝
frontend/src/pages/api-docs/endpoints.ts(+9 -19)📝
frontend/src/schemas/client.ts(+4 -0)➕
frontend/src/test/client-depletion-schema.test.ts(+35 -0)➕
internal/database/client_depletion_migration_test.go(+172 -0)📝
internal/database/db.go(+35 -0)📝
internal/database/dump_sqlite_test.go(+2 -2)📝
internal/database/migrate_data.go(+6 -1)➕
internal/nodee2e/client_depletion_test.go(+61 -0)📝
internal/sub/controller.go(+25 -23)➕
internal/sub/depletion_test.go(+133 -0)...and 25 more files
📄 Description
Summary
Persist the start of a client's current automatic-depletion period in one database column and derive its current reasons when existing responses are read. Expose the read-only fields through client details, traffic/inbound responses, subscription info JSON, and custom subscription templates.
Why
Closes #6883.
A timestamp should survive restarts and partial recovery, while a reason must follow current quota and expiry conditions rather than retain a stale cause. Existing routes, authentication, request handling, response envelopes, portable export/import, and renewal eligibility remain unchanged.
Database and lifecycle
depleted_at BIGINT NOT NULL DEFAULT 0; no new table or index. The explicit SQLite/PostgreSQL migration is idempotent, normalizes nullable timestamps, preserves existing values, and enforces the default and nullability. Historical event times remain unknown.depletedReasonis a read-only SQL projection:"","traffic","expiry", or"both". A legacydepleted_reasoncolumn is retained in place but is neither created nor maintained by this implementation. Database copying excludes virtual fields and retains the timestamp.0even when the current reason is known.API and subscription semantics
Existing client-detail objects gain
depletedAtanddepletedReasonbesideusedTraffic; existing traffic objects gain the same fields. A typed response DTO drives generated schemas and examples without adding these columns to the client record. Frontend parsers continue accepting older responses without the fields.During
traffic -> both -> expiry -> recovered, a known timestamp remains unchanged until full recovery, then becomes0. Crossing an expiry boundary changes the current reason on a read without an UPDATE. For example, this metadata fragment can change from"traffic"to"both"while retaining its timestamp:For a shared subscription ID, every distinct member must currently be depleted, including members without emitted links. Otherwise the result is
0/"". When all members are depleted, reasons are the union of their causes: mixed traffic-only and expiry-only members produce"both", without claiming that every member has both causes. Return the latest member timestamp only when every timestamp is known; otherwise return0with the current aggregate reason. Reuse the batched traffic load rather than issue a second metadata lookup.Accounting boundary
Existing legacy adoption behavior can start a missing master traffic row at zero on a known node inbound. The master then undercounts usage from before adoption. Metadata follows its authoritative shared accumulator and fresh cross-panel totals; node baselines have no quota-period identifier and cannot safely reconstruct the missing period. This limitation is documented and covered by regression tests. Other nodes' counters and independent renewal periods are preserved.
Type of change
Areas affected
Frontend changes are response schemas and API documentation only; there is no new UI or translation key.
How was this tested?
TDD reproductions cover stale reasons after expiry, continuous and recovered periods, cross-panel freshness gaps, independent master totals, read-only legacy columns, nullable migrations, PostgreSQL counter overflow, and idle node synchronization accidentally sweeping unrelated metadata. Regression coverage also includes manual toggles, resets/corrections/limit edits, renewal, imports, rollback, concurrency, 400-client chunk boundaries, node snapshots, subscription aggregation, template rendering, and older frontend responses. Additional red-to-green cases cover extending an already-expired deadline while stale global evidence exists, nullable legacy limits, server-derived metadata in inbound mutation responses, and rejecting foreign-client metadata in those responses.
Local verification used Linux/amd64 under WSL, Go 1.27.3, Node 26.3.0, SQLite, and a disposable PostgreSQL 16 database:
make verifypassed on the final commit, including generated artifacts, Go/frontend checks, frontend/Go builds, Storybook, and matching published OpenAPI specifications.GOFLAGS=-p=2 make gen-check lint-go test-goandGOFLAGS=-p=2 make racepassed on the final tree.make node-e2epassed; the latter ran real disposable panels, including depletion/recovery, restart, and chained global-traffic synchronization.Remote CI: all 12 checks passed on
b6ec3bdc7d428dcdd84a4280e9539e79df87f66c, including PostgreSQL migrations/lifecycle, race, frontend, parser fuzzing, and node end-to-end tests. Verified on 2026-10-26.Database performance
Identical benchmark harness against old PR HEAD
31ceee68c35360e6cc776cec5ee36052474dafdband this implementation, on the same i7-1185G7 Linux host. Ten measured iterations per case; setup excluded. These are point estimates, not statistical performance guarantees. The idle measurement covers the depletion phase, not the entire traffic job. Reads use the existing full traffic loader; active-counter measurements update every listed client in one transaction.Without cross-panel/node reports, before -> after in milliseconds:
With one fresh master report and one node baseline per client, before -> after in milliseconds:
Statement budgets:
Deriving current reasons adds read-side CPU, and the overflow-safe comparisons also have a measured cost with fresh reports. This trades some computation for correct current state and removal of repeated metadata writes; timings do not show a universal speedup.
Reproduce with
go test ./internal/web/service -run '^$' -bench '^BenchmarkClientDepletion' -benchtime=10x -count=1, using the same harness on both revisions.Breaking changes
None. Responses gain read-only fields, older responses remain accepted, and the timestamp column is added automatically. Historical depletion times cannot be reconstructed. Older binaries do not maintain this metadata, so history continuity through a downgrade is not guaranteed. Portable client exports still omit depletion history; database backups retain the timestamp.
Checklist
go build ./...and the test suite pass locally.npm run lint,npm run typecheck, andnpm run buildpass.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.