diff --git a/dnscrypt-proxy/plugin_cache.go b/dnscrypt-proxy/plugin_cache.go index 9f1ddf68..6279a7a5 100644 --- a/dnscrypt-proxy/plugin_cache.go +++ b/dnscrypt-proxy/plugin_cache.go @@ -10,6 +10,8 @@ import ( "github.com/miekg/dns" ) +const StaleResponseTtl = 30 * time.Second + type CachedResponse struct { expiration time.Time msg dns.Msg @@ -68,30 +70,35 @@ func (plugin *PluginCache) Reload() error { func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error { cacheKey := computeCacheKey(pluginsState, msg) + cachedResponses.RLock() - defer cachedResponses.RUnlock() if cachedResponses.cache == nil { + cachedResponses.RUnlock() return nil } cachedAny, ok := cachedResponses.cache.Get(cacheKey) if !ok { + cachedResponses.RUnlock() return nil } cached := cachedAny.(CachedResponse) - + expiration := cached.expiration synth := cached.msg.Copy() + cachedResponses.RUnlock() + synth.Id = msg.Id synth.Response = true synth.Compress = true synth.Question = msg.Question - if time.Now().After(cached.expiration) { - updateTTL(synth, time.Now().Add(30 * time.Second)) + if time.Now().After(expiration) { + expiration2 := time.Now().Add(StaleResponseTtl) + updateTTL(synth, expiration2) pluginsState.sessionData["stale"] = synth return nil } - updateTTL(synth, cached.expiration) + updateTTL(synth, expiration) pluginsState.synthResponse = synth pluginsState.action = PluginsActionSynth