Compare commits

..
2 Commits
Author SHA1 Message Date
风扇滑翔翼andGitHub b92aa0358a More 2026-05-17 13:21:49 +00:00
Fangliding 316bcd6343 Refactor Timer 2026-05-17 20:56:19 +08:00
2 changed files with 34 additions and 67 deletions
+6 -22
View File
@@ -3,7 +3,6 @@ package router
import (
"context"
"math"
"slices"
"sort"
"time"
@@ -78,7 +77,7 @@ func (s *LeastLoadStrategy) PickOutbound(candidates []string) string {
}
func (s *LeastLoadStrategy) pickOutbounds(candidates []string) []*node {
qualified := s.getNodes(candidates)
qualified := s.getNodes(candidates, time.Duration(s.settings.MaxRTT))
selects := s.selectLeastLoad(qualified)
return selects
}
@@ -139,7 +138,7 @@ func (s *LeastLoadStrategy) selectLeastLoad(nodes []*node) []*node {
return nodes[:count]
}
func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration) []*node {
if s.observer == nil {
errors.LogError(s.ctx, "observer is nil")
return make([]*node, 0)
@@ -152,10 +151,12 @@ func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
results := observeResult.(*observatory.ObservationResult)
outboundlist := outboundList(candidates)
var ret []*node
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{
Tag: v.OutboundTag,
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.CountAll = int(v.HealthPing.All)
record.CountFail = int(v.HealthPing.Fail)
}
}
ret = append(ret, record)
}
}
@@ -181,23 +182,6 @@ func (s *LeastLoadStrategy) getNodes(candidates []string) []*node {
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) {
sort.Slice(nodes, func(i, j int) bool {
left := nodes[i]
+28 -45
View File
@@ -3,11 +3,7 @@ package signal
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/task"
)
type ActivityUpdater interface {
@@ -15,45 +11,35 @@ type ActivityUpdater interface {
}
type ActivityTimer struct {
mu sync.RWMutex
updated chan struct{}
checkTask *task.Periodic
mu sync.Mutex
// timer will be nil if this timer is already finished
timer *time.Timer
timeout time.Duration
onTimeout func()
consumed atomic.Bool
once sync.Once
}
func (t *ActivityTimer) Update() {
select {
case t.updated <- struct{}{}:
default:
// someone already called Update or closing, just return
if !t.mu.TryLock() {
return
}
}
func (t *ActivityTimer) check() error {
select {
case <-t.updated:
default:
t.finish()
defer t.mu.Unlock()
if t.timer != nil {
t.timer.Reset(t.timeout)
}
return nil
}
func (t *ActivityTimer) finish() {
t.once.Do(func() {
t.consumed.Store(true)
t.mu.Lock()
defer t.mu.Unlock()
common.CloseIfExists(t.checkTask)
t.mu.Lock()
defer t.mu.Unlock()
if t.timer != nil {
t.timer.Stop()
t.onTimeout()
})
t.timer = nil
}
}
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
if t.consumed.Load() {
return
}
if timeout == 0 {
t.finish()
return
@@ -61,25 +47,22 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
t.mu.Lock()
defer t.mu.Unlock()
// double check, just in case
if t.consumed.Load() {
return
if t.timer != nil {
t.timeout = timeout
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 {
timer := &ActivityTimer{
updated: make(chan struct{}, 1),
activityTimer := &ActivityTimer{
timeout: timeout,
onTimeout: cancel,
}
timer.SetTimeout(timeout)
return timer
// strange situation
if timeout == 0 {
cancel()
return activityTimer
}
activityTimer.timer = time.AfterFunc(timeout, activityTimer.finish)
return activityTimer
}