[PR #6240] [MERGED] feat(clients): give each client its own traffic reset cycle #6195

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6240
Author: @n0ctal
Created: 8/17/2026
Status: ✅ Merged
Merged: 8/18/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-per-client-traffic-reset


📝 Commits (4)

  • 256406f feat(clients): give each client its own traffic reset cycle
  • 001adf4 fix(clients): make the per-client reset cycle editable and safe to run
  • 3078d1e Merge branch 'main' into upstream-per-client-traffic-reset
  • abaaec6 fix(clients): validate renew fields on the bulk and import paths too

📊 Changes

20 files changed (+761 additions, -74 deletions)

View changed files

📝 frontend/public/openapi.json (+24 -0)
📝 frontend/src/generated/examples.ts (+4 -0)
📝 frontend/src/generated/schemas.ts (+24 -0)
📝 frontend/src/generated/types.ts (+4 -0)
📝 frontend/src/generated/zod.ts (+4 -0)
📝 frontend/src/pages/clients/ClientBulkAddModal.tsx (+25 -1)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+35 -1)
📝 frontend/src/pages/inbounds/form/InboundFormModal.tsx (+1 -2)
📝 frontend/src/schemas/client.ts (+6 -0)
📝 frontend/src/schemas/primitives/index.ts (+1 -0)
➕ frontend/src/schemas/primitives/traffic-reset.ts (+7 -0)
📝 internal/database/db.go (+19 -0)
📝 internal/database/model/model.go (+90 -69)
➕ internal/web/job/periodic_traffic_reset_client_test.go (+206 -0)
📝 internal/web/job/periodic_traffic_reset_job.go (+61 -1)
📝 internal/web/service/client_bulk.go (+12 -0)
📝 internal/web/service/client_crud.go (+64 -0)
📝 internal/web/service/client_link.go (+8 -0)
📝 internal/web/service/client_portable.go (+12 -0)
➕ internal/web/service/client_traffic_cycle_test.go (+154 -0)

📄 Description

Closes #5497.

Summary

Traffic reset lives on the inbound, so every client sharing one resets together. The issue's case is an operator running a monthly 1000GB plan next to a weekly 200GB plan: today that means pressing Reset Traffic by hand, per client, on a schedule kept in someone's head.

Clients now carry their own cycle.

Shape

The client fields are the inbound fields: trafficReset (never | hourly | daily | weekly | monthly) and trafficResetDay for the monthly case, same vocabulary, same clamping, same monthly due-day rule via the existing monthlyResetDue. PeriodicTrafficResetJob already runs once per period; it now makes a second pass over the clients whose own cycle matches that period, after the inbound pass.

A client left at never behaves exactly as before — only its inbound's schedule touches it. A client whose own cycle happens to match its inbound's is reset twice in the same tick, which is harmless because the reset is idempotent, and I preferred that to making one schedule quietly suppress the other.

What I left out, and why

The issue also asks for cron expressions and a choice of post-reset quota. I did not implement either. Both are product decisions rather than mechanics, and neither has an inbound-level counterpart, so adding them only for clients would put two different scheduling languages in one panel. The weekly day-of-week choice is out for the same reason: inbound weekly resets fire on the @weekly boundary, and a client-only weekday would diverge from it. Happy to add any of them if you want them — I would rather ask than guess.

Persistence

The fields are on ClientRecord, not only in the inbound settings JSON. That is deliberate and load-bearing: the paths that rebuild a client from the record — an ordinary edit, an attach, a traffic reset — write back whatever the record holds, so a field that lives only in the JSON is silently zeroed by routine panel use and the operator finds out a billing cycle later.

An unknown period is rejected rather than coerced to never. A coerced value leaves a field that reads as configured while no job ever selects the client, which is the worst of the three options.

Validation

Three job tests against a real DB: a weekly client inside a never inbound is reset by the weekly run while a monthly client is not; a client at never survives all four periods; a monthly client is reset on its own day and not on another's.

Mutation-checked — removing the client pass fails with weekly client not reset by the weekly run: up=500 down=700, and removing the monthly due-day filter fails with the not-due client reset.

Persistence is covered by driving the actual edit path (hydrate the record, change a comment, save) and asserting the inbound JSON, the record and client_traffics; dropping the field from ToClient fails with inbound settings hold ""/15 after an unrelated edit. Validation is table-driven over the accepted and rejected shapes.

go build ./..., go vet, golangci-lint, the full Go suite, frontend typecheck, eslint and 1158 unit tests all green.


🔄 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/6240 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/17/2026 **Status:** ✅ Merged **Merged:** 8/18/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-per-client-traffic-reset` --- ### 📝 Commits (4) - [`256406f`](https://github.com/MHSanaei/3x-ui/commit/256406f2669e3349866b0bf54bcddc25071bcd4d) feat(clients): give each client its own traffic reset cycle - [`001adf4`](https://github.com/MHSanaei/3x-ui/commit/001adf4cf278263ea7bac82a9629ac2f011e7255) fix(clients): make the per-client reset cycle editable and safe to run - [`3078d1e`](https://github.com/MHSanaei/3x-ui/commit/3078d1ed5a05de9636742cba06c554fa2c59f99b) Merge branch 'main' into upstream-per-client-traffic-reset - [`abaaec6`](https://github.com/MHSanaei/3x-ui/commit/abaaec64190fe44bfb6ea5ecfab638029e2080be) fix(clients): validate renew fields on the bulk and import paths too ### 📊 Changes **20 files changed** (+761 additions, -74 deletions) <details> <summary>View changed files</summary> 📝 `frontend/public/openapi.json` (+24 -0) 📝 `frontend/src/generated/examples.ts` (+4 -0) 📝 `frontend/src/generated/schemas.ts` (+24 -0) 📝 `frontend/src/generated/types.ts` (+4 -0) 📝 `frontend/src/generated/zod.ts` (+4 -0) 📝 `frontend/src/pages/clients/ClientBulkAddModal.tsx` (+25 -1) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+35 -1) 📝 `frontend/src/pages/inbounds/form/InboundFormModal.tsx` (+1 -2) 📝 `frontend/src/schemas/client.ts` (+6 -0) 📝 `frontend/src/schemas/primitives/index.ts` (+1 -0) ➕ `frontend/src/schemas/primitives/traffic-reset.ts` (+7 -0) 📝 `internal/database/db.go` (+19 -0) 📝 `internal/database/model/model.go` (+90 -69) ➕ `internal/web/job/periodic_traffic_reset_client_test.go` (+206 -0) 📝 `internal/web/job/periodic_traffic_reset_job.go` (+61 -1) 📝 `internal/web/service/client_bulk.go` (+12 -0) 📝 `internal/web/service/client_crud.go` (+64 -0) 📝 `internal/web/service/client_link.go` (+8 -0) 📝 `internal/web/service/client_portable.go` (+12 -0) ➕ `internal/web/service/client_traffic_cycle_test.go` (+154 -0) </details> ### 📄 Description Closes #5497. ## Summary Traffic reset lives on the inbound, so every client sharing one resets together. The issue's case is an operator running a monthly 1000GB plan next to a weekly 200GB plan: today that means pressing **Reset Traffic** by hand, per client, on a schedule kept in someone's head. Clients now carry their own cycle. ## Shape The client fields are the inbound fields: `trafficReset` (`never | hourly | daily | weekly | monthly`) and `trafficResetDay` for the monthly case, same vocabulary, same clamping, same monthly due-day rule via the existing `monthlyResetDue`. `PeriodicTrafficResetJob` already runs once per period; it now makes a second pass over the clients whose own cycle matches that period, after the inbound pass. A client left at `never` behaves exactly as before — only its inbound's schedule touches it. A client whose own cycle happens to match its inbound's is reset twice in the same tick, which is harmless because the reset is idempotent, and I preferred that to making one schedule quietly suppress the other. ## What I left out, and why The issue also asks for cron expressions and a choice of post-reset quota. I did not implement either. Both are product decisions rather than mechanics, and neither has an inbound-level counterpart, so adding them only for clients would put two different scheduling languages in one panel. The weekly day-of-week choice is out for the same reason: inbound weekly resets fire on the `@weekly` boundary, and a client-only weekday would diverge from it. Happy to add any of them if you want them — I would rather ask than guess. ## Persistence The fields are on `ClientRecord`, not only in the inbound settings JSON. That is deliberate and load-bearing: the paths that rebuild a client from the record — an ordinary edit, an attach, a traffic reset — write back whatever the record holds, so a field that lives only in the JSON is silently zeroed by routine panel use and the operator finds out a billing cycle later. An unknown period is rejected rather than coerced to `never`. A coerced value leaves a field that reads as configured while no job ever selects the client, which is the worst of the three options. ## Validation Three job tests against a real DB: a weekly client inside a `never` inbound is reset by the weekly run while a monthly client is not; a client at `never` survives all four periods; a monthly client is reset on its own day and not on another's. Mutation-checked — removing the client pass fails with `weekly client not reset by the weekly run: up=500 down=700`, and removing the monthly due-day filter fails with the not-due client reset. Persistence is covered by driving the actual edit path (hydrate the record, change a comment, save) and asserting the inbound JSON, the record and `client_traffics`; dropping the field from `ToClient` fails with `inbound settings hold ""/15 after an unrelated edit`. Validation is table-driven over the accepted and rejected shapes. `go build ./...`, `go vet`, `golangci-lint`, the full Go suite, frontend typecheck, eslint and 1158 unit tests all green. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:01 +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#6195
No description provided.