Coalesce identical misses, refuse non-QUERY opcodes, refresh hot entries early, check the answered question
Concurrent cache misses for one name each went upstream. After a restart, a cache flush or a cluster-object change that is a burst of identical queries exactly when the cache is empty. Identical exchanges (same name, type, class, DO bit, pinned upstream set and forwarded client subnet) now share one: the first query runs it on its own goroutine and context, the rest wait and get private copies, and the leader alone writes the cache. If the leader's client leaves, waiters take over instead of failing with its cancellation. Shared answers are counted in fancydns_upstream_coalesced_total. validate() accepted OpcodeUpdate, so a dynamic update was forwarded upstream as an ordinary lookup, and every other opcode got FORMERR. A recursive resolver implements QUERY only: anything else is now NOTIMP (RFC 1035 4.1.1), checked before the question count since STATUS carries no question. Prefetch only fired when a stale entry was served, so the first client after a hot name expired got a TTL-1 stale answer. A fresh entry is now refreshed in the background when it has been served at least twice and under a tenth of its lifetime remains; the hit counter restarts with each stored entry, so a name nobody comes back for stops being refreshed. Counted in fancydns_cache_prefetch_total. miekg/dns matches only the message ID of an upstream reply, so a forged reply with the right ID for a different question was accepted and cached under the name that was asked. exchangeOne now requires the reply to carry the question that was sent (an empty section only for FORMERR, SERVFAIL, NOTIMP and REFUSED). A mismatch fails the attempt without counting against the upstream, so a spoofer cannot trip the circuit breaker. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1a536e7d0e
commit
dd1117fd91
8 files changed
+498
-9
No files matched your search
Vendored
+29
-1
@@ -9,6 +9,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
||||
"git.k8s-git-k6n.net/mats/fancy-dns/internal/metrics"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -153,8 +155,11 @@ func (m *Manager) Get(k Key) Result {
|
||||
}
|
||||
if kind == Stale {
|
||||
res.TTL = 0
|
||||
if p := m.prefetchFn.Load(); p != nil && m.opt.Prefetch && m.lru.TryPrefetch(k) {
|
||||
}
|
||||
if m.opt.Prefetch && (kind == Stale || m.nearExpiry(e, now)) {
|
||||
if p := m.prefetchFn.Load(); p != nil && m.lru.TryPrefetch(k) {
|
||||
fn := *p
|
||||
metrics.RecordPrefetch()
|
||||
go func() {
|
||||
defer m.lru.ReleasePrefetch(k)
|
||||
fn(k)
|
||||
@@ -164,6 +169,29 @@ func (m *Manager) Get(k Key) Result {
|
||||
return res
|
||||
}
|
||||
|
||||
// Prefetch thresholds. An entry is refreshed ahead of its expiry when it is
|
||||
// popular (it has been served at least prefetchMinHits times since it was
|
||||
// stored) and has under a tenth of its lifetime left, so a hot name is replaced
|
||||
// before a client ever sees it expire. The hit counter starts again with each
|
||||
// stored entry, so a name that stops being asked for stops being refreshed
|
||||
// after one more cycle.
|
||||
const (
|
||||
prefetchMinHits = 2
|
||||
prefetchRemaining = 10 // refresh when remaining < lifetime/prefetchRemaining
|
||||
prefetchMinLifetime = 10 * time.Second
|
||||
)
|
||||
|
||||
func (m *Manager) nearExpiry(e *Entry, now time.Time) bool {
|
||||
if e.Neg || e.Hits.Load() < prefetchMinHits {
|
||||
return false
|
||||
}
|
||||
life := e.ExpiresAt.Sub(e.CreatedAt)
|
||||
if life < prefetchMinLifetime {
|
||||
return false
|
||||
}
|
||||
return e.ExpiresAt.Sub(now) < life/prefetchRemaining
|
||||
}
|
||||
|
||||
// unpack rebuilds a private message from stored wire bytes and ages the TTLs
|
||||
// down to what is left of the entry's lifetime.
|
||||
func (e *Entry) unpack(k Key, now time.Time) (*dns.Msg, error) {
|
||||
|
||||
Reference in new issue
Block a user