Define a constant for the TTL of stale responses

This commit is contained in:
Frank Denis 2021-09-25 19:53:25 +02:00
parent 2a3e59c4bf
commit e5608e08cf
1 changed files with 12 additions and 5 deletions

View File

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