dnsfilter -- lazily initialize safebrowsing and parental lookup cache

This commit is contained in:
Eugene Bujak 2018-10-04 13:38:52 +03:00
parent cb97a254a5
commit 1cc1e3749d
1 changed files with 11 additions and 3 deletions

View File

@ -130,8 +130,8 @@ const (
// these variables need to survive coredns reload // these variables need to survive coredns reload
var ( var (
stats Stats stats Stats
safebrowsingCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() safebrowsingCache gcache.Cache
parentalCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() parentalCache gcache.Cache
) )
// Result holds state of hostname check // Result holds state of hostname check
@ -551,6 +551,9 @@ func (d *Dnsfilter) checkSafeBrowsing(host string) (Result, error) {
} }
return result, nil 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) result, err := d.lookupCommon(host, &stats.Safebrowsing, safebrowsingCache, true, format, handleBody)
return result, err return result, err
} }
@ -594,6 +597,9 @@ func (d *Dnsfilter) checkParental(host string) (Result, error) {
} }
return result, nil 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) result, err := d.lookupCommon(host, &stats.Parental, parentalCache, false, format, handleBody)
return result, err return result, err
} }
@ -781,8 +787,10 @@ func New() *Dnsfilter {
// Destroy is optional if you want to tidy up goroutines without waiting for them to die off // 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 // right now it closes idle HTTP connections if there are any
func (d *Dnsfilter) Destroy() { func (d *Dnsfilter) Destroy() {
if d != nil && d.transport != nil {
d.transport.CloseIdleConnections() d.transport.CloseIdleConnections()
} }
}
// //
// config manipulation helpers // config manipulation helpers