[GH-ISSUE #6883] [Feature]: Persist when a client was depleted (quota exhausted / expired) and expose it via API #4570

Open
opened 2026-10-31 20:40:35 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @aibedini on GitHub (Oct 20, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6883

3x-ui disables a client automatically once its quota is used up or its expiry passes, but it does not record when or why that happened. Panels, subscription pages and external tooling therefore cannot answer "at what time did this account run out of data?".

What the code does today (checked against current main):

  • disableInvalidClients (internal/web/service/inbound_disable.go) selects rows matching total > 0 AND up + down >= total or expiry_time > 0 AND expiry_time <= now, then sets client_traffics.enable = false and clients.enable = false. No timestamp or reason is stored.
  • expiry_time already gives the exact moment for expiry, but nothing equivalent exists for quota exhaustion.
  • clients.updated_at is bumped when the client is disabled, but it also changes on every unrelated edit, so it cannot be trusted as a depletion time.
  • lastOnline is only bumped on a non-zero traffic delta, so it is a rough approximation of the last consumed byte, not a depletion marker.
  • Panel logs are not persistent, and Telegram/Discord notifications are fire-and-forget (nothing is stored).

Result: anyone who wants to show "your data ran out at 14:32" or audit when accounts were cut off has to poll client_traffics continuously and guess from the first poll that sees up + down >= total. That is inaccurate, wasteful, and impossible to backfill after the fact.

Describe the solution you'd like

Record the depletion event at the moment the panel itself detects it, and expose it.

  1. Storage: add two columns to client_traffics (GORM AutoMigrate, default 0 / "" = unknown, fully backward compatible):

    • depleted_at (int64, Unix ms)
    • depleted_reason (string: "traffic" | "expiry")
  2. Writing: in disableInvalidClients, when a client is flipped to disabled because of depletion, set both fields in the same transaction.

    • reason = "traffic" -> depleted_at = detection time (precision = traffic job interval; please document this).
    • reason = "expiry" -> depleted_at = expiry_time (the exact boundary).
    • If both conditions are true, keep the earlier timestamp.
    • Only set when the client was previously enabled, so repeated polls do not overwrite the first timestamp (idempotent).
    • A client disabled manually by an admin is NOT a depletion and must not get these fields.
  3. Clearing: reset both fields when the client stops being depleted: traffic reset, expiry extended, quota raised, or renewal (the code paths that already guard "enable only once counters are zero", e.g. client_traffic.go, client_bulk.go).

  4. Exposure:

    • Include depletedAt and depletedReason in the existing client responses (/panel/api/clients/get/{email}, /panel/api/clients/traffic/{email}, and the list/inbound client objects).
    • Add them as variables for custom subscription templates (next to lastOnline in docs/custom-subscription-templates.md).
    • Optionally include the timestamp in Telegram/Discord depletion notifications.
  5. Remote nodes / multi-panel: when a node disables a client through the node traffic sync, the master should receive and persist the same two fields rather than stamping its own sync time. For depletion triggered by the cross-panel client_global_traffics check, reason should still be "traffic".

No new history table is required for this request; an append-only event log could be a later extension.

Alternatives you've considered

  • Polling client_traffics from outside and recording the first poll where up + down >= total: precision is only as good as the poll interval, nothing exists for accounts that depleted before polling started, and every consumer has to build the same machinery.
  • Using clients.updated_at: changes on any edit, so it is unreliable.
  • Using lastOnline as a proxy: only approximates the last consumed byte and is unrelated to expiry.
  • Parsing panel logs or relying on Telegram notifications: logs are not persistent and notifications are not stored.

Concrete use case

I run a management layer on top of 3x-ui that serves customers a subscription page and sends renewal reminders. Customers regularly ask "when did my data run out?", and support needs to tell whether an account was cut off two minutes or two days ago before deciding how to handle a renewal dispute.

Today we reconstruct this from our own polling, which gives wrong or missing answers for accounts that depleted between polls or before the tooling existed. With depletedAt and depletedReason provided by the panel, the subscription page could show "Data ran out at 14:32" directly, support could audit cut-offs, and any third-party tool using the API would get the same accurate data with no extra polling.

Scale example: ~hundreds of clients across several inbounds and multiple nodes, where polling every client continuously just to detect one transition is wasteful.

Mockups, screenshots, or examples

Example API response (new fields at the end):

{
"email": "user1",
"enable": false,
"up": 10737418240,
"down": 10737418240,
"total": 21474836480,
"expiryTime": 1795080448277,
"lastOnline": 1792401120000,
"depletedAt": 1792401160000,
"depletedReason": "traffic"
}

Expiry case: "depletedAt" equals "expiryTime", "depletedReason": "expiry".
Never depleted / unknown (including clients depleted before upgrade): "depletedAt": 0, "depletedReason": "".

UI idea: in the clients table, a small tooltip on the "Depleted" badge: "Data exhausted · 2026-10-20 14:32".

Which parts of the panel would this affect?

No response

Are you willing to help?

  • I'd like to implement this feature myself and open a PR.
  • I can help test once a PR is open.

Before submitting

  • I searched existing issues and this feature has not been requested.
  • This feature request is written in English.
Originally created by @aibedini on GitHub (Oct 20, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6883 ### Is your feature request related to a problem? 3x-ui disables a client automatically once its quota is used up or its expiry passes, but it does not record *when* or *why* that happened. Panels, subscription pages and external tooling therefore cannot answer "at what time did this account run out of data?". What the code does today (checked against current main): - `disableInvalidClients` (`internal/web/service/inbound_disable.go`) selects rows matching `total > 0 AND up + down >= total` or `expiry_time > 0 AND expiry_time <= now`, then sets `client_traffics.enable = false` and `clients.enable = false`. No timestamp or reason is stored. - `expiry_time` already gives the exact moment for expiry, but nothing equivalent exists for quota exhaustion. - `clients.updated_at` is bumped when the client is disabled, but it also changes on every unrelated edit, so it cannot be trusted as a depletion time. - `lastOnline` is only bumped on a non-zero traffic delta, so it is a rough approximation of the last consumed byte, not a depletion marker. - Panel logs are not persistent, and Telegram/Discord notifications are fire-and-forget (nothing is stored). Result: anyone who wants to show "your data ran out at 14:32" or audit when accounts were cut off has to poll `client_traffics` continuously and guess from the first poll that sees `up + down >= total`. That is inaccurate, wasteful, and impossible to backfill after the fact. ### Describe the solution you'd like Record the depletion event at the moment the panel itself detects it, and expose it. 1. Storage: add two columns to `client_traffics` (GORM AutoMigrate, default 0 / "" = unknown, fully backward compatible): - `depleted_at` (int64, Unix ms) - `depleted_reason` (string: "traffic" | "expiry") 2. Writing: in `disableInvalidClients`, when a client is flipped to disabled because of depletion, set both fields in the same transaction. - reason = "traffic" -> `depleted_at` = detection time (precision = traffic job interval; please document this). - reason = "expiry" -> `depleted_at` = `expiry_time` (the exact boundary). - If both conditions are true, keep the earlier timestamp. - Only set when the client was previously enabled, so repeated polls do not overwrite the first timestamp (idempotent). - A client disabled manually by an admin is NOT a depletion and must not get these fields. 3. Clearing: reset both fields when the client stops being depleted: traffic reset, expiry extended, quota raised, or renewal (the code paths that already guard "enable only once counters are zero", e.g. `client_traffic.go`, `client_bulk.go`). 4. Exposure: - Include `depletedAt` and `depletedReason` in the existing client responses (`/panel/api/clients/get/{email}`, `/panel/api/clients/traffic/{email}`, and the list/inbound client objects). - Add them as variables for custom subscription templates (next to `lastOnline` in docs/custom-subscription-templates.md). - Optionally include the timestamp in Telegram/Discord depletion notifications. 5. Remote nodes / multi-panel: when a node disables a client through the node traffic sync, the master should receive and persist the same two fields rather than stamping its own sync time. For depletion triggered by the cross-panel `client_global_traffics` check, reason should still be "traffic". No new history table is required for this request; an append-only event log could be a later extension. ### Alternatives you've considered - Polling `client_traffics` from outside and recording the first poll where `up + down >= total`: precision is only as good as the poll interval, nothing exists for accounts that depleted before polling started, and every consumer has to build the same machinery. - Using `clients.updated_at`: changes on any edit, so it is unreliable. - Using `lastOnline` as a proxy: only approximates the last consumed byte and is unrelated to expiry. - Parsing panel logs or relying on Telegram notifications: logs are not persistent and notifications are not stored. ### Concrete use case I run a management layer on top of 3x-ui that serves customers a subscription page and sends renewal reminders. Customers regularly ask "when did my data run out?", and support needs to tell whether an account was cut off two minutes or two days ago before deciding how to handle a renewal dispute. Today we reconstruct this from our own polling, which gives wrong or missing answers for accounts that depleted between polls or before the tooling existed. With `depletedAt` and `depletedReason` provided by the panel, the subscription page could show "Data ran out at 14:32" directly, support could audit cut-offs, and any third-party tool using the API would get the same accurate data with no extra polling. Scale example: ~hundreds of clients across several inbounds and multiple nodes, where polling every client continuously just to detect one transition is wasteful. ### Mockups, screenshots, or examples Example API response (new fields at the end): { "email": "user1", "enable": false, "up": 10737418240, "down": 10737418240, "total": 21474836480, "expiryTime": 1795080448277, "lastOnline": 1792401120000, "depletedAt": 1792401160000, "depletedReason": "traffic" } Expiry case: "depletedAt" equals "expiryTime", "depletedReason": "expiry". Never depleted / unknown (including clients depleted before upgrade): "depletedAt": 0, "depletedReason": "". UI idea: in the clients table, a small tooltip on the "Depleted" badge: "Data exhausted · 2026-10-20 14:32". ### Which parts of the panel would this affect? _No response_ ### Are you willing to help? - [ ] I'd like to implement this feature myself and open a PR. - [ ] I can help test once a PR is open. ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this feature has not been requested. - [x] This feature request is written in English.
Author
Owner

@github-actions[bot] commented on GitHub (Oct 20, 2026):

This is a valid feature request. Your description of current behaviour is accurate: the panel disables depleted clients but records neither when it happened nor why. I checked every claim in the report against main and found nothing to correct.

What the code does today

  • disableInvalidClients (internal/web/service/inbound_disable.go:80) selects rows with depletedCond, whose local form is
    ((total > 0 AND up + down >= total)
    	OR (expiry_time > 0 AND expiry_time <= ?))
    
    (inbound_disable.go:60-61). On a panel that holds fresh client_global_traffics rows from a master, a third branch is added: a cross-panel EXISTS check (inbound_disable.go:46-53).
  • The only writes are Update("enable", false) on client_traffics (inbound_disable.go:163-165) and Updates(map[string]any{"enable": false, "updated_at": now}) on clients (inbound_disable.go:173-175). No reason or depletion timestamp is stored, and the selected depletedRows are never classified by which branch matched.
  • xray.ClientTraffic (internal/xray/client_traffic.go:5-30) has ExpiryTime, LastOnline and LastSubFetch, but no depletion field. A grep of internal/ finds no depleted_at or similar column. The only "depleted" values are computed live: the sub-page check isDepleted := traffic.Total > 0 && (traffic.Up+traffic.Down) >= traffic.Total (internal/sub/service.go:416) and the depleted status in internal/sub/remark_vars.go:301.

Notes for whoever implements it

  • Idempotency is mostly there already. The select only picks rows with enable = true (inbound_disable.go:86), so setting the stamp in that same pass writes it once. Manual admin disables never go through this function, so they would never get a stamp.
  • Reason. The reason has to be worked out per row from Up+Down, Total and ExpiryTime, because the SQL doesn't say which branch matched. A row that matched only the client_global_traffics branch would be "traffic", as you proposed.
  • Clearing. Every path that re-enables a client already goes through one of four Updates(map[string]any{"enable": true, "up": 0, "down": 0}) calls: client_traffic.go:87, :177, :222 and inbound_traffic.go:684 (ResetClientTrafficByEmail). Those are where the new fields would be reset. Extending the expiry or raising the quota needs separate handling, because those edits don't zero the counters.
  • Nodes. A node runs this same job on its own data. The master learns about node-side state through SetRemoteTraffic (internal/web/service/inbound_node.go:392), which receives runtime.TrafficSnapshot.Inbounds (internal/web/runtime/remote.go:748). My inference, which I did not trace to the merge code: new ClientTraffic columns would travel in that snapshot, but the master's merge would need an explicit rule to take the node's stamp and not overwrite it with its own sync time. The node contract tests in internal/web/node_contract_test.go and internal/nodee2e/ cover that path.
  • Exposure. The change also touches API exposure, i.e. the example: tags and openapigen output for ClientTraffic, plus a DB migration in internal/database/db.go, as the repository rules require for model changes.

Duplicates. I found none. Related but different requests: #6314 (auto-renew immediately on depletion) and #6354 (a webhook for auto-renew). Neither stores a depletion time.

Verdict: Feature request

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:6294967035 --> @github-actions[bot] commented on GitHub (Oct 20, 2026): This is a valid feature request. Your description of current behaviour is accurate: the panel disables depleted clients but records neither when it happened nor why. I checked every claim in the report against `main` and found nothing to correct. **What the code does today** - `disableInvalidClients` (`internal/web/service/inbound_disable.go:80`) selects rows with `depletedCond`, whose local form is ```go ((total > 0 AND up + down >= total) OR (expiry_time > 0 AND expiry_time <= ?)) ``` (`inbound_disable.go:60-61`). On a panel that holds fresh `client_global_traffics` rows from a master, a third branch is added: a cross-panel `EXISTS` check (`inbound_disable.go:46-53`). - The only writes are `Update("enable", false)` on `client_traffics` (`inbound_disable.go:163-165`) and `Updates(map[string]any{"enable": false, "updated_at": now})` on `clients` (`inbound_disable.go:173-175`). No reason or depletion timestamp is stored, and the selected `depletedRows` are never classified by which branch matched. - `xray.ClientTraffic` (`internal/xray/client_traffic.go:5-30`) has `ExpiryTime`, `LastOnline` and `LastSubFetch`, but no depletion field. A grep of `internal/` finds no `depleted_at` or similar column. The only "depleted" values are computed live: the sub-page check `isDepleted := traffic.Total > 0 && (traffic.Up+traffic.Down) >= traffic.Total` (`internal/sub/service.go:416`) and the `depleted` status in `internal/sub/remark_vars.go:301`. **Notes for whoever implements it** - **Idempotency is mostly there already.** The select only picks rows with `enable = true` (`inbound_disable.go:86`), so setting the stamp in that same pass writes it once. Manual admin disables never go through this function, so they would never get a stamp. - **Reason.** The reason has to be worked out per row from `Up+Down`, `Total` and `ExpiryTime`, because the SQL doesn't say which branch matched. A row that matched only the `client_global_traffics` branch would be `"traffic"`, as you proposed. - **Clearing.** Every path that re-enables a client already goes through one of four `Updates(map[string]any{"enable": true, "up": 0, "down": 0})` calls: `client_traffic.go:87`, `:177`, `:222` and `inbound_traffic.go:684` (`ResetClientTrafficByEmail`). Those are where the new fields would be reset. Extending the expiry or raising the quota needs separate handling, because those edits don't zero the counters. - **Nodes.** A node runs this same job on its own data. The master learns about node-side state through `SetRemoteTraffic` (`internal/web/service/inbound_node.go:392`), which receives `runtime.TrafficSnapshot.Inbounds` (`internal/web/runtime/remote.go:748`). My inference, which I did not trace to the merge code: new `ClientTraffic` columns would travel in that snapshot, but the master's merge would need an explicit rule to take the node's stamp and not overwrite it with its own sync time. The node contract tests in `internal/web/node_contract_test.go` and `internal/nodee2e/` cover that path. - **Exposure.** The change also touches API exposure, i.e. the `example:` tags and openapigen output for `ClientTraffic`, plus a DB migration in `internal/database/db.go`, as the repository rules require for model changes. **Duplicates.** I found none. Related but different requests: #6314 (auto-renew immediately on depletion) and #6354 (a webhook for auto-renew). Neither stores a depletion time. Verdict: Feature request *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
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#4570
No description provided.