mirror of
https://github.com/coredns/coredns.git
synced 2026-08-20 23:08:28 -04:00
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:
23
plugin/cache/item_test.go
vendored
Normal file
23
plugin/cache/item_test.go
vendored
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user