[PR #6304] [MERGED] feat(sub): leastLoad member weights for subscription balancers #6214

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6304
Author: @DIMFLIX
Created: 8/24/2026
Status: ✅ Merged
Merged: 9/2/2026
Merged by: @MHSanaei

Base: main ← Head: feat/sub-balancer-weights


📝 Commits (10+)

  • 5ca7b7c feat(model): add MemberWeights to SubBalancer
  • b46f14b feat(sub): accept memberWeights on the sub-balancer API
  • 7c54352 feat(sub): emit leastLoad strategy costs from member weights
  • a746493 feat(sub-balancers): leastLoad member weight inputs
  • 2cef3d1 docs(api): document memberWeights on sub-balancers
  • 6b19963 fix(api-docs): use the allowed object ParamType for memberWeights
  • b7de258 fix(sub-balancers): cap the member-weight list height
  • 3480dad fix(sub): anchor leastLoad cost matches to exact member tags
  • befbf7d fix(sub-balancers): keep member weights across the enabled toggle
  • 3d05e8c fix(sub-balancers): address review on member weights

📊 Changes

32 files changed (+525 additions, -15 deletions)

View changed files

📝 docs/content/docs/en/reference/api/subscription-balancers.mdx (+6 -4)
📝 docs/public/openapi.json (+13 -1)
📝 frontend/public/openapi.json (+13 -1)
📝 frontend/src/api/queries/useSubBalancerMutations.ts (+12 -4)
📝 frontend/src/generated/examples.ts (+1 -0)
📝 frontend/src/generated/schemas.ts (+7 -0)
📝 frontend/src/generated/types.ts (+1 -0)
📝 frontend/src/generated/zod.ts (+1 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+7 -1)
📝 frontend/src/pages/settings/SubBalancerFormModal.tsx (+73 -2)
📝 frontend/src/pages/settings/SubscriptionBalancersTab.tsx (+1 -0)
📝 frontend/src/schemas/subBalancer.ts (+10 -0)
📝 frontend/src/test/sub-balancer-form-modal.test.tsx (+72 -0)
📝 internal/database/model/model.go (+4 -1)
📝 internal/sub/json_service.go (+43 -1)
📝 internal/sub/sub_balancer_test.go (+88 -0)
📝 internal/web/controller/sub_balancer.go (+11 -0)
📝 internal/web/service/sub_balancer.go (+35 -0)
📝 internal/web/service/sub_balancer_test.go (+88 -0)
📝 internal/web/translation/ar-EG.json (+3 -0)

...and 12 more files

📄 Description

Summary

Adds admin-configurable static weights (xray costs) to sub-balancers, applied only when the strategy is leastLoad — the only strategy whose StrategySettingsObject supports them.

  • Model: SubBalancer.MemberWeights map[int]float64, same gorm json-serializer pattern as InboundIds; AutoMigrate adds the column on every dialect. Absent entries mean weight 1.0; old balancers keep working unchanged.
  • API: parsed as one JSON form field (memberWeights":"{"3":0.2}"). Validation: weights with any strategy but leastLoad are rejected (xray would silently ignore them); weights <= 0 are rejected — xray's WeightManager derives a bogus auto-weight from the first digit of the matched tag when value is not > 0; entries for inbounds no longer selected are dropped on save.
  • Emission: costs are built after the tagging loop and reuse the exact retagged member tags. Matches are emitted as anchored regexps (^bal-N-tag$): xray's plain cost matching is substring-based, so a bare bal-1-vless would also swallow the deduplicated bal-1-vless-2. Costs are omitted entirely unless at least one explicit weight survives — no all-1.0 arrays.
  • UI: weight inputs (min 0.1, step 0.1) appear next to the inbound picker only under leastLoad; switching strategy hides them without losing values until save, and non-leastLoad submits strip them. Long lists scroll inside a 220px viewport mirroring the picker's listHeight. Tooltip explains lower = preferred.
  • i18n: three new keys added to all 13 locales in the same commit.
  • Docs: endpoints.ts updated, openapi artifacts regenerated (make gen + docs copy + gen:api).

Testing

  • Go: exact costs[] structure (tags + values + anchored regexp), no settings without weights, emission-side guard for non-leastLoad rows written directly to DB, validation rejections (<=0, wrong strategy), stray-id dropping, weight clear on update without the field.
  • Frontend: weight inputs appear only under leastLoad, omitted from submit under other strategies, seeded values on edit; i18n dead-keys suite green.
  • Full local gate (make verify) green except the known post-run teardown flake in balancer-form-modal.test.tsx (all tests pass).

Follow-up to #6243.


🔄 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/6304 **Author:** [@DIMFLIX](https://github.com/DIMFLIX) **Created:** 8/24/2026 **Status:** ✅ Merged **Merged:** 9/2/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/sub-balancer-weights` --- ### 📝 Commits (10+) - [`5ca7b7c`](https://github.com/MHSanaei/3x-ui/commit/5ca7b7c2fbf9d1035d27483f906c7bf8a6775c87) feat(model): add MemberWeights to SubBalancer - [`b46f14b`](https://github.com/MHSanaei/3x-ui/commit/b46f14bf115f8f04e41d9ab63091a31730c065a4) feat(sub): accept memberWeights on the sub-balancer API - [`7c54352`](https://github.com/MHSanaei/3x-ui/commit/7c543520ab48892c9c579ade210c00a7115b73c6) feat(sub): emit leastLoad strategy costs from member weights - [`a746493`](https://github.com/MHSanaei/3x-ui/commit/a746493bd2af079bc77e1dd5ed60bf85a0cb39ea) feat(sub-balancers): leastLoad member weight inputs - [`2cef3d1`](https://github.com/MHSanaei/3x-ui/commit/2cef3d1a71756eea8d8b8316ef92dde34cd3f141) docs(api): document memberWeights on sub-balancers - [`6b19963`](https://github.com/MHSanaei/3x-ui/commit/6b19963fb9c402963bba0071fca6def50e662495) fix(api-docs): use the allowed object ParamType for memberWeights - [`b7de258`](https://github.com/MHSanaei/3x-ui/commit/b7de25803d630d521bbf44b5c279004c1d720ac3) fix(sub-balancers): cap the member-weight list height - [`3480dad`](https://github.com/MHSanaei/3x-ui/commit/3480dad05cd45a346e039210f5c54ef3012ec289) fix(sub): anchor leastLoad cost matches to exact member tags - [`befbf7d`](https://github.com/MHSanaei/3x-ui/commit/befbf7dba48f3473df04abd06cadc717ac1fe5ba) fix(sub-balancers): keep member weights across the enabled toggle - [`3d05e8c`](https://github.com/MHSanaei/3x-ui/commit/3d05e8cdd80ee6a0b638840bfe658f949c551edc) fix(sub-balancers): address review on member weights ### 📊 Changes **32 files changed** (+525 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/reference/api/subscription-balancers.mdx` (+6 -4) 📝 `docs/public/openapi.json` (+13 -1) 📝 `frontend/public/openapi.json` (+13 -1) 📝 `frontend/src/api/queries/useSubBalancerMutations.ts` (+12 -4) 📝 `frontend/src/generated/examples.ts` (+1 -0) 📝 `frontend/src/generated/schemas.ts` (+7 -0) 📝 `frontend/src/generated/types.ts` (+1 -0) 📝 `frontend/src/generated/zod.ts` (+1 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+7 -1) 📝 `frontend/src/pages/settings/SubBalancerFormModal.tsx` (+73 -2) 📝 `frontend/src/pages/settings/SubscriptionBalancersTab.tsx` (+1 -0) 📝 `frontend/src/schemas/subBalancer.ts` (+10 -0) 📝 `frontend/src/test/sub-balancer-form-modal.test.tsx` (+72 -0) 📝 `internal/database/model/model.go` (+4 -1) 📝 `internal/sub/json_service.go` (+43 -1) 📝 `internal/sub/sub_balancer_test.go` (+88 -0) 📝 `internal/web/controller/sub_balancer.go` (+11 -0) 📝 `internal/web/service/sub_balancer.go` (+35 -0) 📝 `internal/web/service/sub_balancer_test.go` (+88 -0) 📝 `internal/web/translation/ar-EG.json` (+3 -0) _...and 12 more files_ </details> ### 📄 Description ## Summary Adds admin-configurable static weights (xray `costs`) to sub-balancers, applied only when the strategy is `leastLoad` — the only strategy whose `StrategySettingsObject` supports them. - **Model**: `SubBalancer.MemberWeights map[int]float64`, same gorm json-serializer pattern as `InboundIds`; AutoMigrate adds the column on every dialect. Absent entries mean weight 1.0; old balancers keep working unchanged. - **API**: parsed as one JSON form field (`memberWeights":"{"3":0.2}"`). Validation: weights with any strategy but `leastLoad` are rejected (xray would silently ignore them); weights <= 0 are rejected — xray's WeightManager derives a bogus auto-weight from the first digit of the matched tag when value is not > 0; entries for inbounds no longer selected are dropped on save. - **Emission**: costs are built after the tagging loop and reuse the exact retagged member tags. Matches are emitted as anchored regexps (`^bal-N-tag$`): xray's plain cost matching is substring-based, so a bare `bal-1-vless` would also swallow the deduplicated `bal-1-vless-2`. Costs are omitted entirely unless at least one explicit weight survives — no all-1.0 arrays. - **UI**: weight inputs (min 0.1, step 0.1) appear next to the inbound picker only under leastLoad; switching strategy hides them without losing values until save, and non-leastLoad submits strip them. Long lists scroll inside a 220px viewport mirroring the picker's listHeight. Tooltip explains lower = preferred. - **i18n**: three new keys added to all 13 locales in the same commit. - **Docs**: endpoints.ts updated, openapi artifacts regenerated (`make gen` + docs copy + gen:api). ## Testing - Go: exact costs[] structure (tags + values + anchored regexp), no settings without weights, emission-side guard for non-leastLoad rows written directly to DB, validation rejections (<=0, wrong strategy), stray-id dropping, weight clear on update without the field. - Frontend: weight inputs appear only under leastLoad, omitted from submit under other strategies, seeded values on edit; i18n dead-keys suite green. - Full local gate (`make verify`) green except the known post-run teardown flake in `balancer-form-modal.test.tsx` (all tests pass). Follow-up to #6243. --- <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:09 +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#6214
No description provided.