Help us shape the future of Go! Take the 2026 Go Developer Survey

sqlite

package module
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package sqlite is the default on-disk agent.Store: a single-file, zero-cgo SQLite journal (via modernc.org/sqlite). An agent run journaled here survives a process crash and resumes without re-running completed steps. No cluster, no daemon: one binary and one file.

Store implements agent.Store, agent.Lister (so agent.Recover finds the runs to re-drive after a restart) and agent.Leaser (so processes sharing the file drive each run one at a time). Open opens three connection pools on the file: a writer of one connection for every Insert and Get, readers for Load and Runs, and a lease connection of its own, whose statements use a short busy timeout so a writer holding the file's lock never delays a lease renewal past its cutoff.

The tables are named with a prefix, "bide_" by default (see WithTablePrefix): bide_steps holds the journal, bide_leases the leases, and bide_schema_version the schema version. Open refuses a file whose schema version is newer than this version knows. The file must be on a local disk: SQLite's locking does not work over NFS. A forward jump of the system clock expires leases early, which the journal's attempt claims keep safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v0.9.0

type Option interface {
	// contains filtered or unexported methods
}

Option configures Open and New.

func WithTablePrefix added in v0.9.0

func WithTablePrefix(prefix string) Option

WithTablePrefix names the tables with prefix instead of "bide_": prefix+"steps", prefix+"leases" and prefix+"schema_version". It must be an SQL identifier (letters, digits and underscores, not starting with a digit), or Open and New return an ErrConfig error.

type Store

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

Store is a SQLite-backed agent.Store.

func New added in v0.9.0

func New(ctx context.Context, db *sql.DB, opts ...Option) (*Store, error)

New returns a Store over db, which the caller opened and closes. db must be in WAL mode and allow at least two open connections (a Load's page and a write inside its loop may each need one); New returns an ErrConfig error otherwise. Its connections should set a busy timeout of several seconds (modernc.org/sqlite: _pragma=busy_timeout(30000) in the DSN), since a write that gives up on another process's lock fails a step whose side effect may have run. All three roles share db, so a lease statement can wait behind the caller's other work; Open avoids that.

func Open

func Open(path string, opts ...Option) (*Store, error)

Open opens (creating if needed) a SQLite journal at path, in WAL mode, with its three connection pools. ":memory:" opens an ephemeral in-memory store for tests, on one connection shared by all three roles (an in-memory database lives on one connection).

func (*Store) AcquireLease added in v0.9.0

func (s *Store) AcquireLease(ctx context.Context, runID, holder string, ttl time.Duration) (bool, error)

AcquireLease implements agent.Leaser with one upsert: it grants the lease when the run is unleased, already held by holder (a renewal), or the current lease has expired.

func (*Store) Close

func (s *Store) Close() error

Close closes the connection pools Open opened. A Store made with New leaves its db open.

func (*Store) Get added in v0.9.0

func (s *Store) Get(ctx context.Context, runID, name string) (agent.Entry, bool, error)

Get implements agent.Store. It reads on the writer's connection: the journal reads a step just before it writes one, and a point read on another connection of the same file costs about as much again as the write, which the reader pool is for bulk reads (Load, Runs) to avoid.

func (*Store) Insert added in v0.9.0

func (s *Store) Insert(ctx context.Context, runID, name string, data []byte) (agent.Entry, bool, error)

Insert implements agent.Store. An entry's Seq is its row id, which SQLite assigns as one more than the table's highest when the row is inserted, under the database's single write lock: so it increases in commit order, within a run and across the table. The entry it returns for a new row holds data itself (SQLite copied it).

func (*Store) Load added in v0.9.0

func (s *Store) Load(ctx context.Context, runID string, after int64) iter.Seq2[agent.Entry, error]

Load implements agent.Store. It reads pageSize entries per query and yields them with no connection held, so the caller may write to the store inside its loop.

func (*Store) ReapLeases added in v0.10.0

func (s *Store) ReapLeases(ctx context.Context, ended []string) (int, error)

ReapLeases implements agent.Leaser with one DELETE, which checks the expiry in the same statement, so a lease taken or renewed meanwhile is kept. The lapsed leases it deletes are those no recovery pass takes over: a finished run's, one on a run the steps table does not hold, and one on a run whose ID contains '>' (a session's or a sub-agent's run).

func (*Store) ReleaseLease added in v0.9.0

func (s *Store) ReleaseLease(ctx context.Context, runID, holder string) error

ReleaseLease implements agent.Leaser: it deletes runID's lease if holder holds it.

func (*Store) RenewLease added in v0.9.0

func (s *Store) RenewLease(ctx context.Context, runID, holder string, ttl time.Duration) (bool, error)

RenewLease implements agent.Leaser: it extends holder's still-live lease on runID.

func (*Store) Runs

func (s *Store) Runs(ctx context.Context, f agent.RunFilter) iter.Seq2[string, error]

Runs implements agent.Lister. The filter is evaluated in the query, runsPage run IDs at a time, with no connection held between pages.

Jump to

Keyboard shortcuts

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