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

View File

@ -17,20 +17,22 @@
"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 }