From 9766f0e11032312171b2929eba2fa654b6f505e8 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 25 Jun 2024 12:21:32 -0700 Subject: [PATCH] net/dns: move mutex before the field it guards And some misc doc tweaks for idiomatic Go style. Updates #cleanup Change-Id: I3ca45f78aaca037f433538b847fd6a9571a2d918 Signed-off-by: Brad Fitzpatrick --- net/dns/manager.go | 12 ++++++------ net/dns/resolver/forwarder.go | 4 +++- net/dns/resolver/tsdns.go | 8 +++++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/net/dns/manager.go b/net/dns/manager.go index 88fe94fac..eee2d5a7d 100644 --- a/net/dns/manager.go +++ b/net/dns/manager.go @@ -59,11 +59,10 @@ type Manager struct { knobs *controlknobs.Knobs // or nil goos string // if empty, gets set to runtime.GOOS - // The last configuration we successfully compiled. Set to nil if - // there was any failure applying the last configuration + mu sync.Mutex // guards following + // config is the last configuration we successfully compiled or nil if there + // was any failure applying the last configuration. config *Config - // Must be held when accessing/setting config. - mu sync.Mutex } // NewManagers created a new manager from the given config. @@ -123,8 +122,9 @@ func (m *Manager) Set(cfg Config) error { return m.setLocked(cfg) } -// Sets the DNS configuration. -// m.mu must be held +// setLocked sets the DNS configuration. +// +// m.mu must be held. func (m *Manager) setLocked(cfg Config) error { syncs.AssertLocked(&m.mu) diff --git a/net/dns/resolver/forwarder.go b/net/dns/resolver/forwarder.go index 8676aebc7..c1c2a05d6 100644 --- a/net/dns/resolver/forwarder.go +++ b/net/dns/resolver/forwarder.go @@ -212,7 +212,9 @@ type forwarder struct { // resolver lookup. cloudHostFallback []resolverAndDelay - // To be called when a SERVFAIL is returned due to missing upstream resolvers. + // missingUpstreamRecovery, if non-nil, is set called when a SERVFAIL is + // returned due to missing upstream resolvers. + // // This should attempt to properly (re)set the upstream resolvers. missingUpstreamRecovery func() } diff --git a/net/dns/resolver/tsdns.go b/net/dns/resolver/tsdns.go index a140c7e93..a3f3d7010 100644 --- a/net/dns/resolver/tsdns.go +++ b/net/dns/resolver/tsdns.go @@ -244,9 +244,11 @@ func New(logf logger.Logf, linkSel ForwardLinkSelector, dialer *tsdial.Dialer, k return r } -// Called by the forwarder on SERVFAIL due to missing upstream resolvers -// The func passed in here should attempt to re-query for those resolvers, -// repair, or recover +// SetMissingUpstreamRecovery sets a callback to be called upon encountering +// a SERVFAIL due to missing upstream resolvers. +// +// This call should only happen before the resolver is used. It is not safe +// for concurrent use. func (r *Resolver) SetMissingUpstreamRecovery(f func()) { r.forwarder.missingUpstreamRecovery = f }