From 1cc1e3749df6ccefb741232d7949fd5893d84f66 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Thu, 4 Oct 2018 13:38:52 +0300 Subject: [PATCH] dnsfilter -- lazily initialize safebrowsing and parental lookup cache --- dnsfilter/dnsfilter.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/dnsfilter/dnsfilter.go b/dnsfilter/dnsfilter.go index b40d6a38..41ca687e 100644 --- a/dnsfilter/dnsfilter.go +++ b/dnsfilter/dnsfilter.go @@ -130,8 +130,8 @@ const ( // these variables need to survive coredns reload var ( stats Stats - safebrowsingCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() - parentalCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() + safebrowsingCache gcache.Cache + parentalCache gcache.Cache ) // Result holds state of hostname check @@ -551,6 +551,9 @@ func (d *Dnsfilter) checkSafeBrowsing(host string) (Result, error) { } return result, nil } + if safebrowsingCache == nil { + safebrowsingCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() + } result, err := d.lookupCommon(host, &stats.Safebrowsing, safebrowsingCache, true, format, handleBody) return result, err } @@ -594,6 +597,9 @@ func (d *Dnsfilter) checkParental(host string) (Result, error) { } return result, nil } + if parentalCache == nil { + parentalCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() + } result, err := d.lookupCommon(host, &stats.Parental, parentalCache, false, format, handleBody) return result, err } @@ -781,7 +787,9 @@ func New() *Dnsfilter { // Destroy is optional if you want to tidy up goroutines without waiting for them to die off // right now it closes idle HTTP connections if there are any func (d *Dnsfilter) Destroy() { - d.transport.CloseIdleConnections() + if d != nil && d.transport != nil { + d.transport.CloseIdleConnections() + } } //