[GH-ISSUE #6387] [Feature]: Add support for priority/preferential outgoing requests for load balancers. #4333

Open
opened 2026-10-31 20:38:54 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @ad0mant on GitHub (Sep 2, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6387

When creating/editing a load balancer, add the optional parameter Outgoing traffic priority.

Describe the solution you'd like

Balancer

Outgoing traffic:

[ Server A ]

[ Server B ]

[ Server C ]

[ Server D ]

Outgoing traffic priority:

[ Server A ]

Priority RTT threshold:

[ 300 ] ms

Alternatives you've considered

Current workaround

Currently, I can achieve similar behavior by creating a separate balancer for each user and using another balancer as a backup.

For example:

Balancer User 1
Selector: A
Backup: General balancer

General balancer
Selectors: A, B, C, D
Strategy: Least load

However, this requires backup switching between balancers and a feedback mechanism.

3x-ui issues a warning:

Traffic will be routed as follows: Balancer → Feedback → Server → Target balancer → Outgoing connection.

This works, but it’s overly complex for such a simple use case as “preferred server → backup pool”.

Concrete use case

For example:

Outgoing traffic priority: Server A
Fallback option: General load balancer
RTT threshold: 300 ms

Behavior:

If server A is available and its RTT is below 300 ms → use server A.
If server A is unavailable → use the backup load balancer.
If the RTT of server A exceeds 300 ms → use the backup load balancer.
Then, the backup balancer selects an available server in accordance with its configured strategy, for example, “Least Load.”

The RTT threshold should be optional. If it is disabled, switching to the backup server should occur only when the outgoing traffic priority is considered unavailable..

This would allow for a simple configuration, for example:

User 1 → Balancer 1
Priority: Server A

User 2 → Load Balancer 2
Priority: Server B

User 3 → Load Balancer 3
Priority: Server C

Mockups, screenshots, or examples

No response

Which parts of the panel would this affect?

Frontend (UI / panel pages)

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 @ad0mant on GitHub (Sep 2, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6387 ### Is your feature request related to a problem? When creating/editing a load balancer, add the optional parameter **Outgoing traffic priority**. ### Describe the solution you'd like Balancer Outgoing traffic: [ Server A ] [ Server B ] [ Server C ] [ Server D ] Outgoing traffic priority: [ Server A ] Priority RTT threshold: [ 300 ] ms ### Alternatives you've considered Current workaround Currently, I can achieve similar behavior by creating a separate balancer for each user and using another balancer as a backup. For example: Balancer User 1 Selector: A Backup: General balancer General balancer Selectors: A, B, C, D Strategy: Least load However, this requires backup switching between balancers and a feedback mechanism. 3x-ui issues a warning: Traffic will be routed as follows: Balancer → Feedback → Server → Target balancer → Outgoing connection. This works, but it’s overly complex for such a simple use case as “preferred server → backup pool”. ### Concrete use case For example: Outgoing traffic priority: Server A Fallback option: General load balancer RTT threshold: 300 ms Behavior: If server A is available and its RTT is below 300 ms → use server A. If server A is unavailable → use the backup load balancer. If the RTT of server A exceeds 300 ms → use the backup load balancer. Then, the backup balancer selects an available server in accordance with its configured strategy, for example, “Least Load.” The RTT threshold should be optional. If it is disabled, switching to the backup server should occur only when the outgoing traffic priority is considered unavailable.. This would allow for a simple configuration, for example: User 1 → Balancer 1 Priority: Server A User 2 → Load Balancer 2 Priority: Server B User 3 → Load Balancer 3 Priority: Server C ### Mockups, screenshots, or examples _No response_ ### Which parts of the panel would this affect? Frontend (UI / panel pages) ### 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 (Sep 2, 2026):

Thanks for the detailed write-up — this is a valid feature request, but almost everything you describe is already expressible with fields the Balancer form ships today, so what is actually missing is a shortcut in the UI rather than new routing capability. Details below, with the caveat that one of the knobs (costs) has a semantic I could not verify from this repository.

What already exists

The balancer object the panel writes is Xray-core's own shape — tag, selector, fallbackTag, strategy: { type, settings } and nothing else. See frontend/src/pages/xray/balancers/BalancersTab.tsx:235-238:

if (form.strategy && form.strategy !== 'random') {
  wire.strategy = { type: form.strategy };
  if (form.strategy === 'leastLoad' && form.settings) {
    wire.strategy.settings = form.settings;

and the schema at frontend/src/schemas/routing.ts:69-75.

  1. RTT threshold — this is settings.maxRTT, already in the form as Max RTT (frontend/src/pages/xray/balancers/BalancerFormModal.tsx:295-304, label "maxRtt": "Max RTT" at internal/web/translation/en-US.json:1896, schema maxRTT: z.string().optional() at frontend/src/schemas/routing.ts:56). The field is a free-form Xray duration string — the placeholder is e.g. 1s, and the panel does not validate it, so 300ms goes through as typed.

  2. Preferring one member — this is settings.costs, already in the form as Costs, with a per-entry Tag pattern, Weight and a literal/regex switch (BalancerFormModal.tsx:353-418; internal/web/translation/en-US.json:1899-1902; schema frontend/src/schemas/routing.ts:47-51).

  3. Per-user mapping — routing rules already match on the client, so “User 1 → Balancer 1” needs no new mechanism: RuleObjectSchema has user: z.array(z.string()).optional() (frontend/src/schemas/routing.ts:26) and the rule form exposes it as User (frontend/src/pages/xray/routing/RuleFormModal.tsx:299-311, internal/web/translation/en-US.json:1706).

So your use case collapses into a single balancer, no second balancer and no fallback hop:

{
  "tag": "user1",
  "selector": ["A", "B", "C", "D"],
  "strategy": {
    "type": "leastLoad",
    "settings": {
      "expected": 1,
      "maxRTT": "300ms",
      "costs": [{ "regexp": false, "match": "A", "value": 0.1 }]
    }
  }
}

Two constraints worth knowing before you try it:

  • settings is kept only for Least load — BalancerFormModal.tsx:193 is literally if (result.strategy !== 'leastLoad') delete result.settings;, so an RTT threshold is not available on Random / Round-robin.
  • Least load needs a burstObservatory, but you do not have to create one: the panel adds and removes it for you from your balancer strategies (frontend/src/pages/xray/balancers/balancer-helpers.ts:62-85, and the Observatory tab says so — internal/web/translation/en-US.json:1849).

What I could not determine: the direction of the costs weight — whether a lower or a higher value makes a tag more preferred. That is decided inside Xray-core's leastLoad strategy, which is a Go module dependency and is not part of this checkout, so I will not guess at it. You can settle it in one step on a live panel: set a weight on A, then read back which member the balancer is actually picking — the panel queries the running core for that (balancerStatus in internal/web/controller/xray_setting.go:366-386, "reports the live state (override + strategy picks)"). If the preference lands the wrong way, invert the weight.

About the message you quoted

That is an informational notice, not a warning about a misconfiguration — "balancerFallbackInfo" at internal/web/translation/en-US.json:1889. It appears only when the fallback you pick is another balancer (BalancerFormModal.tsx:167-170 and :265-272). The reason is that Xray's fallbackTag names an outbound, not a balancer, so to chain to a balancer the panel synthesizes a loopback outbound plus a routing rule — ensureBalancerLoopback at frontend/src/pages/xray/balancers/balancer-loopback.ts:71-109:

const newOutbound = { tag: lbTag, protocol: 'loopback', settings: { inboundTag: lbTag } };

Picking a plain outbound tag as the fallback instead adds no loopback and no extra hop (BalancerFormModal.tsx:140-165).

What is genuinely new here

A first-class “priority member + threshold” control that compiles down to selector + maxRTT + costs, so an operator does not have to know the leastLoad tuning fields to express “prefer A, fall back to the pool”. That is a reasonable UI ask and I have left the enhancement label in place.

One correction to the scoping in your report: you selected Frontend (UI / panel pages) only. A new balancer key cannot be frontend-only — the panel hands routing through to Xray-core as raw JSON, so a priority field would either have to be understood by Xray-core upstream, or be a panel-side control that emits the existing Xray fields.

Related but not duplicates, so I am leaving this issue open and separate: #6355 (chained balancers — a second balancing stage on the selected node) and #6305 (routing rules inside the client/subscription balancer).

Verdict: Feature request

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

<!-- gh-comment-id:5505897506 --> @github-actions[bot] commented on GitHub (Sep 2, 2026): Thanks for the detailed write-up — this is a valid feature request, but almost everything you describe is already expressible with fields the Balancer form ships today, so what is actually missing is a shortcut in the UI rather than new routing capability. Details below, with the caveat that one of the knobs (`costs`) has a semantic I could not verify from this repository. ## What already exists The balancer object the panel writes is Xray-core's own shape — `tag`, `selector`, `fallbackTag`, `strategy: { type, settings }` and nothing else. See `frontend/src/pages/xray/balancers/BalancersTab.tsx:235-238`: ```ts if (form.strategy && form.strategy !== 'random') { wire.strategy = { type: form.strategy }; if (form.strategy === 'leastLoad' && form.settings) { wire.strategy.settings = form.settings; ``` and the schema at `frontend/src/schemas/routing.ts:69-75`. 1. **RTT threshold** — this is `settings.maxRTT`, already in the form as **Max RTT** (`frontend/src/pages/xray/balancers/BalancerFormModal.tsx:295-304`, label `"maxRtt": "Max RTT"` at `internal/web/translation/en-US.json:1896`, schema `maxRTT: z.string().optional()` at `frontend/src/schemas/routing.ts:56`). The field is a free-form Xray duration string — the placeholder is `e.g. 1s`, and the panel does not validate it, so `300ms` goes through as typed. 2. **Preferring one member** — this is `settings.costs`, already in the form as **Costs**, with a per-entry *Tag pattern*, *Weight* and a literal/regex switch (`BalancerFormModal.tsx:353-418`; `internal/web/translation/en-US.json:1899-1902`; schema `frontend/src/schemas/routing.ts:47-51`). 3. **Per-user mapping** — routing rules already match on the client, so “User 1 → Balancer 1” needs no new mechanism: `RuleObjectSchema` has `user: z.array(z.string()).optional()` (`frontend/src/schemas/routing.ts:26`) and the rule form exposes it as **User** (`frontend/src/pages/xray/routing/RuleFormModal.tsx:299-311`, `internal/web/translation/en-US.json:1706`). So your use case collapses into a **single** balancer, no second balancer and no fallback hop: ```json { "tag": "user1", "selector": ["A", "B", "C", "D"], "strategy": { "type": "leastLoad", "settings": { "expected": 1, "maxRTT": "300ms", "costs": [{ "regexp": false, "match": "A", "value": 0.1 }] } } } ``` Two constraints worth knowing before you try it: - `settings` is kept **only** for Least load — `BalancerFormModal.tsx:193` is literally `if (result.strategy !== 'leastLoad') delete result.settings;`, so an RTT threshold is not available on Random / Round-robin. - Least load needs a `burstObservatory`, but you do not have to create one: the panel adds and removes it for you from your balancer strategies (`frontend/src/pages/xray/balancers/balancer-helpers.ts:62-85`, and the Observatory tab says so — `internal/web/translation/en-US.json:1849`). **What I could not determine:** the direction of the `costs` weight — whether a lower or a higher `value` makes a tag more preferred. That is decided inside Xray-core's `leastLoad` strategy, which is a Go module dependency and is not part of this checkout, so I will not guess at it. You can settle it in one step on a live panel: set a weight on `A`, then read back which member the balancer is actually picking — the panel queries the running core for that (`balancerStatus` in `internal/web/controller/xray_setting.go:366-386`, "reports the live state (override + strategy picks)"). If the preference lands the wrong way, invert the weight. ## About the message you quoted That is an informational notice, not a warning about a misconfiguration — `"balancerFallbackInfo"` at `internal/web/translation/en-US.json:1889`. It appears only when the fallback you pick is **another balancer** (`BalancerFormModal.tsx:167-170` and `:265-272`). The reason is that Xray's `fallbackTag` names an *outbound*, not a balancer, so to chain to a balancer the panel synthesizes a `loopback` outbound plus a routing rule — `ensureBalancerLoopback` at `frontend/src/pages/xray/balancers/balancer-loopback.ts:71-109`: ```ts const newOutbound = { tag: lbTag, protocol: 'loopback', settings: { inboundTag: lbTag } }; ``` Picking a plain outbound tag as the fallback instead adds no loopback and no extra hop (`BalancerFormModal.tsx:140-165`). ## What is genuinely new here A first-class “priority member + threshold” control that compiles down to `selector` + `maxRTT` + `costs`, so an operator does not have to know the `leastLoad` tuning fields to express “prefer A, fall back to the pool”. That is a reasonable UI ask and I have left the `enhancement` label in place. One correction to the scoping in your report: you selected *Frontend (UI / panel pages)* only. A new balancer key cannot be frontend-only — the panel hands `routing` through to Xray-core as raw JSON, so a `priority` field would either have to be understood by Xray-core upstream, or be a panel-side control that emits the existing Xray fields. Related but not duplicates, so I am leaving this issue open and separate: #6355 (chained balancers — a *second* balancing stage on the selected node) and #6305 (routing rules inside the client/subscription balancer). Verdict: Feature request *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
Author
Owner

@ad0mant commented on GitHub (Sep 2, 2026):

  1. Предпочтение отдается одному элементу — это settings.costsуже представленный в виде **Costs , с **шаблоном Tag для каждой записи , Weight и переключателем литерал/регулярное выражение ( BalancerFormModal.tsx:353-418; internal/web/translation/en-US.json:1899-1902; schema frontend/src/schemas/routing.ts:47-51).

Could you please tell me if I understood this correctly: we create a load balancer, select the least load mode, and choose the selectors.

In the costs parameters, we specify the cost of our servers. In the end, this means that with this load balancer, traffic will first go to the server with a cost of 10; if its RTT is exceeded, it will switch to the next one, which has a cost of 7, and so on?

Image

P.S. This setting isn’t working as expected; I’m constantly being redirected to different servers — I’m checking it via 2ip.io.

<!-- gh-comment-id:5507803920 --> @ad0mant commented on GitHub (Sep 2, 2026): > 2. **Предпочтение отдается одному элементу** — это `settings.costs`уже представленный в виде **Costs , с **_шаблоном Tag_ для каждой записи , _Weight_ и переключателем литерал/регулярное выражение ( `BalancerFormModal.tsx:353-418`; `internal/web/translation/en-US.json:1899-1902`; schema `frontend/src/schemas/routing.ts:47-51`). Could you please tell me if I understood this correctly: we create a load balancer, select the least load mode, and choose the selectors. In the costs parameters, we specify the cost of our servers. In the end, this means that with this load balancer, traffic will first go to the server with a cost of 10; if its RTT is exceeded, it will switch to the next one, which has a cost of 7, and so on? <img width="487" height="845" alt="Image" src="https://github.com/user-attachments/assets/4fe111e0-f3b6-49c1-a8c3-99adc31f0c7a" /> P.S. This setting isn’t working as expected; I’m constantly being redirected to different servers — I’m checking it via 2ip.io.
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#4333
No description provided.