Add negative cache
Cache negative results, too, but only with half TTL, as configured. Plus get rid of "mesg.Id = req.Id" data race (build with -race flag) at once without locking. Found another data race: delete must be protected with Lock, RLock is not enough.
This commit is contained in:
parent
44b4cfa24e
commit
6bbefe3f18
6
cache.go
6
cache.go
|
@ -58,7 +58,7 @@ type MemoryCache struct {
|
||||||
Backend map[string]Mesg
|
Backend map[string]Mesg
|
||||||
Expire time.Duration
|
Expire time.Duration
|
||||||
Maxcount int
|
Maxcount int
|
||||||
mu *sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MemoryCache) Get(key string) (*dns.Msg, error) {
|
func (c *MemoryCache) Get(key string) (*dns.Msg, error) {
|
||||||
|
@ -92,9 +92,9 @@ func (c *MemoryCache) Set(key string, msg *dns.Msg) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MemoryCache) Remove(key string) {
|
func (c *MemoryCache) Remove(key string) {
|
||||||
c.mu.RLock()
|
c.mu.Lock()
|
||||||
delete(c.Backend, key)
|
delete(c.Backend, key)
|
||||||
c.mu.RUnlock()
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MemoryCache) Exists(key string) bool {
|
func (c *MemoryCache) Exists(key string) bool {
|
||||||
|
|
32
handler.go
32
handler.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
@ -25,7 +24,7 @@ func (q *Question) String() string {
|
||||||
|
|
||||||
type GODNSHandler struct {
|
type GODNSHandler struct {
|
||||||
resolver *Resolver
|
resolver *Resolver
|
||||||
cache Cache
|
cache, negCache Cache
|
||||||
hosts Hosts
|
hosts Hosts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +34,7 @@ func NewHandler() *GODNSHandler {
|
||||||
clientConfig *dns.ClientConfig
|
clientConfig *dns.ClientConfig
|
||||||
cacheConfig CacheSettings
|
cacheConfig CacheSettings
|
||||||
resolver *Resolver
|
resolver *Resolver
|
||||||
cache Cache
|
cache, negCache Cache
|
||||||
)
|
)
|
||||||
|
|
||||||
resolvConfig := settings.ResolvConfig
|
resolvConfig := settings.ResolvConfig
|
||||||
|
@ -52,10 +51,14 @@ func NewHandler() *GODNSHandler {
|
||||||
switch cacheConfig.Backend {
|
switch cacheConfig.Backend {
|
||||||
case "memory":
|
case "memory":
|
||||||
cache = &MemoryCache{
|
cache = &MemoryCache{
|
||||||
Backend: make(map[string]Mesg),
|
Backend: make(map[string]Mesg, cacheConfig.Maxcount),
|
||||||
Expire: time.Duration(cacheConfig.Expire) * time.Second,
|
Expire: time.Duration(cacheConfig.Expire) * time.Second,
|
||||||
Maxcount: cacheConfig.Maxcount,
|
Maxcount: cacheConfig.Maxcount,
|
||||||
mu: new(sync.RWMutex),
|
}
|
||||||
|
negCache = &MemoryCache{
|
||||||
|
Backend: make(map[string]Mesg),
|
||||||
|
Expire: time.Duration(cacheConfig.Expire) * time.Second / 2,
|
||||||
|
Maxcount: cacheConfig.Maxcount,
|
||||||
}
|
}
|
||||||
case "redis":
|
case "redis":
|
||||||
// cache = &MemoryCache{
|
// cache = &MemoryCache{
|
||||||
|
@ -72,7 +75,7 @@ func NewHandler() *GODNSHandler {
|
||||||
|
|
||||||
hosts := NewHosts(settings.Hosts, settings.Redis)
|
hosts := NewHosts(settings.Hosts, settings.Redis)
|
||||||
|
|
||||||
return &GODNSHandler{resolver, cache, hosts}
|
return &GODNSHandler{resolver, cache, negCache, hosts}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
|
func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
|
||||||
|
@ -123,11 +126,19 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
|
||||||
if IPQuery > 0 {
|
if IPQuery > 0 {
|
||||||
mesg, err := h.cache.Get(key)
|
mesg, err := h.cache.Get(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if mesg, err = h.negCache.Get(key); err != nil {
|
||||||
Debug("%s didn't hit cache: %s", Q.String(), err)
|
Debug("%s didn't hit cache: %s", Q.String(), err)
|
||||||
|
} else {
|
||||||
|
Debug("%s hit negative cache", Q.String())
|
||||||
|
dns.HandleFailed(w, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Debug("%s hit cache", Q.String())
|
Debug("%s hit cache", Q.String())
|
||||||
mesg.Id = req.Id
|
// we need this copy against concurrent modification of Id
|
||||||
w.WriteMsg(mesg)
|
msg := *mesg
|
||||||
|
msg.Id = req.Id
|
||||||
|
w.WriteMsg(&msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,6 +148,11 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Debug("%s", err)
|
Debug("%s", err)
|
||||||
dns.HandleFailed(w, req)
|
dns.HandleFailed(w, req)
|
||||||
|
|
||||||
|
// cache the failure, too!
|
||||||
|
if err = h.negCache.Set(key, nil); err != nil {
|
||||||
|
Debug("Set %s negative cache failed: %v", Q.String(), err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue