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
- func Schema(table string) string
- type Option
- type Store
- func (s *Store) Close() error
- func (s *Store) Delete(ctx context.Context, topic, callback string) error
- func (s *Store) Get(ctx context.Context, topic, callback string) (websub.Subscription, error)
- func (s *Store) ListByTopic(ctx context.Context, topic string) ([]websub.Subscription, error)
- func (s *Store) ListExpiring(ctx context.Context, before time.Time) ([]websub.Subscription, error)
- func (s *Store) Save(ctx context.Context, sub websub.Subscription) error
- func (s *Store) Topics(ctx context.Context) ([]string, error)
Constants ¶
const DefaultTable = "websub_subscriptions"
DefaultTable is the table subscriptions live in.
Variables ¶
This section is empty.
Functions ¶
func Schema ¶
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 ¶
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 ¶
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 ¶
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 ¶
Close releases the database if this store opened it, and does nothing if the handle was supplied by the caller.
func (*Store) Delete ¶
Delete removes the subscription for topic and callback. Removing one that does not exist is not an error.
func (*Store) Get ¶
Get returns the subscription for topic and callback, or an error matching websub.ErrUnknownSubscription.
func (*Store) ListByTopic ¶
ListByTopic returns every subscription for topic.
func (*Store) ListExpiring ¶
ListExpiring returns every subscription whose lease elapses strictly before the given time. Permanent subscriptions are never returned.