sqlx

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sqlx holds the transaction helpers the rag packages share: running a transaction again, whole, when it loses against a concurrent writer.

The package does not know which SQLite driver the caller uses, so the test for "this error means the writer lost a race" is a parameter. Callers that own a precise classifier (Pando's internal/db.IsRetryable understands the multiwriter engine's error types) pass it in; everyone else gets DefaultRetryable, which matches the error text every SQLite driver produces.

Index

Constants

View Source
const RetryBudget = 30 * time.Second

RetryBudget bounds how long one operation keeps retrying a lost commit before the error is returned to the caller.

Variables

This section is empty.

Functions

func DefaultRetryable

func DefaultRetryable(err error) bool

DefaultRetryable recognises SQLITE_BUSY, SQLITE_LOCKED (including the extended BUSY_SNAPSHOT code) and "database is locked" by their error text, which every Go SQLite driver includes in its messages.

func RetryLoop

func RetryLoop(ctx context.Context, retryable Retryable, fn func() error) error

RetryLoop runs fn until it succeeds, fails with an error retryable does not accept, the context ends or RetryBudget is spent. The backoff starts in the microseconds: a lost commit retried against a fresh snapshot usually wins at once. A nil retryable means DefaultRetryable.

func RunTx

func RunTx(ctx context.Context, conn TxBeginner, retryable Retryable, fn func(*sql.Tx) error) error

RunTx runs fn inside a transaction and commits it. When the commit (or any statement) loses against a concurrent writer, the transaction is rolled back and fn runs again, whole, on a fresh snapshot.

fn may therefore run more than once: it must only act through tx and must not have effects outside the transaction (send events, mutate shared state) before RunTx returns. Compute expensive inputs such as embeddings before calling RunTx.

func RunTxOpts

func RunTxOpts(ctx context.Context, conn TxBeginner, opts *sql.TxOptions, retryable Retryable, fn func(*sql.Tx) error) error

RunTxOpts is RunTx with explicit transaction options.

Types

type Retryable

type Retryable func(err error) bool

Retryable reports whether err means the operation lost against a concurrent writer and may be run again whole.

type TxBeginner

type TxBeginner interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

TxBeginner is satisfied by *sql.DB (and by test doubles that open real transactions).

Jump to

Keyboard shortcuts

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