sqlite

package
v0.0.5-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package sqlite is a durable, single-node queue backend built on database/sql + SQLite. Messages survive process restart. Delivery is at-least-once: a subscriber claims a message, and only DELETEs it after the handler returns nil.

Consumers must blank-import their SQLite driver (typically modernc.org/sqlite). This package uses only database/sql.

The schema lives in schema.sql (embedded). Call Init once on a fresh database to create the tables.

SQLite pool notes

SQLite has two footguns that hex/queue/sqlite consumers should know about:

  • :memory: databases are per-connection. If the *sql.DB pool holds more than one connection, Init runs against one and Publish/Subscribe may land on another that has no schema. Use MaxOpenConns: 1 with :memory:.
  • File-backed databases with concurrent writers hit SQLITE_BUSY unless WAL mode and a busy_timeout are enabled. Configure the DSN accordingly: "file:queue.db?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(ctx context.Context, db *sql.DB) error

Init runs the schema DDL. Safe to call more than once (uses IF NOT EXISTS). Consumers that also use hex/db/sqlite for their own migrations can skip this and include schema.sql content in their migration set.

Types

type Options

type Options struct {
	// PollInterval is how often subscribers scan for new messages.
	// Defaults to 200ms. Tune down for lower latency, up for lower load.
	PollInterval time.Duration

	// MaxRetries caps redelivery attempts. Zero means unlimited. Callers
	// wanting DLQ should use the hex/queue/jobs layer.
	MaxRetries int

	// RetryDelay is the wait before a failed message is redelivered.
	// Zero means immediate.
	RetryDelay time.Duration

	// VisibilityTimeout is how long a claimed message stays hidden from
	// other consumers before being made visible again (assumed
	// crashed). Defaults to 30 seconds.
	VisibilityTimeout time.Duration
}

Options tune a sqlite Queue.

type Queue

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

Queue is the sqlite-backed implementation of queue.Queue.

func New

func New(db *sql.DB, opts Options) *Queue

New returns a sqlite Queue backed by db. The database must already contain the queue schema (call Init).

func (*Queue) Close

func (q *Queue) Close(ctx context.Context) error

Close stops all subscribers and closes the underlying DB is left to the caller (hex/db semantics — caller owns *sql.DB).

func (*Queue) Publish

func (q *Queue) Publish(ctx context.Context, topic string, body []byte, opts ...queue.PublishOptions) (string, error)

Publish inserts a message.

func (*Queue) Subscribe

func (q *Queue) Subscribe(ctx context.Context, topic string, handler queue.Handler) (queue.Subscription, error)

Subscribe starts a poller that dispatches messages on topic to handler.

Jump to

Keyboard shortcuts

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