net: use Go's DNS resolver when system configuration permits

If the machine's network configuration files (resolv.conf,
nsswitch.conf) don't have any unsupported options, prefer Go's DNS
resolver, which doesn't have the cgo & thread over.

It means users can have more than 500 DNS requests outstanding (our
current limit for cgo lookups) and not have one blocked thread per
outstanding request.

Discussed in thread https://groups.google.com/d/msg/golang-dev/2ZUi792oztM/Q0rg_DkF5HMJ

Change-Id: I3f685d70aff6b47bec30b63e9fba674b20507f95
Reviewed-on: https://go-review.googlesource.com/8945
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-04-16 14:33:25 -07:00
parent c231228085
commit 4a0ba7aa17
10 changed files with 1079 additions and 53 deletions

View file

@ -13,22 +13,23 @@ import (
var dnsReadConfigTests = []struct {
name string
conf dnsConfig
want *dnsConfig
}{
{
name: "testdata/resolv.conf",
conf: dnsConfig{
servers: []string{"8.8.8.8", "2001:4860:4860::8888", "fe80::1%lo0"},
search: []string{"localdomain"},
ndots: 5,
timeout: 10,
attempts: 3,
rotate: true,
want: &dnsConfig{
servers: []string{"8.8.8.8", "2001:4860:4860::8888", "fe80::1%lo0"},
search: []string{"localdomain"},
ndots: 5,
timeout: 10,
attempts: 3,
rotate: true,
unknownOpt: true, // the "options attempts 3" line
},
},
{
name: "testdata/domain-resolv.conf",
conf: dnsConfig{
want: &dnsConfig{
servers: []string{"8.8.8.8"},
search: []string{"localdomain"},
ndots: 1,
@ -38,7 +39,7 @@ var dnsReadConfigTests = []struct {
},
{
name: "testdata/search-resolv.conf",
conf: dnsConfig{
want: &dnsConfig{
servers: []string{"8.8.8.8"},
search: []string{"test", "invalid"},
ndots: 1,
@ -48,12 +49,23 @@ var dnsReadConfigTests = []struct {
},
{
name: "testdata/empty-resolv.conf",
conf: dnsConfig{
want: &dnsConfig{
ndots: 1,
timeout: 5,
attempts: 2,
},
},
{
name: "testdata/openbsd-resolv.conf",
want: &dnsConfig{
ndots: 1,
timeout: 5,
attempts: 2,
lookup: []string{"file", "bind"},
servers: []string{"169.254.169.254", "10.240.0.1"},
search: []string{"c.symbolic-datum-552.internal."},
},
},
}
func TestDNSReadConfig(t *testing.T) {
@ -62,8 +74,8 @@ func TestDNSReadConfig(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(conf, &tt.conf) {
t.Errorf("got %v; want %v", conf, &tt.conf)
if !reflect.DeepEqual(conf, tt.want) {
t.Errorf("%s:\n got: %+v\nwant: %+v", tt.name, conf, tt.want)
}
}
}