mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-03 04:18:07 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b92aa0358a | ||
|
|
316bcd6343 |
@@ -3,7 +3,6 @@ package router
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -78,7 +77,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)
|
qualified := s.getNodes(candidates, time.Duration(s.settings.MaxRTT))
|
||||||
selects := s.selectLeastLoad(qualified)
|
selects := s.selectLeastLoad(qualified)
|
||||||
return selects
|
return selects
|
||||||
}
|
}
|
||||||
@@ -139,7 +138,7 @@ func (s *LeastLoadStrategy) selectLeastLoad(nodes []*node) []*node {
|
|||||||
return nodes[:count]
|
return nodes[:count]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
|
func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration) []*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)
|
||||||
@@ -152,10 +151,12 @@ func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
|
|||||||
|
|
||||||
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 s.shouldSelectNode(v, candidates) {
|
if v.Alive && (v.Delay < maxRTT.Milliseconds() || maxRTT == 0) && outboundlist.contains(v.OutboundTag) {
|
||||||
record := &node{
|
record := &node{
|
||||||
Tag: v.OutboundTag,
|
Tag: v.OutboundTag,
|
||||||
CountAll: 1,
|
CountAll: 1,
|
||||||
@@ -171,8 +172,8 @@ func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,23 +182,6 @@ func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
|
|||||||
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]
|
||||||
|
|||||||
+26
-43
@@ -3,11 +3,7 @@ package signal
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common"
|
|
||||||
"github.com/xtls/xray-core/common/task"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ActivityUpdater interface {
|
type ActivityUpdater interface {
|
||||||
@@ -15,45 +11,35 @@ type ActivityUpdater interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ActivityTimer struct {
|
type ActivityTimer struct {
|
||||||
mu sync.RWMutex
|
mu sync.Mutex
|
||||||
updated chan struct{}
|
// timer will be nil if this timer is already finished
|
||||||
checkTask *task.Periodic
|
timer *time.Timer
|
||||||
|
timeout time.Duration
|
||||||
onTimeout func()
|
onTimeout func()
|
||||||
consumed atomic.Bool
|
|
||||||
once sync.Once
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) Update() {
|
func (t *ActivityTimer) Update() {
|
||||||
select {
|
// someone already called Update or closing, just return
|
||||||
case t.updated <- struct{}{}:
|
if !t.mu.TryLock() {
|
||||||
default:
|
return
|
||||||
}
|
}
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
if t.timer != nil {
|
||||||
|
t.timer.Reset(t.timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) check() error {
|
|
||||||
select {
|
|
||||||
case <-t.updated:
|
|
||||||
default:
|
|
||||||
t.finish()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) finish() {
|
func (t *ActivityTimer) finish() {
|
||||||
t.once.Do(func() {
|
|
||||||
t.consumed.Store(true)
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
|
if t.timer != nil {
|
||||||
common.CloseIfExists(t.checkTask)
|
t.timer.Stop()
|
||||||
t.onTimeout()
|
t.onTimeout()
|
||||||
})
|
t.timer = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
||||||
if t.consumed.Load() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
t.finish()
|
t.finish()
|
||||||
return
|
return
|
||||||
@@ -61,25 +47,22 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
|||||||
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
// double check, just in case
|
if t.timer != nil {
|
||||||
if t.consumed.Load() {
|
t.timeout = timeout
|
||||||
return
|
t.timer.Reset(timeout)
|
||||||
}
|
}
|
||||||
newCheckTask := &task.Periodic{
|
|
||||||
Interval: timeout,
|
|
||||||
Execute: t.check,
|
|
||||||
}
|
|
||||||
common.CloseIfExists(t.checkTask)
|
|
||||||
t.checkTask = newCheckTask
|
|
||||||
t.Update()
|
|
||||||
common.Must(newCheckTask.Start())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
||||||
timer := &ActivityTimer{
|
activityTimer := &ActivityTimer{
|
||||||
updated: make(chan struct{}, 1),
|
timeout: timeout,
|
||||||
onTimeout: cancel,
|
onTimeout: cancel,
|
||||||
}
|
}
|
||||||
timer.SetTimeout(timeout)
|
// strange situation
|
||||||
return timer
|
if timeout == 0 {
|
||||||
|
cancel()
|
||||||
|
return activityTimer
|
||||||
|
}
|
||||||
|
activityTimer.timer = time.AfterFunc(timeout, activityTimer.finish)
|
||||||
|
return activityTimer
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user