mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-02 09:48:43 +00:00
leastLoad balancer: Implement missing tolerance (#6156)
Closes https://github.com/XTLS/Xray-core/issues/6154
This commit is contained in:
@@ -3,6 +3,7 @@ package router
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"math"
|
"math"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -77,7 +78,7 @@ func (s *LeastLoadStrategy) PickOutbound(candidates []string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *LeastLoadStrategy) pickOutbounds(candidates []string) []*node {
|
func (s *LeastLoadStrategy) pickOutbounds(candidates []string) []*node {
|
||||||
qualified := s.getNodes(candidates, time.Duration(s.settings.MaxRTT))
|
qualified := s.getNodes(candidates)
|
||||||
selects := s.selectLeastLoad(qualified)
|
selects := s.selectLeastLoad(qualified)
|
||||||
return selects
|
return selects
|
||||||
}
|
}
|
||||||
@@ -138,7 +139,7 @@ func (s *LeastLoadStrategy) selectLeastLoad(nodes []*node) []*node {
|
|||||||
return nodes[:count]
|
return nodes[:count]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration) []*node {
|
func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
|
||||||
if s.observer == nil {
|
if s.observer == nil {
|
||||||
errors.LogError(s.ctx, "observer is nil")
|
errors.LogError(s.ctx, "observer is nil")
|
||||||
return make([]*node, 0)
|
return make([]*node, 0)
|
||||||
@@ -151,12 +152,10 @@ func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration)
|
|||||||
|
|
||||||
results := observeResult.(*observatory.ObservationResult)
|
results := observeResult.(*observatory.ObservationResult)
|
||||||
|
|
||||||
outboundlist := outboundList(candidates)
|
|
||||||
|
|
||||||
var ret []*node
|
var ret []*node
|
||||||
|
|
||||||
for _, v := range results.Status {
|
for _, v := range results.Status {
|
||||||
if v.Alive && (v.Delay < maxRTT.Milliseconds() || maxRTT == 0) && outboundlist.contains(v.OutboundTag) {
|
if s.shouldSelectNode(v, candidates) {
|
||||||
record := &node{
|
record := &node{
|
||||||
Tag: v.OutboundTag,
|
Tag: v.OutboundTag,
|
||||||
CountAll: 1,
|
CountAll: 1,
|
||||||
@@ -172,8 +171,8 @@ func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration)
|
|||||||
record.RTTDeviationCost = time.Duration(s.costs.Apply(v.OutboundTag, float64(v.HealthPing.Deviation)))
|
record.RTTDeviationCost = time.Duration(s.costs.Apply(v.OutboundTag, float64(v.HealthPing.Deviation)))
|
||||||
record.CountAll = int(v.HealthPing.All)
|
record.CountAll = int(v.HealthPing.All)
|
||||||
record.CountFail = int(v.HealthPing.Fail)
|
record.CountFail = int(v.HealthPing.Fail)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = append(ret, record)
|
ret = append(ret, record)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -182,6 +181,23 @@ func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration)
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LeastLoadStrategy) shouldSelectNode(v *observatory.OutboundStatus, candidates []string) bool {
|
||||||
|
maxRTT := time.Duration(s.settings.MaxRTT)
|
||||||
|
if !v.Alive {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if maxRTT != 0 && v.Delay >= maxRTT.Milliseconds() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !slices.Contains(candidates, v.OutboundTag) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if v.HealthPing != nil && v.HealthPing.All > 0 && s.settings.Tolerance > 0 && float64(v.HealthPing.Fail)/float64(v.HealthPing.All) > float64(s.settings.Tolerance) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func leastloadSort(nodes []*node) {
|
func leastloadSort(nodes []*node) {
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
sort.Slice(nodes, func(i, j int) bool {
|
||||||
left := nodes[i]
|
left := nodes[i]
|
||||||
|
|||||||
Reference in New Issue
Block a user