refactor(cache): modernize with generics (#7842)

This commit is contained in:
vflaux
2026-02-04 02:23:53 +01:00
committed by GitHub
parent 923a8b5d2b
commit 30c20b52ff
13 changed files with 70 additions and 68 deletions

View File

@@ -23,7 +23,7 @@ func hash(rrs []dns.RR) uint64 {
return h.Sum64()
}
func periodicClean(c *cache.Cache, stop <-chan struct{}) {
func periodicClean(c *cache.Cache[[]dns.RR], stop <-chan struct{}) {
tick := time.NewTicker(8 * time.Hour)
defer tick.Stop()
for {
@@ -32,8 +32,8 @@ func periodicClean(c *cache.Cache, stop <-chan struct{}) {
// we sign for 8 days, check if a signature in the cache reached 75% of that (i.e. 6), if found delete
// the signature
is75 := time.Now().UTC().Add(twoDays)
c.Walk(func(items map[uint64]any, key uint64) bool {
for _, rr := range items[key].([]dns.RR) {
c.Walk(func(items map[uint64][]dns.RR, key uint64) bool {
for _, rr := range items[key] {
if !rr.(*dns.RRSIG).ValidityPeriod(is75) {
delete(items, key)
}

View File

@@ -7,6 +7,8 @@ import (
"github.com/coredns/coredns/plugin/pkg/cache"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func TestCacheSet(t *testing.T) {
@@ -20,7 +22,7 @@ func TestCacheSet(t *testing.T) {
t.Fatalf("Failed to parse key: %v\n", err)
}
c := cache.New(defaultCap)
c := cache.New[[]dns.RR](defaultCap)
m := testMsg()
state := request.Request{Req: m, Zone: "miek.nl."}
k := hash(m.Answer) // calculate *before* we add the sig
@@ -44,7 +46,7 @@ func TestCacheNotValidExpired(t *testing.T) {
t.Fatalf("Failed to parse key: %v\n", err)
}
c := cache.New(defaultCap)
c := cache.New[[]dns.RR](defaultCap)
m := testMsg()
state := request.Request{Req: m, Zone: "miek.nl."}
k := hash(m.Answer) // calculate *before* we add the sig
@@ -68,7 +70,7 @@ func TestCacheNotValidYet(t *testing.T) {
t.Fatalf("Failed to parse key: %v\n", err)
}
c := cache.New(defaultCap)
c := cache.New[[]dns.RR](defaultCap)
m := testMsg()
state := request.Request{Req: m, Zone: "miek.nl."}
k := hash(m.Answer) // calculate *before* we add the sig

View File

@@ -22,11 +22,11 @@ type Dnssec struct {
keys []*DNSKEY
splitkeys bool
inflight *singleflight.Group
cache *cache.Cache
cache *cache.Cache[[]dns.RR]
}
// New returns a new Dnssec.
func New(zones []string, keys []*DNSKEY, splitkeys bool, next plugin.Handler, c *cache.Cache) Dnssec {
func New(zones []string, keys []*DNSKEY, splitkeys bool, next plugin.Handler, c *cache.Cache[[]dns.RR]) Dnssec {
return Dnssec{Next: next,
zones: zones,
keys: keys,
@@ -152,7 +152,7 @@ func (d Dnssec) get(key uint64, server string) ([]dns.RR, bool) {
if s, ok := d.cache.Get(key); ok {
// we sign for 8 days, check if a signature in the cache reached 3/4 of that
is75 := time.Now().UTC().Add(twoDays)
for _, rr := range s.([]dns.RR) {
for _, rr := range s {
if !rr.(*dns.RRSIG).ValidityPeriod(is75) {
cacheMisses.WithLabelValues(server).Inc()
return nil, false
@@ -160,7 +160,7 @@ func (d Dnssec) get(key uint64, server string) ([]dns.RR, bool) {
}
cacheHits.WithLabelValues(server).Inc()
return s.([]dns.RR), true
return s, true
}
cacheMisses.WithLabelValues(server).Inc()
return nil, false

View File

@@ -69,7 +69,7 @@ func TestSigningDifferentZone(t *testing.T) {
m := testMsgEx()
state := request.Request{Req: m, Zone: "example.org."}
c := cache.New(defaultCap)
c := cache.New[[]dns.RR](defaultCap)
d := New([]string{"example.org."}, []*DNSKEY{key}, false, nil, c)
m = d.Sign(state, time.Now().UTC(), server)
if !section(m.Answer, 1) {
@@ -250,7 +250,7 @@ func testEmptyMsg() *dns.Msg {
func newDnssec(t *testing.T, zones []string) (Dnssec, func(), func()) {
t.Helper()
k, rm1, rm2 := newKey(t)
c := cache.New(defaultCap)
c := cache.New[[]dns.RR](defaultCap)
d := New(zones, []*DNSKEY{k}, false, nil, c)
return d, rm1, rm2
}

View File

@@ -170,7 +170,7 @@ func TestLookupZone(t *testing.T) {
dnskey, rm1, rm2 := newKey(t)
defer rm1()
defer rm2()
c := cache.New(defaultCap)
c := cache.New[[]dns.RR](defaultCap)
dh := New([]string{"miek.nl."}, []*DNSKEY{dnskey}, false, fm, c)
for _, tc := range dnsTestCases {
@@ -193,7 +193,7 @@ func TestLookupDNSKEY(t *testing.T) {
dnskey, rm1, rm2 := newKey(t)
defer rm1()
defer rm2()
c := cache.New(defaultCap)
c := cache.New[[]dns.RR](defaultCap)
dh := New([]string{"miek.nl."}, []*DNSKEY{dnskey}, false, test.ErrorHandler(), c)
for _, tc := range dnssecTestCases {

View File

@@ -12,6 +12,8 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/cache"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns"
)
var log = clog.NewWithPlugin("dnssec")
@@ -24,7 +26,7 @@ func setup(c *caddy.Controller) error {
return plugin.Error("dnssec", err)
}
ca := cache.New(capacity)
ca := cache.New[[]dns.RR](capacity)
stop := make(chan struct{})
c.OnShutdown(func() error {