cache

package
v0.6.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultFailureTTL uint32 = 5

DefaultFailureTTL is the typical RFC 9520 §3 "small number of seconds" negative-failure window: 5 s by default, balancing fast recovery on transient flakes against worthwhile suppression of repeat work.

View Source
const MaxFailureTTL uint32 = 30

MaxFailureTTL is the RFC 9520 §4 ceiling on resolution-failure caching. "DNS resolvers SHOULD cache resolution failures … TTL … MUST NOT exceed 5 minutes." We pick a tighter 30-second cap so transient outages clear quickly while still protecting upstream from the per-query stampede.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a sharded in-memory DNS cache with TTL-based expiration.

func NewCache

func NewCache(maxEntries int, minTTL, maxTTL, negMaxTTL uint32, m *metrics.Metrics) *Cache

NewCache creates a new sharded DNS cache.

func NewCacheWithStale

func NewCacheWithStale(maxEntries int, minTTL, maxTTL, negMaxTTL uint32, serveStale bool, staleTTL uint32, m *metrics.Metrics) *Cache

NewCacheWithStale creates a cache with optional serve-stale support (RFC 8767).

func (*Cache) Delete

func (c *Cache) Delete(name string, qtype uint16, class uint16) bool

Delete removes a specific entry from the cache. Returns true if the entry was found and deleted.

func (*Cache) DetailedStats

func (c *Cache) DetailedStats() CacheStats

DetailedStats returns detailed cache statistics including positive/negative breakdowns.

func (*Cache) Flush

func (c *Cache) Flush()

Flush clears all cache entries.

func (*Cache) Get

func (c *Cache) Get(name string, qtype uint16, class uint16) (*Entry, bool)

Get retrieves an entry from the cache with TTL decay.

func (*Cache) GetStale

func (c *Cache) GetStale(name string, qtype uint16, class uint16) (*Entry, bool)

GetStale retrieves an expired entry from the cache for serve-stale (RFC 8767). Returns the entry with staleTTL if serve-stale is enabled and the entry exists but is expired.

The returned entry's DNSSECStatus is cleared: when records are served past their TTL the covering RRSIGs may also be past their signature-inception/ expiration window, so we cannot honestly carry over the original "secure" verdict. Downstream validators that care will re-verify; the AD bit on a stale serve would be a lie.

func (*Cache) GetWithECS

func (c *Cache) GetWithECS(name string, qtype uint16, class uint16, ecsPrefix string) (*Entry, bool)

GetWithECS retrieves a cache entry using an ECS-aware key. If ecsPrefix is empty, it behaves like Get.

func (*Cache) Lookup

func (c *Cache) Lookup(name string, qtype uint16, class uint16) (*Entry, bool)

Lookup retrieves an entry from the cache without deleting expired entries. Unlike Get, this is a read-only operation suitable for inspection.

func (*Cache) LookupAll

func (c *Cache) LookupAll(name string, class uint16) []*Entry

LookupAll returns all cached entries for the given name (across all types).

func (*Cache) NegativeEntries

func (c *Cache) NegativeEntries(limit int) []NegativeEntryInfo

NegativeEntries returns up to limit negative cache entries across all shards.

func (*Cache) SetHardenBelowNX

func (c *Cache) SetHardenBelowNX(enabled bool)

SetHardenBelowNX enables or disables RFC 8020 harden-below-nxdomain.

func (*Cache) SetPrefetchEnabled

func (c *Cache) SetPrefetchEnabled(enabled bool)

SetPrefetchEnabled enables or disables proactive cache prefetching.

func (*Cache) SetPrefetchFunc

func (c *Cache) SetPrefetchFunc(fn func(name string, qtype, qclass uint16))

SetPrefetchFunc sets the callback used to re-resolve entries nearing expiry.

func (*Cache) StartSweeper

func (c *Cache) StartSweeper(ctx context.Context, interval time.Duration)

StartSweeper runs a background goroutine that periodically evicts expired entries.

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns current cache statistics.

func (*Cache) Store

func (c *Cache) Store(name string, qtype uint16, class uint16, answers []dns.ResourceRecord, authority []dns.ResourceRecord)

Store caches a positive DNS result.

func (*Cache) StoreFailure added in v0.6.11

func (c *Cache) StoreFailure(name string, qtype uint16, class uint16, ttl uint32)

StoreFailure records a resolution-failure (SERVFAIL) negative entry for the (name, qtype, class) triple, with TTL clamped to [1, MaxFailureTTL]. RFC 9520 §3: "Recursive servers MUST cache resolution failures." Without this cap, a client retrying a broken name every 100 ms re-runs the full iterative chain every time — a small loop can swamp an upstream auth server with thousands of QPS. The entry stores no records and emits RCODE=SERVFAIL on cache hit, just like the upstream would have.

func (*Cache) StoreNegative

func (c *Cache) StoreNegative(name string, qtype uint16, class uint16, negType NegativeType, rcode uint8, authority []dns.ResourceRecord)

StoreNegative caches a negative DNS result (NXDOMAIN/NODATA). NXDOMAIN applies to the entire name (RFC 2308 §3) so it is stored with qtype=0 as a sentinel. NODATA is type-specific.

func (*Cache) StoreWithECS

func (c *Cache) StoreWithECS(name string, qtype uint16, class uint16, ecsPrefix string, answers []dns.ResourceRecord, authority []dns.ResourceRecord)

StoreWithECS caches a positive DNS result with an ECS prefix key. If ecsPrefix is empty, it behaves like Store.

func (*Cache) StoreWithECSStatus added in v0.6.9

func (c *Cache) StoreWithECSStatus(
	name string, qtype uint16, class uint16,
	ecsPrefix string, ecsScope uint8,
	answers []dns.ResourceRecord, authority []dns.ResourceRecord,
	dnssecStatus string,
)

StoreWithECSStatus caches a positive DNS result keyed by ECS source prefix along with the DNSSEC validator's verdict and the authoritative server's returned ECS SCOPE PREFIX-LENGTH (echoed to the client per RFC 7871 §7.2.1). ecsPrefix == "" routes to StoreWithStatus (global, all clients share).

func (*Cache) StoreWithStatus added in v0.6.3

func (c *Cache) StoreWithStatus(name string, qtype uint16, class uint16, answers []dns.ResourceRecord, authority []dns.ResourceRecord, dnssecStatus string)

StoreWithStatus caches a positive DNS result together with the validator's verdict ("secure", "insecure", "bogus", or ""). The status is consumed by the server to set the AD bit on cache-served responses without re-running DNSSEC validation per lookup.

type CacheStats

type CacheStats struct {
	Entries         int
	PositiveEntries int
	NegativeEntries int
}

CacheStats holds cache statistics.

type Entry

type Entry struct {
	Records    []dns.ResourceRecord
	Authority  []dns.ResourceRecord
	InsertedAt time.Time
	OrigTTL    uint32
	Negative   bool
	NegType    NegativeType
	SOA        *dns.ResourceRecord
	RCODE      uint8

	// DNSSECStatus records the validator's verdict on the cached records:
	// "secure", "insecure", "bogus", or "" (unknown). Used by the server
	// to set the AD bit on responses served from cache without re-running
	// validation.
	DNSSECStatus string

	// ECSScope is the SCOPE PREFIX-LENGTH the authoritative server
	// returned in its ECS option (RFC 7871 §6). Echoed back to the client
	// in the response OPT when the client itself sent ECS. Zero means the
	// answer is not subnet-scoped (global cache entry shared across all
	// clients).
	ECSScope uint8
	// contains filtered or unexported fields
}

Entry represents a cached DNS result.

func (*Entry) Expired

func (e *Entry) Expired() bool

Expired returns true if the entry has expired.

func (*Entry) RemainingTTL

func (e *Entry) RemainingTTL() uint32

RemainingTTL returns the remaining TTL in seconds.

func (*Entry) WithDecayedTTL

func (e *Entry) WithDecayedTTL(remaining uint32) *Entry

WithDecayedTTL returns a deep copy of the entry with TTL adjusted.

type NegativeEntryInfo

type NegativeEntryInfo struct {
	Name         string               `json:"name"`
	QType        string               `json:"qtype"`
	NegType      string               `json:"neg_type"`
	RCODE        string               `json:"rcode"`
	RemainingTTL uint32               `json:"remaining_ttl"`
	Authority    []ResourceRecordInfo `json:"authority"`
}

NegativeEntryInfo holds information about a negative cache entry.

type NegativeType

type NegativeType uint8

NegativeType indicates the type of negative cache entry.

const (
	NegNone     NegativeType = 0
	NegNXDomain NegativeType = 1
	NegNoData   NegativeType = 2
	// NegServFail is the short-lived "resolution failure" cache entry
	// introduced for RFC 9520 §3. When the recursive resolver cannot
	// produce an answer (upstream timeout, all NS unreachable, broken
	// delegation, signed-zone bogus), we cache the failure for a few
	// seconds so an aggressive client cannot amplify upstream load by
	// retrying the same expensive iterative chain on every miss. TTL
	// is capped at 30 s per the RFC's recommendation.
	NegServFail NegativeType = 3
)

type ResourceRecordInfo

type ResourceRecordInfo struct {
	Name  string `json:"name"`
	Type  string `json:"type"`
	TTL   uint32 `json:"ttl"`
	RData string `json:"rdata"`
}

ResourceRecordInfo holds a human-readable representation of a DNS resource record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL