Merge pull request #11 from tgulacsi/negcache

Add negative cache
This commit is contained in:
kenshin 2015-02-25 13:50:09 +08:00
commit 4cdcc43bc9
2 changed files with 33 additions and 17 deletions

View File

@ -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 {

View File

@ -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
} }