Commit 4d9bda51 authored by Matthew Dempsky's avatar Matthew Dempsky

net: append ":53" to DNS servers when reading resolv.conf

Avoids generating some redundant garbage from re-concatenating the
same string for every DNS query.

benchmark                                      old allocs     new allocs     delta
BenchmarkGoLookupIP-32                         156            154            -1.28%
BenchmarkGoLookupIPNoSuchHost-32               456            446            -2.19%
BenchmarkGoLookupIPWithBrokenNameServer-32     577            564            -2.25%

benchmark                                      old bytes     new bytes     delta
BenchmarkGoLookupIP-32                         10873         10824         -0.45%
BenchmarkGoLookupIPNoSuchHost-32               43303         43140         -0.38%
BenchmarkGoLookupIPWithBrokenNameServer-32     46824         46616         -0.44%

Update #15473.

Change-Id: I3b0173dfedf31bd08eaea1069968b416850864a1
Reviewed-on: https://go-review.googlesource.com/22556Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 2cc27a7d
...@@ -190,7 +190,6 @@ func tryOneName(ctx context.Context, cfg *dnsConfig, name string, qtype uint16) ...@@ -190,7 +190,6 @@ func tryOneName(ctx context.Context, cfg *dnsConfig, name string, qtype uint16)
var lastErr error var lastErr error
for i := 0; i < cfg.attempts; i++ { for i := 0; i < cfg.attempts; i++ {
for _, server := range cfg.servers { for _, server := range cfg.servers {
server = JoinHostPort(server, "53")
msg, err := exchange(ctx, server, name, qtype) msg, err := exchange(ctx, server, name, qtype)
if err != nil { if err != nil {
lastErr = &DNSError{ lastErr = &DNSError{
......
...@@ -221,7 +221,7 @@ var updateResolvConfTests = []struct { ...@@ -221,7 +221,7 @@ var updateResolvConfTests = []struct {
{ {
name: "golang.org", name: "golang.org",
lines: []string{"nameserver 8.8.8.8"}, lines: []string{"nameserver 8.8.8.8"},
servers: []string{"8.8.8.8"}, servers: []string{"8.8.8.8:53"},
}, },
{ {
name: "", name: "",
...@@ -231,7 +231,7 @@ var updateResolvConfTests = []struct { ...@@ -231,7 +231,7 @@ var updateResolvConfTests = []struct {
{ {
name: "www.example.com", name: "www.example.com",
lines: []string{"nameserver 8.8.4.4"}, lines: []string{"nameserver 8.8.4.4"},
servers: []string{"8.8.4.4"}, servers: []string{"8.8.4.4:53"},
}, },
} }
......
...@@ -14,12 +14,12 @@ import ( ...@@ -14,12 +14,12 @@ import (
) )
var ( var (
defaultNS = []string{"127.0.0.1", "::1"} defaultNS = []string{"127.0.0.1:53", "[::1]:53"}
getHostname = os.Hostname // variable for testing getHostname = os.Hostname // variable for testing
) )
type dnsConfig struct { type dnsConfig struct {
servers []string // servers to use servers []string // server addresses (in host:port form) to use
search []string // suffixes to append to local name search []string // suffixes to append to local name
ndots int // number of dots in name to trigger absolute lookup ndots int // number of dots in name to trigger absolute lookup
timeout time.Duration // wait before giving up on a query, including retries timeout time.Duration // wait before giving up on a query, including retries
...@@ -70,9 +70,9 @@ func dnsReadConfig(filename string) *dnsConfig { ...@@ -70,9 +70,9 @@ func dnsReadConfig(filename string) *dnsConfig {
// just an IP address. Otherwise we need DNS // just an IP address. Otherwise we need DNS
// to look it up. // to look it up.
if parseIPv4(f[1]) != nil { if parseIPv4(f[1]) != nil {
conf.servers = append(conf.servers, f[1]) conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
} else if ip, _ := parseIPv6(f[1], true); ip != nil { } else if ip, _ := parseIPv6(f[1], true); ip != nil {
conf.servers = append(conf.servers, f[1]) conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
} }
} }
......
...@@ -21,7 +21,7 @@ var dnsReadConfigTests = []struct { ...@@ -21,7 +21,7 @@ var dnsReadConfigTests = []struct {
{ {
name: "testdata/resolv.conf", name: "testdata/resolv.conf",
want: &dnsConfig{ want: &dnsConfig{
servers: []string{"8.8.8.8", "2001:4860:4860::8888", "fe80::1%lo0"}, servers: []string{"8.8.8.8:53", "[2001:4860:4860::8888]:53", "[fe80::1%lo0]:53"},
search: []string{"localdomain"}, search: []string{"localdomain"},
ndots: 5, ndots: 5,
timeout: 10 * time.Second, timeout: 10 * time.Second,
...@@ -33,7 +33,7 @@ var dnsReadConfigTests = []struct { ...@@ -33,7 +33,7 @@ var dnsReadConfigTests = []struct {
{ {
name: "testdata/domain-resolv.conf", name: "testdata/domain-resolv.conf",
want: &dnsConfig{ want: &dnsConfig{
servers: []string{"8.8.8.8"}, servers: []string{"8.8.8.8:53"},
search: []string{"localdomain"}, search: []string{"localdomain"},
ndots: 1, ndots: 1,
timeout: 5 * time.Second, timeout: 5 * time.Second,
...@@ -43,7 +43,7 @@ var dnsReadConfigTests = []struct { ...@@ -43,7 +43,7 @@ var dnsReadConfigTests = []struct {
{ {
name: "testdata/search-resolv.conf", name: "testdata/search-resolv.conf",
want: &dnsConfig{ want: &dnsConfig{
servers: []string{"8.8.8.8"}, servers: []string{"8.8.8.8:53"},
search: []string{"test", "invalid"}, search: []string{"test", "invalid"},
ndots: 1, ndots: 1,
timeout: 5 * time.Second, timeout: 5 * time.Second,
...@@ -67,7 +67,7 @@ var dnsReadConfigTests = []struct { ...@@ -67,7 +67,7 @@ var dnsReadConfigTests = []struct {
timeout: 5 * time.Second, timeout: 5 * time.Second,
attempts: 2, attempts: 2,
lookup: []string{"file", "bind"}, lookup: []string{"file", "bind"},
servers: []string{"169.254.169.254", "10.240.0.1"}, servers: []string{"169.254.169.254:53", "10.240.0.1:53"},
search: []string{"c.symbolic-datum-552.internal."}, search: []string{"c.symbolic-datum-552.internal."},
}, },
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment