Documentation
¶
Overview ¶
Package endpoint_cache implements a database-backed endpoint override cache with an in-memory layer for fast lookups.
On initialization, all EndpointOverride rows are loaded from the database into a sync.RWMutex-protected map. All read operations (Resolve, ResolveMX, Get, List) are served from memory — no database round-trip on the hot path.
Write operations (Set, Delete) update memory first, then persist to the database. This ensures that the running server always has instant access to the latest overrides without any DB latency.
For IP addresses without an override, the IP itself is returned (an IP is already a concrete endpoint — no resolution needed).
For domain names without an override, an empty string is returned so that the caller falls through to OS DNS resolution.
Index ¶
- type Cache
- func (c *Cache) Delete(lookupKey string) error
- func (c *Cache) Get(lookupKey string) (*mdb.EndpointOverride, error)
- func (c *Cache) List() ([]mdb.EndpointOverride, error)
- func (c *Cache) Resolve(ctx context.Context, key string) (string, error)
- func (c *Cache) ResolveMX(ctx context.Context, domain string) (records []*net.MX, cacheHit bool, err error)
- func (c *Cache) Set(lookupKey, targetHost, comment string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache wraps a GORM database to provide endpoint resolution with local overrides. All reads are served from the in-memory map; writes go to memory first, then DB.
func New ¶
New creates an endpoint_cache.Cache from the given GORM database connection. It automatically runs AutoMigrate for the EndpointOverride table, then loads all existing overrides into memory.
func (*Cache) Delete ¶
Delete removes an endpoint override entry. Memory is updated first, then persisted to the database.
func (*Cache) Get ¶
func (c *Cache) Get(lookupKey string) (*mdb.EndpointOverride, error)
Get retrieves a single endpoint override entry from memory.
func (*Cache) List ¶
func (c *Cache) List() ([]mdb.EndpointOverride, error)
List returns all endpoint override entries from memory.
func (*Cache) Resolve ¶
Resolve looks up the target host for the given key (domain name or IP).
Behaviour:
- If an explicit override exists in memory, its TargetHost is returned.
- If key is an IP address (bare or bracketed) with NO override, the IP itself is returned — an IP is already a concrete endpoint.
- If key is a domain name with NO override, an empty string is returned so the caller uses the original hostname for DNS resolution (which preserves proper TLS certificate verification and MTA-STS compatibility).
func (*Cache) ResolveMX ¶
func (c *Cache) ResolveMX(ctx context.Context, domain string) (records []*net.MX, cacheHit bool, err error)
ResolveMX resolves the MX host for a domain. It first checks the in-memory override store. If an override exists for the domain, it returns a single synthetic MX record pointing to the override target with cacheHit=true. Otherwise it performs a standard MX lookup via the OS resolver and returns cacheHit=false.