Skip to content

Commit 1931a3f

Browse files
committed
net: ensure ipv6 endpoints are tried first
When connecting to an endpoint that resolves to ipv4 and ipv6 addresses, ensure that ipv6 addresses are tried first, as per happy eyeballs RFC. Currently whether ipv4 or ipv6 addresses are tried first depends on the dns resolution speed. If the A resolution returned first, it would be first in the address list, and partition() uses the label of the first result to build the primaries category. Fixes #54928
1 parent ed530db commit 1931a3f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/net/dial.go

+7
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn
421421
var primaries, fallbacks addrList
422422
if d.dualStack() && network == "tcp" {
423423
primaries, fallbacks = addrs.partition(isIPv4)
424+
425+
// when fallbacks is not empty, ensure primaries
426+
// are ipv6 addresses as per happy eyeballs spec
427+
if len(fallbacks) > 0 && !isIPv4(fallbacks[0]) {
428+
primaries, fallbacks = fallbacks, primaries
429+
}
430+
424431
} else {
425432
primaries = addrs
426433
}

0 commit comments

Comments
 (0)