[PR #6987] [MERGED] Re-enable depleted clients on renewal, and stop operator writes queueing behind the 5s poll and node merges #6565

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6987
Author: @Zaltapar
Created: 10/28/2026
Status: ✅ Merged
Merged: 11/5/2026
Merged by: @MHSanaei

Base: main ← Head: fix/renewal-reenable-and-faster-saves


📝 Commits (5)

  • 10e6baa fix(clients): re-enable depleted clients when a renewal restores their limits
  • e675723 perf(clients): fold the reset re-enable into the reset and serve operator writes ahead of the 5s poll and node merges
  • 01136c8 fix(clients): re-enable every inbound on reset, and drain both writer lanes on stop
  • 5403b0d fix(clients): preserve manual disables when integrating renewal fixes
  • c3ee71a Merge remote-tracking branch 'origin/main' into codex/review-pr-6987-20261105

📊 Changes

13 files changed (+684 additions, -92 deletions)

View changed files

📝 internal/web/service/client_crud.go (+15 -0)
📝 internal/web/service/client_inbound_apply.go (+28 -1)
➕ internal/web/service/client_renewal_reenable.go (+60 -0)
📝 internal/web/service/client_traffic.go (+15 -10)
📝 internal/web/service/client_traffic_reset_order_test.go (+1 -1)
➕ internal/web/service/client_update_reenable_test.go (+284 -0)
📝 internal/web/service/inbound_disable.go (+42 -0)
📝 internal/web/service/inbound_node.go (+3 -1)
📝 internal/web/service/inbound_traffic.go (+64 -47)
📝 internal/web/service/inbound_traffic_global.go (+1 -1)
📝 internal/web/service/traffic_writer.go (+74 -19)
📝 internal/web/service/traffic_writer_test.go (+95 -11)
📝 internal/web/service/tuic_traffic_receipt.go (+2 -1)

📄 Description

Summary

Two fixes for the renewal workflow on a VPN reseller panel:

  1. A renewed client that the quota loop had switched off is re-enabled by the renewal itself, in the single-client paths that were missing the #5619 set-difference rule.
  2. Saving / resetting a client no longer does the re-enable's work twice, and operator writes no longer queue behind the 5s traffic poll and the per-node snapshot merges that arrive every 5s.

Why

When a client exhausts its traffic or its duration, the enforcement loop disables it in all three stores (inbound settings JSON, client_traffics, clients) and drops the user from the running core. bulkAdjust already re-enabled such a client when the adjustment lifted it back within limits (#5619), and a traffic reset re-enabled it too — but only by running a second, full client update behind the reset.

Renewing the client from the client editor (or via the single-field renewal endpoints the Telegram bot uses: set expiry / set traffic limit) left it disabled: the update wrote the fresh expiry/quota but kept the off switch, so the operator had to flip the enable switch by hand. This is the same gap as #5619 on the paths that endpoint covers.

The renewal actions were also slow: a reset of one client on N inbounds cost N serialized write slots for the counters and N more for the follow-up full update (N inbound JSON rewrites, N stat-row re-assertions, N runtime adds). On a node-heavy panel every one of those writes could additionally wait behind the next batch of 5s traffic polls / node snapshot merges in the single serial writer (#6035), so a save or reset "took so much time".

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

Changes

Re-enable on renewal (fix(clients)) — the #5619 set-difference rule applied to the renewal paths that still lacked it:

  • New ClientService.renewalReenables: the client's enforcement row must carry the off switch AND match the production depletion predicate (including a fresh cross-panel push, exactly as the loop itself reads it), and the post-update total/expiry must clear the depletion (projected against the current usage and the pushed usage; usage is untouched by an update). A client the operator disabled while it was still within its limits, or one still depleted after the update, stays off.
  • ClientService.Update (the editor save and POST /clients/update/:email, local and node inbounds alike) forces enable on in that case; the existing per-inbound apply then flips the JSON entry, the client_traffics row and the clients record and re-adds the user on the running core (or pushes the node), so the re-enable lands in all stores at once.
  • The no-inbound update branch also re-asserts the enforcement row's enable flag — only the inbound path ever wrote it before.
  • ResetClientExpiryTimeByEmail / ResetClientTrafficLimitByEmail (Telegram bot renewals) flip the stored entry back on with the renewal instead of leaving it off.

A note on the enable override: the guard cannot distinguish an enable:false the editor round-tripped from one a caller chose deliberately, so a renewal that clears the depletion turns the client on even when the request carries enable:false (the editor always round-trips the stored value, which is off for a quota-disabled client). This matches the panel-wide invariant that a client back within its limits is on — the same one bulkAdjust, the traffic resets and the calendar auto-renew already implement (#5619). A caller who wants a renewed client to stay off (e.g. payment not cleared yet) must disable it again after the renewal; nothing in the request or response reports the override, which is a known trade-off of this rule.

Faster renewal actions (perf(clients)):

  • InboundService.resetClientTrafficLocked now flips this inbound's stored entry and the shared client record back on in the reset's own transaction (the re-enable twin of the disable pass's markClientsDisabledInSettings), re-adds the user on a local core, and marks the node dirty on a node inbound. The flip is decided per stored entry, not from the shared enforcement row: the first inbound's reset turns that row back on, and a row-keyed check would leave every sibling inbound's off entry stale, cutting the client off there until a full client update. ClientService.ResetTrafficByEmail only still runs the follow-up full update when the resets left the record off (an inbound-less client, or a record no inbound hosted) — the common renewal case drops N serialized writes, N JSON rewrites and N runtime adds.
  • The serial traffic writer keeps its single-goroutine ordering (what keeps the poll and client mutations from deadlocking Postgres, c5d31de4e) but now serves two lanes — panel-initiated writes jump queued traffic-poll and node-merge work, so a save or reset on a node-heavy panel no longer waits on the next batch of 5s snapshots (#6035). Within each lane the order stays FIFO; on stop the drain empties both lanes, so no queued write is dropped.
    • Known trade-offs: a sustained burst of admin writes can delay the periodic poll / node merges (no cap on the priority), and a poll captured before a reset can now land after it, adding pre-reset deltas onto the zeroed row for one window — the same transient the base had, just widened to the queue wait.

How was this tested?

  • New tests in client_update_reenable_test.go: an expired client extended in the editor comes back on in all three stores; a raised quota does the same; an extension that still lands in the past / a quota still below usage / a manual switch-off of a not-depleted client / a fresh cross-panel push above the new quota all stay disabled; the no-inbound case re-asserts the enforcement row; both single-field setters re-enable and both respect a manual disable; and a multi-inbound client comes back on every inbound after a reset (the per-entry flip).
  • TestResetTrafficOfDepletedClientSurvivesTickMidReset keeps proving that a tick landing mid-reset cannot re-disable the client (all three stores read enabled at 0 afterwards).
  • TestTrafficWriterAdminLanePreemptsBackgroundQueue: with both lanes filled, the admin write is served before the background one. TestTrafficWriterStopDrainsEveryQueuedWrite: a stop drains every queued write on both lanes.
  • TestTrafficResetKeepsTheReverseTag (and the reverse-tag suite) keep proving the drift case — entry on, enforcement row off — still plans the runtime re-add, which is what rebuilds a reverse tag on reset.
  • Full internal/web/service, internal/web/runtime, internal/web/job, internal/web/controller suites pass, plus a -race run over the same packages, and a full-package run of the go-test gate.
  • Both Postgres CI gate test sets (postgres-durable-first and postgres-migration from .github/workflows/ci.yml) pass locally against a UTF8 Postgres 17 instance. (Note for anyone reproducing on Windows: the server must be created with initdb -E UTF8 — a locale-encoded database makes the pgx-based tests fail with "simple protocol queries must be run with client_encoding=UTF8" before they even connect to the panel's schema.)
  • The race gate passes locally over the touched packages; the fuzz-smoke gate (both fuzzers, 30s) and the pinned golangci-lint v2.15.0 pass over the whole repo. node-e2e cannot run on the local Windows-on-ARM machine, so it — along with the whole fork-PR CI suite — is validated when a maintainer approves the workflow run.

Breaking changes

None. Renewal re-enabling is strictly the set-difference the bulk paths already promised; manually disabled clients and still-depleted renewals are untouched. The one behavior change is documented above: a renewal that clears the depletion takes precedence over a round-tripped enable:false.


🔄 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/6987 **Author:** [@Zaltapar](https://github.com/Zaltapar) **Created:** 10/28/2026 **Status:** ✅ Merged **Merged:** 11/5/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/renewal-reenable-and-faster-saves` --- ### 📝 Commits (5) - [`10e6baa`](https://github.com/MHSanaei/3x-ui/commit/10e6baa8153e2afcb7ce9f7152d1ab1a5e31fb8e) fix(clients): re-enable depleted clients when a renewal restores their limits - [`e675723`](https://github.com/MHSanaei/3x-ui/commit/e675723b240aa779e49553ee43de82d5b172ecf4) perf(clients): fold the reset re-enable into the reset and serve operator writes ahead of the 5s poll and node merges - [`01136c8`](https://github.com/MHSanaei/3x-ui/commit/01136c8f0e3b3d55babfd4274a48b3fafa3eda34) fix(clients): re-enable every inbound on reset, and drain both writer lanes on stop - [`5403b0d`](https://github.com/MHSanaei/3x-ui/commit/5403b0d3d9ecb6e9758f3389024f66d7ef7102fa) fix(clients): preserve manual disables when integrating renewal fixes - [`c3ee71a`](https://github.com/MHSanaei/3x-ui/commit/c3ee71a397a7fd4b92eac6c2bf21700b9be97dfb) Merge remote-tracking branch 'origin/main' into codex/review-pr-6987-20261105 ### 📊 Changes **13 files changed** (+684 additions, -92 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/client_crud.go` (+15 -0) 📝 `internal/web/service/client_inbound_apply.go` (+28 -1) ➕ `internal/web/service/client_renewal_reenable.go` (+60 -0) 📝 `internal/web/service/client_traffic.go` (+15 -10) 📝 `internal/web/service/client_traffic_reset_order_test.go` (+1 -1) ➕ `internal/web/service/client_update_reenable_test.go` (+284 -0) 📝 `internal/web/service/inbound_disable.go` (+42 -0) 📝 `internal/web/service/inbound_node.go` (+3 -1) 📝 `internal/web/service/inbound_traffic.go` (+64 -47) 📝 `internal/web/service/inbound_traffic_global.go` (+1 -1) 📝 `internal/web/service/traffic_writer.go` (+74 -19) 📝 `internal/web/service/traffic_writer_test.go` (+95 -11) 📝 `internal/web/service/tuic_traffic_receipt.go` (+2 -1) </details> ### 📄 Description ## Summary Two fixes for the renewal workflow on a VPN reseller panel: 1. **A renewed client that the quota loop had switched off is re-enabled by the renewal itself**, in the single-client paths that were missing the #5619 set-difference rule. 2. **Saving / resetting a client no longer does the re-enable's work twice**, and operator writes no longer queue behind the 5s traffic poll and the per-node snapshot merges that arrive every 5s. ## Why When a client exhausts its traffic or its duration, the enforcement loop disables it in all three stores (inbound settings JSON, `client_traffics`, `clients`) and drops the user from the running core. `bulkAdjust` already re-enabled such a client when the adjustment lifted it back within limits (#5619), and a traffic reset re-enabled it too — but only by running a second, full client update behind the reset. Renewing the client from the client editor (or via the single-field renewal endpoints the Telegram bot uses: set expiry / set traffic limit) left it disabled: the update wrote the fresh expiry/quota but kept the off switch, so the operator had to flip the enable switch by hand. This is the same gap as #5619 on the paths that endpoint covers. The renewal actions were also slow: a reset of one client on N inbounds cost N serialized write slots for the counters **and** N more for the follow-up full update (N inbound JSON rewrites, N stat-row re-assertions, N runtime adds). On a node-heavy panel every one of those writes could additionally wait behind the next batch of 5s traffic polls / node snapshot merges in the single serial writer (#6035), so a save or reset "took so much time". ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] 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 - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [x] Telegram bot ## Changes **Re-enable on renewal (`fix(clients)`)** — the #5619 set-difference rule applied to the renewal paths that still lacked it: - New `ClientService.renewalReenables`: the client's enforcement row must carry the off switch AND match the production depletion predicate (including a fresh cross-panel push, exactly as the loop itself reads it), and the post-update total/expiry must clear the depletion (projected against the current usage and the pushed usage; usage is untouched by an update). A client the operator disabled while it was still within its limits, or one still depleted after the update, stays off. - `ClientService.Update` (the editor save and `POST /clients/update/:email`, local and node inbounds alike) forces `enable` on in that case; the existing per-inbound apply then flips the JSON entry, the `client_traffics` row and the `clients` record and re-adds the user on the running core (or pushes the node), so the re-enable lands in all stores at once. - The no-inbound update branch also re-asserts the enforcement row's `enable` flag — only the inbound path ever wrote it before. - `ResetClientExpiryTimeByEmail` / `ResetClientTrafficLimitByEmail` (Telegram bot renewals) flip the stored entry back on with the renewal instead of leaving it off. A note on the `enable` override: the guard cannot distinguish an `enable:false` the editor round-tripped from one a caller chose deliberately, so a renewal that clears the depletion turns the client on even when the request carries `enable:false` (the editor always round-trips the stored value, which is off for a quota-disabled client). This matches the panel-wide invariant that a client back within its limits is on — the same one `bulkAdjust`, the traffic resets and the calendar auto-renew already implement (#5619). A caller who wants a renewed client to stay off (e.g. payment not cleared yet) must disable it again after the renewal; nothing in the request or response reports the override, which is a known trade-off of this rule. **Faster renewal actions (`perf(clients)`)**: - `InboundService.resetClientTrafficLocked` now flips this inbound's stored entry and the shared client record back on in the reset's own transaction (the re-enable twin of the disable pass's `markClientsDisabledInSettings`), re-adds the user on a local core, and marks the node dirty on a node inbound. The flip is decided **per stored entry**, not from the shared enforcement row: the first inbound's reset turns that row back on, and a row-keyed check would leave every sibling inbound's off entry stale, cutting the client off there until a full client update. `ClientService.ResetTrafficByEmail` only still runs the follow-up full update when the resets left the record off (an inbound-less client, or a record no inbound hosted) — the common renewal case drops N serialized writes, N JSON rewrites and N runtime adds. - The serial traffic writer keeps its single-goroutine ordering (what keeps the poll and client mutations from deadlocking Postgres, c5d31de4e) but now serves two lanes — panel-initiated writes jump queued traffic-poll and node-merge work, so a save or reset on a node-heavy panel no longer waits on the next batch of 5s snapshots (#6035). Within each lane the order stays FIFO; on stop the drain empties both lanes, so no queued write is dropped. - Known trade-offs: a sustained burst of admin writes can delay the periodic poll / node merges (no cap on the priority), and a poll captured before a reset can now land after it, adding pre-reset deltas onto the zeroed row for one window — the same transient the base had, just widened to the queue wait. ## How was this tested? - New tests in `client_update_reenable_test.go`: an expired client extended in the editor comes back on in all three stores; a raised quota does the same; an extension that still lands in the past / a quota still below usage / a manual switch-off of a not-depleted client / a fresh cross-panel push above the new quota all stay disabled; the no-inbound case re-asserts the enforcement row; both single-field setters re-enable and both respect a manual disable; and a multi-inbound client comes back on **every** inbound after a reset (the per-entry flip). - `TestResetTrafficOfDepletedClientSurvivesTickMidReset` keeps proving that a tick landing mid-reset cannot re-disable the client (all three stores read enabled at 0 afterwards). - `TestTrafficWriterAdminLanePreemptsBackgroundQueue`: with both lanes filled, the admin write is served before the background one. `TestTrafficWriterStopDrainsEveryQueuedWrite`: a stop drains every queued write on both lanes. - `TestTrafficResetKeepsTheReverseTag` (and the reverse-tag suite) keep proving the drift case — entry on, enforcement row off — still plans the runtime re-add, which is what rebuilds a reverse tag on reset. - Full `internal/web/service`, `internal/web/runtime`, `internal/web/job`, `internal/web/controller` suites pass, plus a `-race` run over the same packages, and a full-package run of the `go-test` gate. - Both Postgres CI gate test sets (`postgres-durable-first` and `postgres-migration` from `.github/workflows/ci.yml`) pass locally against a UTF8 Postgres 17 instance. (Note for anyone reproducing on Windows: the server must be created with `initdb -E UTF8` — a locale-encoded database makes the `pgx`-based tests fail with "simple protocol queries must be run with client_encoding=UTF8" before they even connect to the panel's schema.) - The `race` gate passes locally over the touched packages; the `fuzz-smoke` gate (both fuzzers, 30s) and the pinned `golangci-lint` v2.15.0 pass over the whole repo. `node-e2e` cannot run on the local Windows-on-ARM machine, so it — along with the whole fork-PR CI suite — is validated when a maintainer approves the workflow run. ## Breaking changes None. Renewal re-enabling is strictly the set-difference the bulk paths already promised; manually disabled clients and still-depleted renewals are untouched. The one behavior change is documented above: a renewal that clears the depletion takes precedence over a round-tripped `enable:false`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #6987] Re-enable depleted clients on renewal, and stop operator writes queueing behind the 5s poll and node merges to [PR #6987] [MERGED] Re-enable depleted clients on renewal, and stop operator writes queueing behind the 5s poll and node merges 2026-11-07 14:34:00 +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#6565
No description provided.