From f2222b5e025066db031de62d76cbc061423f20a6 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 5 Feb 2026 16:20:10 +0800 Subject: [PATCH] chore: code cleanup --- component/dialer/dialer.go | 75 +++++++++++++------------------------- 1 file changed, 26 insertions(+), 49 deletions(-) diff --git a/component/dialer/dialer.go b/component/dialer/dialer.go index 2cd73f42..9c9fd43b 100644 --- a/component/dialer/dialer.go +++ b/component/dialer/dialer.go @@ -21,15 +21,22 @@ import ( const ( DefaultTCPTimeout = 5 * time.Second DefaultUDPTimeout = DefaultTCPTimeout -) -type dialFunc func(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) + dualStackFallbackTimeout = 300 * time.Millisecond +) var ( - tcpConcurrent = atomic.NewBool(false) - fallbackTimeout = 300 * time.Millisecond + tcpConcurrent = atomic.NewBool(false) ) +func SetTcpConcurrent(concurrent bool) { + tcpConcurrent.Store(concurrent) +} + +func GetTcpConcurrent() bool { + return tcpConcurrent.Load() +} + func DialContext(ctx context.Context, network, address string, options ...Option) (net.Conn, error) { opt := applyOptions(options...) @@ -48,11 +55,22 @@ func DialContext(ctx context.Context, network, address string, options ...Option return nil, err } + tcpConcurrent := GetTcpConcurrent() + switch network { case "tcp4", "tcp6", "udp4", "udp6": - return actualSingleStackDialContext(ctx, network, ips, port, opt) + if tcpConcurrent { + return parallelDialContext(ctx, network, ips, port, opt) + } + return serialDialContext(ctx, network, ips, port, opt) case "tcp", "udp": - return actualDualStackDialContext(ctx, network, ips, port, opt) + if tcpConcurrent { + if opt.prefer != 4 && opt.prefer != 6 { + return parallelDialContext(ctx, network, ips, port, opt) + } + return dualStackDialContext(ctx, parallelDialContext, network, ips, port, opt) + } + return dualStackDialContext(ctx, serialDialContext, network, ips, port, opt) default: return nil, ErrorInvalidedNetworkStack } @@ -102,14 +120,6 @@ func ListenPacket(ctx context.Context, network, address string, rAddrPort netip. return lc.ListenPacket(ctx, network, address) } -func SetTcpConcurrent(concurrent bool) { - tcpConcurrent.Store(concurrent) -} - -func GetTcpConcurrent() bool { - return tcpConcurrent.Load() -} - func dialContext(ctx context.Context, network string, destination netip.Addr, port string, opt option) (net.Conn, error) { var address string destination, port = resolver.LookupIP4P(destination, port) @@ -192,40 +202,7 @@ func ICMPControl(destination netip.Addr) func(network, address string, conn sysc } } -func actualSingleStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) { - if tcpConcurrent.Load() { - return concurrentSingleStackDialContext(ctx, network, ips, port, opt) - } else { - return serialSingleStackDialContext(ctx, network, ips, port, opt) - } -} - -func actualDualStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) { - if tcpConcurrent.Load() { - return concurrentDualStackDialContext(ctx, network, ips, port, opt) - } else { - return serialDualStackDialContext(ctx, network, ips, port, opt) - } -} - -func serialSingleStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) { - return serialDialContext(ctx, network, ips, port, opt) -} - -func serialDualStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) { - return dualStackDialContext(ctx, serialDialContext, network, ips, port, opt) -} - -func concurrentSingleStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) { - return parallelDialContext(ctx, network, ips, port, opt) -} - -func concurrentDualStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) { - if opt.prefer != 4 && opt.prefer != 6 { - return parallelDialContext(ctx, network, ips, port, opt) - } - return dualStackDialContext(ctx, parallelDialContext, network, ips, port, opt) -} +type dialFunc func(ctx context.Context, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) func dualStackDialContext(ctx context.Context, dialFn dialFunc, network string, ips []netip.Addr, port string, opt option) (net.Conn, error) { ipv4s, ipv6s := resolver.SortationAddr(ips) @@ -234,7 +211,7 @@ func dualStackDialContext(ctx context.Context, dialFn dialFunc, network string, } preferIPVersion := opt.prefer - fallbackTicker := time.NewTicker(fallbackTimeout) + fallbackTicker := time.NewTicker(dualStackFallbackTimeout) defer fallbackTicker.Stop() results := make(chan dialResult)