dns

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package dns provides cached DNS resolution for scanorama.

It supports both forward lookups (hostname → []IP) and reverse lookups (IP → hostname) backed by the dns_cache database table. Every result — including negative ones — is stored so that repeated queries hit the database instead of the OS resolver.

TTL behavior ------------- Each cache row carries its own ttl_seconds value. Two TTLs are in play:

  • TTL (default 1 h) — used for successful positive results.
  • NegativeTTL (default 5 min) — used for NXDOMAIN / empty answers and resolver errors, so transient failures do not get pinned for an hour.

Both can be overridden at construction time via WithTTL / WithNegativeTTL.

Cache invalidation ------------------ Entries expire passively when their TTL window closes; the next lookup for that key then triggers a fresh network query. Two explicit invalidation methods are also provided:

  • Invalidate(ctx, direction, key) — removes all rows for one key.
  • InvalidateAll(ctx) — truncates the entire dns_cache table.

RefreshStale can be called from a background goroutine or scheduled job to proactively re-resolve entries whose TTL has already expired.

Concurrency ----------- Resolver is safe for concurrent use. A singleflight group ensures that simultaneous callers for the same key share one in-flight network lookup.

Index

Constants

View Source
const DefaultNegativeTTL = 5 * time.Minute

DefaultNegativeTTL is used for NXDOMAIN / empty-answer / resolver-error entries. It is intentionally short so transient failures resolve quickly.

View Source
const DefaultTTL = time.Hour

DefaultTTL is used for successful positive cache entries when the caller does not supply an explicit TTL via WithTTL.

Variables

View Source
var ErrNoRecords = errors.New("dns: lookup returned no records")

ErrNoRecords is returned by a Lookup method when the resolver returned a successful response that contained no usable records (NXDOMAIN / empty answer section).

Functions

This section is empty.

Types

type Direction

type Direction string

Direction distinguishes the two lookup directions stored in dns_cache.

const (
	DirectionForward Direction = "forward" // hostname → IP
	DirectionReverse Direction = "reverse" // IP       → hostname
)

type Option

type Option func(*Resolver)

Option configures a Resolver.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the structured logger used for warnings and debug output.

func WithNegativeTTL

func WithNegativeTTL(d time.Duration) Option

WithNegativeTTL overrides the default TTL used for NXDOMAIN / empty-answer / resolver-error cache entries.

func WithNetResolver

func WithNetResolver(nr *net.Resolver) Option

WithNetResolver replaces the underlying net.Resolver (useful in tests that want to verify the Option wiring without injecting lookupHostFn directly).

func WithTTL

func WithTTL(d time.Duration) Option

WithTTL overrides the default TTL for successful positive cache entries.

type Resolver

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

Resolver performs cached DNS lookups.

func New

func New(database *db.DB, opts ...Option) *Resolver

New creates a Resolver backed by the given database connection.

func (*Resolver) Invalidate

func (r *Resolver) Invalidate(ctx context.Context, direction Direction, key string) error

Invalidate removes all dns_cache entries for the given direction and key, forcing the next lookup to perform a fresh network query.

This is useful when an external event (e.g. a DHCP lease change or a manual host update) makes the cached value known-stale before its TTL expires.

func (*Resolver) InvalidateAll

func (r *Resolver) InvalidateAll(ctx context.Context) error

InvalidateAll truncates the entire dns_cache table. All subsequent lookups will hit the network until their results are re-cached.

Use with care — this is a heavy operation intended for administrative resets, not routine use.

func (*Resolver) LookupAddr

func (r *Resolver) LookupAddr(ctx context.Context, ip string) (string, error)

LookupAddr performs a reverse PTR lookup for the given IP address and returns the first hostname (trailing dot stripped). Results are cached; ErrNoRecords is returned for empty / NXDOMAIN responses.

func (*Resolver) LookupHost

func (r *Resolver) LookupHost(ctx context.Context, host string) ([]string, error)

LookupHost resolves a hostname to one or more IP address strings. The cache is consulted first; a live lookup is only performed when all cached entries are stale or absent.

On success the returned slice contains at least one element. ErrNoRecords is returned when the resolver returned an empty answer.

func (*Resolver) RefreshStale

func (r *Resolver) RefreshStale(ctx context.Context) error

RefreshStale finds all dns_cache rows whose TTL has expired and re-resolves them. It is intended to be called from a background goroutine or scheduled job; it respects the supplied context for cancellation.

Jump to

Keyboard shortcuts

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