Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/net/dnsclient_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ func goLookupIPFiles(name string) (addrs []IPAddr, canonical string) {
addrs = append(addrs, addr)
}
}
sortByRFC6724(addrs)
return addrs, canonical
}

Expand All @@ -607,6 +606,12 @@ func (r *Resolver) goLookupIP(ctx context.Context, network, host string, order h
}

func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, network, name string, order hostLookupOrder, conf *dnsConfig) (addrs []IPAddr, cname dnsmessage.Name, err error) {
addrs, cname, err = r.goLookupIPCNAME(ctx, network, name, order, conf)
sortByRFC6724(addrs)
return addrs, cname, err
}

func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, order hostLookupOrder, conf *dnsConfig) (addrs []IPAddr, cname dnsmessage.Name, err error) {
if order == hostLookupFilesDNS || order == hostLookupFiles {
var canonical string
addrs, canonical = goLookupIPFiles(name)
Expand Down Expand Up @@ -796,7 +801,6 @@ func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, network, name strin
// just one is misleading. See also golang.org/issue/6324.
lastErr.Name = name
}
sortByRFC6724(addrs)
if len(addrs) == 0 && !(network == "CNAME" && cname.Length > 0) {
if order == hostLookupDNSFiles {
var canonical string
Expand Down
14 changes: 13 additions & 1 deletion src/net/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"internal/nettrace"
"internal/singleflight"
"internal/stringslite"
"math/rand/v2"
"net/netip"
"sync"

Expand Down Expand Up @@ -373,7 +374,18 @@ func (r *Resolver) lookupIPAddr(ctx context.Context, network, host string) ([]IP
addrs, _ := r.Val.([]IPAddr)
trace.DNSDone(ipAddrsEface(addrs), r.Shared, err)
}
return lookupIPReturn(r.Val, err, r.Shared)
// shuffle resolved addrs before sort by RFC6724 for connection load-balancing
// See https://github.com/golang/go/issues/34511
// See https://github.com/grafana/loki/issues/3301
// See https://github.com/grafana/loki/issues/9239
// See https://github.com/restic/restic/issues/4444
// See https://github.com/golang/go/issues/31698
addrs, err := lookupIPReturn(r.Val, err, r.Shared)
rand.Shuffle(len(addrs), func(i, j int) {
addrs[i], addrs[j] = addrs[j], addrs[i]
})
sortByRFC6724(addrs)
return addrs, err
}
}

Expand Down
21 changes: 15 additions & 6 deletions src/net/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func TestDNSFlood(t *testing.T) {

defer dnsWaitGroup.Wait()

var N = 5000
N := 5000
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
// On Darwin this test consumes kernel threads much
// than other platforms for some reason.
Expand Down Expand Up @@ -757,7 +757,7 @@ func TestLookupPort(t *testing.T) {
port int
ok bool
}
var tests = []test{
tests := []test{
{"tcp", "0", 0, true},
{"udp", "0", 0, true},
{"udp", "domain", 53, true},
Expand Down Expand Up @@ -809,7 +809,7 @@ func TestLookupPort_Minimal(t *testing.T) {
name string
port int
}
var tests = []test{
tests := []test{
{"tcp", "http", 80},
{"tcp", "HTTP", 80}, // case shouldn't matter
{"tcp", "https", 443},
Expand All @@ -832,7 +832,7 @@ func TestLookupProtocol_Minimal(t *testing.T) {
name string
want int
}
var tests = []test{
tests := []test{
{"tcp", 6},
{"TcP", 6}, // case shouldn't matter
{"icmp", 1},
Expand All @@ -847,7 +847,6 @@ func TestLookupProtocol_Minimal(t *testing.T) {
t.Errorf("LookupProtocol(%q) = %d, %v; want %d, error=nil", tt.name, got, err, tt.want)
}
}

}

func TestLookupNonLDH(t *testing.T) {
Expand Down Expand Up @@ -1192,14 +1191,24 @@ func TestLookupIPAddrConcurrentCallsForNetworks(t *testing.T) {
t.Errorf("lookupIPAddr(%v, %v): unexpected error: %v", network, host, err)
}
wantIPs := results[[2]string{network, host}]
if !reflect.DeepEqual(gotIPs, wantIPs) {
// ingore order
if !reflect.DeepEqual(sortedIPAddrStrings(gotIPs), sortedIPAddrStrings(wantIPs)) {
t.Errorf("lookupIPAddr(%v, %v): mismatched IPAddr results\n\tGot: %v\n\tWant: %v", network, host, gotIPs, wantIPs)
}
}()
}
wg.Wait()
}

func sortedIPAddrStrings(ipAddrs []IPAddr) []string {
ret := make([]string, len(ipAddrs))
for i, ipAddr := range ipAddrs {
ret[i] = ipAddr.String() + "\000" + ipAddr.Zone
}
slices.Sort(ret)
return ret
}

// Issue 53995: Resolver.LookupIP should return error for empty host name.
func TestResolverLookupIPWithEmptyHost(t *testing.T) {
_, err := DefaultResolver.LookupIP(context.Background(), "ip", "")
Expand Down
2 changes: 1 addition & 1 deletion src/net/lookup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []
if order == hostLookupCgo {
return cgoLookupIP(ctx, network, host)
}
ips, _, err := r.goLookupIPCNAMEOrder(ctx, network, host, order, conf)
ips, _, err := r.goLookupIPCNAME(ctx, network, host, order, conf)
return ips, err
}

Expand Down