From a83482cac43ae349bb732ae2710f864ce4102dbe Mon Sep 17 00:00:00 2001 From: djx30103 <75022199+djx30103@users.noreply.github.com> Date: Thu, 3 Oct 2024 11:20:57 +0800 Subject: [PATCH] perf: reducing the lock strength of the soa cache entry (#2285) Co-authored-by: icpd <35096485+icpd@users.noreply.github.com> Co-authored-by: Fernandez Ludovic --- challenge/dns01/nameserver.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/challenge/dns01/nameserver.go b/challenge/dns01/nameserver.go index 206611be..a2149b3f 100644 --- a/challenge/dns01/nameserver.go +++ b/challenge/dns01/nameserver.go @@ -16,10 +16,7 @@ import ( const defaultResolvConf = "/etc/resolv.conf" -var ( - fqdnSoaCache = map[string]*soaCacheEntry{} - muFqdnSoaCache sync.Mutex -) +var fqdnSoaCache = &sync.Map{} var defaultNameservers = []string{ "google-public-dns-a.google.com:53", @@ -51,9 +48,7 @@ func (cache *soaCacheEntry) isExpired() bool { // ClearFqdnCache clears the cache of fqdn to zone mappings. Primarily used in testing. func ClearFqdnCache() { - muFqdnSoaCache.Lock() - fqdnSoaCache = map[string]*soaCacheEntry{} - muFqdnSoaCache.Unlock() + fqdnSoaCache.Clear() } func AddDNSTimeout(timeout time.Duration) ChallengeOption { @@ -153,12 +148,13 @@ func FindZoneByFqdnCustom(fqdn string, nameservers []string) (string, error) { } func lookupSoaByFqdn(fqdn string, nameservers []string) (*soaCacheEntry, error) { - muFqdnSoaCache.Lock() - defer muFqdnSoaCache.Unlock() - // Do we have it cached and is it still fresh? - if ent := fqdnSoaCache[fqdn]; ent != nil && !ent.isExpired() { - return ent, nil + entAny, ok := fqdnSoaCache.Load(fqdn) + if ok && entAny != nil { + ent, ok1 := entAny.(*soaCacheEntry) + if ok1 && !ent.isExpired() { + return ent, nil + } } ent, err := fetchSoaByFqdn(fqdn, nameservers) @@ -166,7 +162,8 @@ func lookupSoaByFqdn(fqdn string, nameservers []string) (*soaCacheEntry, error) return nil, err } - fqdnSoaCache[fqdn] = ent + fqdnSoaCache.Store(fqdn, ent) + return ent, nil }