Cache Plugin: return non-fixed TTL for cached entries (refactor)

This commit is contained in:
Amit 2018-02-09 22:11:12 +02:00
parent 2a6a1852f1
commit 65dfa8ecca
3 changed files with 18 additions and 7 deletions

View File

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

View File

@ -71,7 +71,7 @@ func (plugin *PluginCacheResponse) Eval(pluginsState *PluginsState, msg *dns.Msg
} }
plugin.cachedResponses.cache.Add(cacheKey, cachedResponse) plugin.cachedResponses.cache.Add(cacheKey, cachedResponse)
updateTTL(msg, cachedResponse.expiration) updateTTL(msg, cachedResponse.expiration)
return nil return nil
} }
@ -120,7 +120,7 @@ func (plugin *PluginCache) Eval(pluginsState *PluginsState, msg *dns.Msg) error
return nil return nil
} }
updateTTL(&cached.msg, cached.expiration) updateTTL(&cached.msg, cached.expiration)
synth := cached.msg synth := cached.msg
synth.Id = msg.Id synth.Id = msg.Id

View File

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