dbcache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MPL-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DbCache

type DbCache struct {
	Conf   config.Config
	Ctx    context.Context
	Db     *sql.DB
	Logger *zap.Logger
	Mu     sync.Mutex

	// OnReload, if set, runs at the end of every Reload against the
	// freshly-built in-memory DB while the cache lock is still held —
	// so no request ever observes the snapshot before the overlay is
	// applied. Used by chassis/sysops to re-apply the trusted system
	// opstacks (Reload rebuilds :memory: from the runtime.db dump and
	// would otherwise drop them). A hook error is logged, not fatal:
	// the previous snapshot stays live rather than going dark.
	OnReload func(*sql.DB) error
}

DbCache structure

func New

func New(conf config.Config, logger *zap.Logger, ctx context.Context) (*DbCache, error)

New Create a new in-memory DB cache.

Critical: go-sqlite3 gives each *connection* in the pool its own `:memory:` database. So if connection #1 loads the schema and a later concurrent query opens connection #2, that second connection sees an empty DB and the query fails with "no such table: ops".

To avoid that, pin the in-memory cache to a single connection. Reads are fast and the cache is read-only on the hot path; serializing through one connection costs nothing visible but guarantees consistency under concurrent load.

func (*DbCache) Reload

func (dbc *DbCache) Reload() error

Reload a db file into Memory. Sources from the runtime DB only — the auth DB (when present) is owned exclusively by the admin role and is never mirrored into the read cache.

func (*DbCache) Snapshot

func (dbc *DbCache) Snapshot() *sql.DB

Snapshot returns the current mirror handle under the lock. Callers that live longer than one reload (e.g. the ingress resolver) MUST call this per use rather than capturing dbc.Db once: Reload() swaps dbc.Db to a fresh *sql.DB, so a captured handle goes stale and never sees rows written after it was captured. The returned *sql.DB stays valid for the caller's immediate query (the old handle isn't closed on swap); at worst it's one reload-cycle stale, which is the same guarantee the rest of the read path has.

func (*DbCache) Watch

func (dbc *DbCache) Watch()

Watch a db file for changes

Jump to

Keyboard shortcuts

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