fix: dial panic

This commit is contained in:
Skyxim 2023-03-10 22:08:01 +08:00
parent 913ed62095
commit 035d878a9f

View file

@ -182,13 +182,14 @@ func dualStackDialContext(ctx context.Context, dialFn dialFunc, network string,
go racer(ipv6s, preferIPVersion != 4) go racer(ipv6s, preferIPVersion != 4)
var fallback dialResult var fallback dialResult
var errs []error var errs []error
for i := 0; i < 2; i++ { for i := 0; i < 2; {
select { select {
case <-fallbackTicker.C: case <-fallbackTicker.C:
if fallback.error == nil && fallback.Conn != nil { if fallback.error == nil && fallback.Conn != nil {
return fallback.Conn, nil return fallback.Conn, nil
} }
case res := <-results: case res := <-results:
i++
if res.error == nil { if res.error == nil {
if res.isPrimary { if res.isPrimary {
return res.Conn, nil return res.Conn, nil
@ -217,7 +218,7 @@ func parallelDialContext(ctx context.Context, network string, ips []netip.Addr,
returned := make(chan struct{}) returned := make(chan struct{})
defer close(returned) defer close(returned)
racer := func(ctx context.Context, ip netip.Addr) { racer := func(ctx context.Context, ip netip.Addr) {
result := dialResult{isPrimary: true} result := dialResult{isPrimary: true, ip: ip}
defer func() { defer func() {
select { select {
case results <- result: case results <- result:
@ -227,7 +228,6 @@ func parallelDialContext(ctx context.Context, network string, ips []netip.Addr,
} }
} }
}() }()
result.ip = ip
result.Conn, result.error = dialContext(ctx, network, ip, port, opt) result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
} }
@ -235,23 +235,18 @@ func parallelDialContext(ctx context.Context, network string, ips []netip.Addr,
go racer(ctx, ip) go racer(ctx, ip)
} }
var errs []error var errs []error
for { for i := 0; i < len(ips); i++ {
select { res := <-results
case <-ctx.Done():
if len(errs) > 0 {
return nil, errorsJoin(errs...)
}
if ctx.Err() == context.DeadlineExceeded {
return nil, os.ErrDeadlineExceeded
}
return nil, ctx.Err()
case res := <-results:
if res.error == nil { if res.error == nil {
return res.Conn, nil return res.Conn, nil
} }
errs = append(errs, res.error) errs = append(errs, res.error)
} }
if len(errs) > 0 {
return nil, errorsJoin(errs...)
} }
return nil, os.ErrDeadlineExceeded
} }
func serialDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) { func serialDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {