if the DNS response Rcode contains an error the cache expires after just 10 seconds.

this should limit the cache in case of temporary upstream DNS errors.
pull/3/head
bloved 2021-09-16 09:40:03 +02:00
parent 2fe0b7b0c2
commit e2a625a92e
1 changed files with 17 additions and 4 deletions

View File

@ -13,7 +13,7 @@ import (
type cacheItem struct { type cacheItem struct {
Query []byte Query []byte
Date time.Time ExpireDate time.Time
} }
//DomainCache stores a domain name inside the cache //DomainCache stores a domain name inside the cache
@ -28,7 +28,17 @@ func DomainCache(s string, resp *dns.Msg) {
if err != nil { if err != nil {
fmt.Println("Problems packing the response: ", err.Error()) fmt.Println("Problems packing the response: ", err.Error())
} }
domain2cache.Date = time.Now() if resp.Rcode == dns.RcodeSuccess{
// on success stores response normally
domain2cache.ExpireDate = time.Now().Add((time.Duration(ZabovCacheTTL) * time.Hour))
}else
{
// on failure stores response for a very short time
if ZabovDebug {
fmt.Println("DomainCache(): DNS error Rcode: ", resp.Rcode, s, "cache time reduced to 10 seconds...")
}
domain2cache.ExpireDate = time.Now().Add((time.Duration(10) * time.Second))
}
err = enc.Encode(domain2cache) err = enc.Encode(domain2cache)
@ -77,7 +87,10 @@ func GetDomainFromCache(s string) *dns.Msg {
return nil return nil
} }
if time.Since(record.Date) > (time.Duration(ZabovCacheTTL) * time.Hour) { if time.Now().After(record.ExpireDate) {
if ZabovDebug {
fmt.Println("GetDomainFromCache(): entry expired:", s)
}
return nil return nil
} }