Cache Plugin: return non-fixed TTL for cached entries

This commit is contained in:
Amit 2018-02-09 17:59:04 +02:00
parent c42ae840db
commit 2a6a1852f1
3 changed files with 14 additions and 5 deletions

View File

@ -83,20 +83,24 @@ func getMinTTL(msg *dns.Msg, minTTL uint32, maxTTL uint32, negCacheMinTTL uint32
return time.Duration(ttl) * time.Second
}
func setMaxTTL(msg *dns.Msg, ttl uint32) {
func setMaxTTL(msg *dns.Msg, ttl uint32, force bool) {
for _, rr := range msg.Answer {
if ttl < rr.Header().Ttl {
if (ttl < rr.Header().Ttl) || force {
rr.Header().Ttl = ttl
}
}
for _, rr := range msg.Ns {
if ttl < rr.Header().Ttl {
if (ttl < rr.Header().Ttl) || force {
rr.Header().Ttl = ttl
}
}
for _, rr := range msg.Extra {
if ttl < rr.Header().Ttl {
if (ttl < rr.Header().Ttl) || force {
rr.Header().Ttl = ttl
}
}
}
func updateTTL(msg *dns.Msg, expiration time.Time) {
setMaxTTL(msg, uint32(time.Until(expiration) / time.Second), true)
}

View File

@ -70,6 +70,8 @@ func (plugin *PluginCacheResponse) Eval(pluginsState *PluginsState, msg *dns.Msg
}
}
plugin.cachedResponses.cache.Add(cacheKey, cachedResponse)
updateTTL(msg, cachedResponse.expiration)
return nil
}
@ -117,6 +119,9 @@ func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error
if time.Now().After(cached.expiration) {
return nil
}
updateTTL(&cached.msg, cached.expiration)
synth := cached.msg
synth.Id = msg.Id
synth.Response = true

View File

@ -178,7 +178,7 @@ func (pluginsState *PluginsState) ApplyResponsePlugins(pluginsGlobals *PluginsGl
}
pluginsGlobals.RUnlock()
if ttl != nil {
setMaxTTL(&msg, *ttl)
setMaxTTL(&msg, *ttl, false)
}
packet2, err := msg.PackBuffer(packet)
if err != nil {