fix: no main result conn, will fail

This commit is contained in:
Skyxim 2022-08-28 20:26:13 +08:00
parent db94dc76b4
commit af97922e94

View file

@ -218,7 +218,6 @@ func concurrentDualStackDialContext(ctx context.Context, network, address string
} }
var ips []netip.Addr var ips []netip.Addr
if opt.direct { if opt.direct {
ips, err = resolver.ResolveAllIP(host) ips, err = resolver.ResolveAllIP(host)
} else { } else {
@ -298,24 +297,30 @@ func concurrentDialContext(ctx context.Context, network string, ips []netip.Addr
if res.isPrimary { if res.isPrimary {
return res.Conn, nil return res.Conn, nil
} else { } else {
fallback = res if !fallback.done || fallback.error != nil {
fallback = res
}
} }
} else { } else {
if res.isPrimary { if res.isPrimary {
preferCount.Add(-1) preferCount.Add(-1)
if preferCount.Load() == 0 && fallback.done { if preferCount.Load() == 0 && fallback.done && fallback.error == nil {
return fallback.Conn, nil return fallback.Conn, nil
} }
} }
} }
case <-ctx.Done(): case <-ctx.Done():
if fallback.done { if fallback.done && fallback.error == nil {
return fallback.Conn, nil return fallback.Conn, nil
} }
break break
} }
} }
if fallback.done && fallback.error == nil {
return fallback.Conn, nil
}
return nil, fmt.Errorf("all ips %v tcp shake hands failed", ips) return nil, fmt.Errorf("all ips %v tcp shake hands failed", ips)
} }