net/dnscache: initialize the single Resolver more directly

This commit is contained in:
Brad Fitzpatrick 2020-03-09 21:04:08 -07:00
parent d07146aafb
commit d44325295e
1 changed files with 9 additions and 7 deletions

View File

@ -17,20 +17,22 @@ import (
"golang.org/x/sync/singleflight"
)
var single = func() *Resolver {
r := &Resolver{
Forward: &net.Resolver{PreferGo: true},
}
var single = &Resolver{
Forward: &net.Resolver{PreferGo: preferGoResolver()},
}
func preferGoResolver() bool {
// There does not appear to be a local resolver running
// on iOS, and NetworkExtension is good at isolating DNS.
// So do not use the Go resolver on macOS/iOS.
if runtime.GOOS == "darwin" {
r.Forward.PreferGo = false
return false
}
return r
}()
// Otherwise, the Go resolver is fine and slightly preferred
// since it's lighter, not using cgo calls & threads.
return true
}
// Get returns a caching Resolver singleton.
func Get() *Resolver { return single }