plugin/cache: preserve monotonic time for TTL expiry (#8346)

Keep cache timestamps and TTL calculations on the time values returned by the cache clock. Converting them with UTC strips Go's monotonic clock reading and can extend cached entries when the wall clock moves backward.

Add a regression test that verifies new cache items retain the original monotonic timestamp.

Fixes #5478.

Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>
This commit is contained in:
houyuwushang
2026-07-30 09:12:56 +08:00
committed by GitHub
parent 3c2c33bb50
commit c0adbae99b
3 changed files with 29 additions and 4 deletions

View File

@@ -26,7 +26,7 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
return plugin.NextOrFailure(c.Name(), c.Next, ctx, w, rc)
}
now := c.now().UTC()
now := c.now()
server := metrics.WithServer(ctx)
// On cache refresh, we will just use the DO bit from the incoming query for the refresh since we key our cache
@@ -158,7 +158,7 @@ func (c *Cache) verifyWithTimeout(ctx context.Context, state request.Request, w
// Should not happen: refreshed=true means the upstream response was cacheable.
return true, res.code, res.err
}
now := c.now().UTC()
now := c.now()
if c.keepttl {
now = fresh.stored
}