-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix cache race #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ import ( | |
) | ||
|
||
type Resolver struct { | ||
sync.RWMutex | ||
mx sync.Mutex | ||
url string | ||
|
||
// RR cache | ||
|
@@ -101,8 +101,8 @@ func (r *Resolver) LookupTXT(ctx context.Context, domain string) ([]string, erro | |
} | ||
|
||
func (r *Resolver) getCachedIPAddr(domain string) ([]net.IPAddr, bool) { | ||
r.RLock() | ||
defer r.RUnlock() | ||
r.mx.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switching away from RW mutexes may slow things down here. Given that we're caching against what would be a network request waiting on a mutex doesn't feel too bad, and if this becomes an issue we can always come back and optimize it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add a follow up issue? It shouldn't be hard to do, just queue the entries for cleanup and do that with write lock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
defer r.mx.Unlock() | ||
|
||
fqdn := dns.Fqdn(domain) | ||
entry, ok := r.ipCache[fqdn] | ||
|
@@ -123,16 +123,16 @@ func (r *Resolver) cacheIPAddr(domain string, ips []net.IPAddr, ttl uint32) { | |
return | ||
} | ||
|
||
r.Lock() | ||
defer r.Unlock() | ||
r.mx.Lock() | ||
defer r.mx.Unlock() | ||
|
||
fqdn := dns.Fqdn(domain) | ||
r.ipCache[fqdn] = ipAddrEntry{ips, time.Now().Add(time.Duration(ttl) * time.Second)} | ||
} | ||
|
||
func (r *Resolver) getCachedTXT(domain string) ([]string, bool) { | ||
r.RLock() | ||
defer r.RUnlock() | ||
r.mx.Lock() | ||
defer r.mx.Unlock() | ||
|
||
fqdn := dns.Fqdn(domain) | ||
entry, ok := r.txtCache[fqdn] | ||
|
@@ -153,8 +153,8 @@ func (r *Resolver) cacheTXT(domain string, txt []string, ttl uint32) { | |
return | ||
} | ||
|
||
r.Lock() | ||
defer r.Unlock() | ||
r.mx.Lock() | ||
defer r.mx.Unlock() | ||
|
||
fqdn := dns.Fqdn(domain) | ||
r.txtCache[fqdn] = txtEntry{txt, time.Now().Add(time.Duration(ttl) * time.Second)} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Figured exporting the Lock/Unlock functions here was unintentional and a bad idea for a consumer to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes, it was just programmatic convenience!