Skip to content

Commit

Permalink
better optimize url test
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Jul 31, 2024
1 parent cc4e2cf commit 557dc59
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions outbound/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

const TimeoutDelay = 65535
const MinFailureToReset = 10
const MinFailureToReset = 15

var (
_ adapter.Outbound = (*URLTest)(nil)
Expand Down Expand Up @@ -145,7 +145,8 @@ func (s *URLTest) DialContext(ctx context.Context, network string, destination M
}
conn, err := outbound.DialContext(ctx, network, destination)
if err == nil {
s.group.tcpConnectionFailureCount.Decrement(false)
// s.group.tcpConnectionFailureCount.Decrement(false)
s.group.tcpConnectionFailureCount.Reset()
return s.group.interruptGroup.NewConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
}

Expand All @@ -171,7 +172,8 @@ func (s *URLTest) ListenPacket(ctx context.Context, destination M.Socksaddr) (ne
}
conn, err := outbound.ListenPacket(ctx, destination)
if err == nil {
s.group.udpConnectionFailureCount.Decrement(false)
// s.group.udpConnectionFailureCount.Decrement(false)
s.group.udpConnectionFailureCount.Reset()
return s.group.interruptGroup.NewPacketConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
}
if !s.group.pauseManager.IsNetworkPaused() && s.group.udpConnectionFailureCount.IncrementConditionReset(MinFailureToReset) {
Expand Down Expand Up @@ -306,19 +308,19 @@ func (g *URLTestGroup) Close() error {
}

func (g *URLTestGroup) Select(network string) (adapter.Outbound, bool) {
var minDelay uint16
var minDelay uint16 = TimeoutDelay
var minOutbound adapter.Outbound
switch network {
case N.NetworkTCP:
if g.selectedOutboundTCP != nil {
if history := g.history.LoadURLTestHistory(RealTag(g.selectedOutboundTCP)); history != nil {
if history := g.history.LoadURLTestHistory(RealTag(g.selectedOutboundTCP)); history != nil && history.Delay != TimeoutDelay {
minOutbound = g.selectedOutboundTCP
minDelay = history.Delay
}
}
case N.NetworkUDP:
if g.selectedOutboundUDP != nil {
if history := g.history.LoadURLTestHistory(RealTag(g.selectedOutboundUDP)); history != nil {
if history := g.history.LoadURLTestHistory(RealTag(g.selectedOutboundUDP)); history != nil && history.Delay != TimeoutDelay {
minOutbound = g.selectedOutboundUDP
minDelay = history.Delay
}
Expand All @@ -332,7 +334,7 @@ func (g *URLTestGroup) Select(network string) (adapter.Outbound, bool) {
if history == nil || history.Delay == TimeoutDelay {
continue
}
if minDelay == 0 || minDelay > history.Delay+g.tolerance {
if minDelay == 0 || minDelay == TimeoutDelay || minDelay > history.Delay+g.tolerance {
minDelay = history.Delay
minOutbound = detour
}
Expand Down Expand Up @@ -405,9 +407,9 @@ func (g *URLTestGroup) urlTest(ctx context.Context, force bool) (map[string]uint
continue
}
b.Go(realTag, func() (any, error) {
ctx, cancel := context.WithTimeout(context.Background(), C.TCPTimeout)
testCtx, cancel := context.WithTimeout(g.ctx, C.TCPTimeout)
defer cancel()
t, err := urltest.URLTest(ctx, g.link, p)
t, err := urltest.URLTest(testCtx, g.link, p)
if err != nil {
g.logger.Debug("outbound ", tag, " unavailable (", TimeoutDelay, "ms): ", err)
// g.history.DeleteURLTestHistory(realTag)
Expand Down

0 comments on commit 557dc59

Please sign in to comment.