websubsqlite

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package websubsqlite stores hub subscriptions in SQLite.

It uses modernc.org/sqlite, a pure Go driver, so it cross compiles and needs no C toolchain. Builds with CGO_ENABLED=0 work.

store, err := websubsqlite.Open(ctx, "file:websub.db")
if err != nil {
	return err
}
defer store.Close()

hub, err := websub.NewHub("https://example.com/hub", websub.WithStore(store))

This backend suits single binary and embedded deployments. SQLite takes one writer at a time, so a hub with heavy subscription churn will find that a bottleneck well before anything else; use postgres there.

Index

Constants

View Source
const DefaultTable = "websub_subscriptions"

DefaultTable is the table subscriptions live in.

Variables

This section is empty.

Functions

func Schema

func Schema(table string) string

Schema returns the statements this backend needs, for callers managing migrations themselves.

Timestamps and durations are stored as integers rather than as SQLite's text dates, because a Go time.Duration is nanoseconds and a store must not quietly round what it was handed. The protocol only ever carries whole seconds, but nothing stops a caller setting a finer lease.

Types

type Option

type Option func(*config)

Option configures a Store.

func WithTable

func WithTable(name string) Option

WithTable sets the table name, which defaults to DefaultTable. Useful for sharing a database with an application's own tables, or for isolating tests from each other.

The name is interpolated into SQL, since a table name cannot be a bound parameter, so it is validated as an identifier and rejected otherwise.

func WithoutSchema

func WithoutSchema() Option

WithoutSchema skips creating the table and index. Use it when migrations are managed elsewhere. See Schema for the statements to run.

type Store

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

Store is a websub.SubscriptionStore backed by SQLite.

func New

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

New returns a store using an existing database handle, which it does not close.

The handle is used as given. See Open for the pragmas this backend expects, in particular write-ahead logging and a busy timeout, which a caller opening their own handle has to set themselves.

func Open

func Open(ctx context.Context, dsn string, opts ...Option) (*Store, error)

Open opens the database at dsn and returns a store.

Unless the dsn already sets them, Open turns on write-ahead logging and a busy timeout. Both are effectively required: SQLite takes one writer at a time, and a hub verifying several subscriptions at once will otherwise get SQLITE_BUSY rather than waiting its turn. WAL also stops readers blocking the writer, which matters because a hub reads on every publish.

`:memory:` gives an in-memory database, which is useful in tests, though each connection in the pool gets its own; use `file::memory:?cache=shared` or a temporary file instead.

The returned store owns the database and closes it on Store.Close.

func (*Store) Close

func (s *Store) Close() error

Close releases the database if this store opened it, and does nothing if the handle was supplied by the caller.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, topic, callback string) error

Delete removes the subscription for topic and callback. Removing one that does not exist is not an error.

func (*Store) Get

func (s *Store) Get(ctx context.Context, topic, callback string) (websub.Subscription, error)

Get returns the subscription for topic and callback, or an error matching websub.ErrUnknownSubscription.

func (*Store) ListByTopic

func (s *Store) ListByTopic(ctx context.Context, topic string) ([]websub.Subscription, error)

ListByTopic returns every subscription for topic.

func (*Store) ListExpiring

func (s *Store) ListExpiring(ctx context.Context, before time.Time) ([]websub.Subscription, error)

ListExpiring returns every subscription whose lease elapses strictly before the given time. Permanent subscriptions are never returned.

func (*Store) Save

func (s *Store) Save(ctx context.Context, sub websub.Subscription) error

Save writes sub, replacing any existing entry for the same topic and callback.

func (*Store) Topics

func (s *Store) Topics(ctx context.Context) ([]string, error)

Topics returns every topic with at least one subscription.

Jump to

Keyboard shortcuts

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