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

23
plugin/cache/item_test.go vendored Normal file
View File

@@ -0,0 +1,23 @@
package cache
import (
"reflect"
"testing"
"time"
"github.com/miekg/dns"
)
func TestNewItemPreservesMonotonicClock(t *testing.T) {
now := time.Now()
if reflect.DeepEqual(now, now.Round(0)) {
t.Fatal("time.Now did not include a monotonic clock reading")
}
i := newItem(new(dns.Msg), now, time.Minute)
// DeepEqual compares the complete time representation, including its
// monotonic clock reading. Time.Equal intentionally ignores that detail.
if !reflect.DeepEqual(i.stored, now) {
t.Fatalf("stored time = %v; want original time %v", i.stored, now)
}
}