Fix lint problem

This commit is contained in:
ashly-right 2024-03-21 07:40:02 +01:00
parent 0ade3706a8
commit 81e7bd5adb
No known key found for this signature in database
GPG Key ID: 2C70FC9CFBECF3DE

View File

@ -209,12 +209,12 @@ type URLTestGroup struct {
pauseManager pause.Manager pauseManager pause.Manager
selectedOutboundTCP adapter.Outbound selectedOutboundTCP adapter.Outbound
selectedOutboundUDP adapter.Outbound selectedOutboundUDP adapter.Outbound
randomize bool randomize bool
bestTCPLatencyOutbounds []adapter.Outbound bestTCPLatencyOutbounds []adapter.Outbound
bestUDPLatencyOutbounds []adapter.Outbound bestUDPLatencyOutbounds []adapter.Outbound
interruptGroup *interrupt.Group interruptGroup *interrupt.Group
interruptExternalConnections bool interruptExternalConnections bool
access sync.Mutex access sync.Mutex
ticker *time.Ticker ticker *time.Ticker
close chan struct{} close chan struct{}
@ -267,7 +267,7 @@ func NewURLTestGroup(
pauseManager: service.FromContext[pause.Manager](ctx), pauseManager: service.FromContext[pause.Manager](ctx),
interruptGroup: interrupt.NewGroup(), interruptGroup: interrupt.NewGroup(),
interruptExternalConnections: interruptExternalConnections, interruptExternalConnections: interruptExternalConnections,
randomize: randomize, randomize: randomize,
}, nil }, nil
} }
@ -348,29 +348,29 @@ func (g *URLTestGroup) Select(network string) (adapter.Outbound, bool) {
} }
func (g *URLTestGroup) loopCheck() { func (g *URLTestGroup) loopCheck() {
if time.Now().Sub(g.lastActive.Load()) > g.interval { if time.Now().Sub(g.lastActive.Load()) > g.interval {
g.lastActive.Store(time.Now()) g.lastActive.Store(time.Now())
g.CheckOutbounds(false) g.CheckOutbounds(false)
} }
for { for {
select { select {
case <-g.close: case <-g.close:
return return
case <-g.ticker.C: case <-g.ticker.C:
} }
if time.Now().Sub(g.lastActive.Load()) > g.idleTimeout { if time.Now().Sub(g.lastActive.Load()) > g.idleTimeout {
g.access.Lock() g.access.Lock()
g.ticker.Stop() g.ticker.Stop()
g.ticker = nil g.ticker = nil
g.access.Unlock() g.access.Unlock()
return return
} }
g.pauseManager.WaitActive() g.pauseManager.WaitActive()
g.CheckOutbounds(false) g.CheckOutbounds(false)
if g.randomize { if g.randomize {
g.selectBestLatencyOutbounds() g.selectBestLatencyOutbounds()
} }
} }
} }
func (g *URLTestGroup) CheckOutbounds(force bool) { func (g *URLTestGroup) CheckOutbounds(force bool) {
@ -379,14 +379,14 @@ func (g *URLTestGroup) CheckOutbounds(force bool) {
func (g *URLTestGroup) URLTest(ctx context.Context) (map[string]uint16, error) { func (g *URLTestGroup) URLTest(ctx context.Context) (map[string]uint16, error) {
result, err := g.urlTest(ctx, false) result, err := g.urlTest(ctx, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if g.randomize { if g.randomize {
g.selectBestLatencyOutbounds() g.selectBestLatencyOutbounds()
} }
return result, nil return result, nil
} }
func (g *URLTestGroup) urlTest(ctx context.Context, force bool) (map[string]uint16, error) { func (g *URLTestGroup) urlTest(ctx context.Context, force bool) (map[string]uint16, error) {
@ -454,69 +454,67 @@ func (g *URLTestGroup) performUpdateCheck() {
} }
func (g *URLTestGroup) selectBestLatencyOutbounds() { func (g *URLTestGroup) selectBestLatencyOutbounds() {
var bestTCPLatency uint16 var bestTCPLatency uint16
var bestUDPLatency uint16 var bestUDPLatency uint16
var bestTCPOutbounds []adapter.Outbound var bestTCPOutbounds []adapter.Outbound
var bestUDPOutbounds []adapter.Outbound var bestUDPOutbounds []adapter.Outbound
for _, detour := range g.outbounds { for _, detour := range g.outbounds {
history := g.history.LoadURLTestHistory(RealTag(detour)) history := g.history.LoadURLTestHistory(RealTag(detour))
if history == nil { if history == nil {
continue continue
} }
if common.Contains(detour.Network(), N.NetworkTCP) { if common.Contains(detour.Network(), N.NetworkTCP) {
if bestTCPLatency == 0 || history.Delay < bestTCPLatency { if bestTCPLatency == 0 || history.Delay < bestTCPLatency {
bestTCPLatency = history.Delay bestTCPLatency = history.Delay
} }
} }
if common.Contains(detour.Network(), N.NetworkUDP) { if common.Contains(detour.Network(), N.NetworkUDP) {
if bestUDPLatency == 0 || history.Delay < bestUDPLatency { if bestUDPLatency == 0 || history.Delay < bestUDPLatency {
bestUDPLatency = history.Delay bestUDPLatency = history.Delay
} }
} }
} }
for _, detour := range g.outbounds { for _, detour := range g.outbounds {
history := g.history.LoadURLTestHistory(RealTag(detour)) history := g.history.LoadURLTestHistory(RealTag(detour))
if history == nil { if history == nil {
continue continue
} }
if common.Contains(detour.Network(), N.NetworkTCP) && history.Delay <= bestTCPLatency+g.tolerance { if common.Contains(detour.Network(), N.NetworkTCP) && history.Delay <= bestTCPLatency+g.tolerance {
bestTCPOutbounds = append(bestTCPOutbounds, detour) bestTCPOutbounds = append(bestTCPOutbounds, detour)
} }
if common.Contains(detour.Network(), N.NetworkUDP) && history.Delay <= bestUDPLatency+g.tolerance { if common.Contains(detour.Network(), N.NetworkUDP) && history.Delay <= bestUDPLatency+g.tolerance {
bestUDPOutbounds = append(bestUDPOutbounds, detour) bestUDPOutbounds = append(bestUDPOutbounds, detour)
} }
} }
g.bestTCPLatencyOutbounds = bestTCPOutbounds g.bestTCPLatencyOutbounds = bestTCPOutbounds
g.bestUDPLatencyOutbounds = bestUDPOutbounds g.bestUDPLatencyOutbounds = bestUDPOutbounds
} }
// selectRandomOutbound selects an outbound randomly among the outbounds with the best latency // selectRandomOutbound selects an outbound randomly among the outbounds with the best latency
func (g *URLTestGroup) selectRandomOutbound(network string) adapter.Outbound { func (g *URLTestGroup) selectRandomOutbound(network string) adapter.Outbound {
var bestOutbounds []adapter.Outbound var bestOutbounds []adapter.Outbound
switch network { switch network {
case N.NetworkTCP: case N.NetworkTCP:
bestOutbounds = g.bestTCPLatencyOutbounds bestOutbounds = g.bestTCPLatencyOutbounds
case N.NetworkUDP: case N.NetworkUDP:
bestOutbounds = g.bestUDPLatencyOutbounds bestOutbounds = g.bestUDPLatencyOutbounds
default: default:
return nil return nil
} }
if len(bestOutbounds) == 0 { if len(bestOutbounds) == 0 {
return nil return nil
} }
randIndex := rand.Intn(len(bestOutbounds)) randIndex := rand.Intn(len(bestOutbounds))
g.logger.Debug("Random outbound selection: ", bestOutbounds[randIndex].Tag()) g.logger.Debug("Random outbound selection: ", bestOutbounds[randIndex].Tag())
return bestOutbounds[randIndex] return bestOutbounds[randIndex]
} }