Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reducing the lock strength of the soa cache entry #2285

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions challenge/dns01/nameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear() was added in Go 1.23, please update go.mod or use API compatible with Go 1.22. Thank you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will not update to go1.23, but I will change this.

}

func AddDNSTimeout(timeout time.Duration) ChallengeOption {
Expand Down Expand Up @@ -153,20 +148,22 @@ 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)
if err != nil {
return nil, err
}

fqdnSoaCache[fqdn] = ent
fqdnSoaCache.Store(fqdn, ent)

return ent, nil
}

Expand Down