server

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Overview

Package server provides the core DNS server implementation.

Index

Constants

View Source
const ClassCHAOS = 3

ClassCHAOS is the DNS class for server identity and metadata.

View Source
const DLQChannel = "dns:invalidation:dlq"

DLQChannel is the Redis list key for dead letter queue of failed invalidation messages.

View Source
const InvalidationChannel = "dns:invalidation"

InvalidationChannel is the Redis pub/sub channel for cache invalidation events.

Variables

This section is empty.

Functions

This section is empty.

Types

type DNSCache

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

DNSCache implements a sharded, thread-safe, in-memory cache for DNS responses. Sharding is used to minimize lock contention during high-concurrency access.

func NewDNSCache

func NewDNSCache(done <-chan struct{}, wg *sync.WaitGroup) *DNSCache

NewDNSCache initializes a new DNSCache with pre-allocated shards and starts the background expiration cleanup loop. The done channel controls when the cleanup goroutine exits. If wg is provided, wg.Add(1) is called and wg.Done() is called when the cleanup goroutine exits.

func (*DNSCache) Cleanup

func (c *DNSCache) Cleanup()

Cleanup scans all shards and deletes items that have passed their expiration time.

func (*DNSCache) Flush

func (c *DNSCache) Flush()

Flush clears all entries from the DNS cache.

func (*DNSCache) Get

func (c *DNSCache) Get(key string) ([]byte, bool)

Get retrieves a response from the cache. It returns (nil, false) if the key is missing or has already expired. Note: callers must not retain or mutate the returned slice.

func (*DNSCache) GetInto

func (c *DNSCache) GetInto(key string, txID uint16) ([]byte, bool)

GetInto returns data from the cache with the transaction ID injected. For data >= 2 bytes: TXID is written at offset 0 (overwriting first 2 bytes of data). For data < 2 bytes: no TXID injection (would corrupt too-short responses). The returned slice is from a pooled buffer — callers must not retain or mutate it.

func (*DNSCache) Invalidate

func (c *DNSCache) Invalidate(key string)

Invalidate removes a specific key from the cache.

func (*DNSCache) Ping

func (c *DNSCache) Ping(ctx context.Context) error

Ping verifies the cache is responsive by checking the context.

func (*DNSCache) Set

func (c *DNSCache) Set(key string, data []byte, ttl time.Duration)

Set stores a response in the cache with a specific TTL. The input data is copied so the cache owns its own backing array.

func (*DNSCache) SetNoCopy

func (c *DNSCache) SetNoCopy(key string, data []byte, ttl time.Duration)

SetNoCopy stores data directly without copying. The caller must not retain or mutate the passed slice after calling this method. Intended for data that is already an independent heap copy (e.g., from Redis).

type RedisCache

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

RedisCache implements a DNS cache backed by Redis.

func NewRedisCache

func NewRedisCache(addr string, password string, db int, cfg RedisPoolConfig) *RedisCache

NewRedisCache creates a new Redis cache client.

func (*RedisCache) Close

func (r *RedisCache) Close() error

Close shuts down the Redis client connection.

func (*RedisCache) DLQLen

func (r *RedisCache) DLQLen(ctx context.Context) (int64, error)

DLQLen returns the current length of the dead letter queue.

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string) ([]byte, bool)

Get retrieves a cached DNS response by key.

func (*RedisCache) GetWithTTL

func (r *RedisCache) GetWithTTL(ctx context.Context, key string) ([]byte, time.Duration, bool)

GetWithTTL retrieves cached data and remaining TTL in a single pipeline call.

func (*RedisCache) Invalidate

func (r *RedisCache) Invalidate(ctx context.Context, name string, qType domain.RecordType) error

Invalidate deletes the key from Redis and publishes an invalidation event to all nodes. When qType is empty, publishes a zone-level invalidation (zone name only, no type suffix).

func (*RedisCache) Ping

func (r *RedisCache) Ping(ctx context.Context) error

Ping checks Redis connectivity.

func (*RedisCache) PoolConfig

func (r *RedisCache) PoolConfig() RedisPoolConfig

PoolConfig returns the pool configuration currently applied to the client.

func (*RedisCache) PopFromDLQ

func (r *RedisCache) PopFromDLQ(ctx context.Context, timeout time.Duration) (string, error)

PopFromDLQ pops a message from the dead letter queue with blocking. Returns ("", nil) if timeout is reached before a message is available.

func (*RedisCache) PushToDLQ

func (r *RedisCache) PushToDLQ(ctx context.Context, msg string) error

PushToDLQ pushes a failed invalidation message to the dead letter queue. The message is stored with a timestamp prefix for ordering.

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, data []byte, ttl time.Duration)

Set stores a DNS response in the cache with the given TTL.

func (*RedisCache) Subscribe

func (r *RedisCache) Subscribe(ctx context.Context) *redis.PubSub

Subscribe returns a PubSub instance that receives invalidation keys.

type RedisPoolConfig

type RedisPoolConfig struct {
	PoolSize        int
	MinIdleConns    int
	PoolTimeout     time.Duration
	ConnMaxLifetime time.Duration
}

RedisPoolConfig holds connection pool settings for the Redis client.

type Server

type Server struct {
	Addr            string
	Repo            ports.DNSRepository
	Cache           *DNSCache
	Redis           *RedisCache
	DNSSEC          *services.DNSSECService
	DNSSECValidator *services.DNSSECValidator
	DNSSECMode      string // "disabled", "ad-bit-only", "strict"
	DNSSECConfig    *config.DNSSECConfig

	WorkerCount int

	Logger *slog.Logger

	TsigKeys         map[string][]byte
	NodeID           string
	RecursionEnabled bool
	CookieSecret     []byte

	// Testing/Chaos flags
	SimulateDBLatency  time.Duration
	NotifyPortOverride int
	DisableAsync       bool // If true, NOTIFY and UPDATE handlers won't spawn goroutines

	// TLS Config for DoT, DoH, and DoQ
	TLSConfig *tls.Config
	DoQAddr   string // DNS-over-QUIC listen address (default ":853")
	// contains filtered or unexported fields
}

Server is the core DNS server that handles incoming queries, zone transfers (AXFR/IXFR), updates (RFC 2136), and NOTIFY (RFC 1996).

func NewServer

func NewServer(addr string, repo ports.DNSRepository, logger *slog.Logger) *Server

NewServer creates a new DNS server instance.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run starts the DNS server and blocks until the context is canceled.

Jump to

Keyboard shortcuts

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