From b7662047aedc5f2c512911eb59d514ce75b16e18 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 16 Nov 2022 14:19:47 -0800 Subject: net: change resolverConfig.dnsConfig to an atomic.Pointer We were using a RWMutex RLock around a single memory load, which is not a good use of a RWMutex--it introduces extra work for the RLock but contention around a single memory load is unlikely. And, the tryUpdate method was not acquiring the mutex anyhow. The new atomic.Pointer type is type-safe and easy to use correctly for a simple use-case like this. Change-Id: Ib3859c03414c44d2e897f6d15c92c8e4b5c81a11 Reviewed-on: https://go-review.googlesource.com/c/go/+/451416 TryBot-Result: Gopher Robot Reviewed-by: Damien Neil Auto-Submit: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor --- src/net/dnsclient_unix_test.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'src/net/dnsclient_unix_test.go') diff --git a/src/net/dnsclient_unix_test.go b/src/net/dnsclient_unix_test.go index c2a85db6de..2a15845ea1 100644 --- a/src/net/dnsclient_unix_test.go +++ b/src/net/dnsclient_unix_test.go @@ -280,9 +280,7 @@ func (conf *resolvConfTest) forceUpdate(name string, lastChecked time.Time) erro } func (conf *resolvConfTest) forceUpdateConf(c *dnsConfig, lastChecked time.Time) bool { - conf.mu.Lock() - conf.dnsConfig = c - conf.mu.Unlock() + conf.dnsConfig.Store(c) for i := 0; i < 5; i++ { if conf.tryAcquireSema() { conf.lastChecked = lastChecked @@ -294,10 +292,7 @@ func (conf *resolvConfTest) forceUpdateConf(c *dnsConfig, lastChecked time.Time) } func (conf *resolvConfTest) servers() []string { - conf.mu.RLock() - servers := conf.dnsConfig.servers - conf.mu.RUnlock() - return servers + return conf.dnsConfig.Load().servers } func (conf *resolvConfTest) teardown() error { @@ -1445,9 +1440,7 @@ func TestDNSGoroutineRace(t *testing.T) { func lookupWithFake(fake fakeDNSServer, name string, typ dnsmessage.Type) error { r := Resolver{PreferGo: true, Dial: fake.DialContext} - resolvConf.mu.RLock() - conf := resolvConf.dnsConfig - resolvConf.mu.RUnlock() + conf := resolvConf.dnsConfig.Load() ctx, cancel := context.WithCancel(context.Background()) defer cancel() -- cgit v1.2.1