websubredis

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: 8 Imported by: 0

Documentation

Overview

Package websubredis stores hub subscriptions in Redis.

store, err := websubredis.Open(ctx, "redis://localhost:6379/0")
if err != nil {
	return err
}
defer store.Close()

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

This backend suits a hub whose subscription state is worth keeping across a restart but is not worth a relational database, and one running several instances behind a load balancer. Use postgres where the subscription history itself matters.

Key layout

Each subscription is a hash. A set per topic holds its callbacks, so a publish is one SMEMBERS and a pipelined read rather than a scan. A sorted set scored by expiry makes the sweep for dead leases a range query. A set of topics backs Topics.

Redis key expiry is deliberately not used for leases. A hub has to see an expired subscription in order to remove it and report it, and a key that has already evaporated cannot be reported.

Index

Constants

View Source
const DefaultPrefix = "websub"

DefaultPrefix is prepended to every key this package writes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*config)

Option configures a Store.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the key prefix, which defaults to DefaultPrefix. Use it to share a Redis instance with an application's own keys, or to isolate tests from each other.

type Store

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

Store is a websub.SubscriptionStore backed by Redis.

func New

func New(client redis.UniversalClient, opts ...Option) (*Store, error)

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

func Open

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

Open connects using a Redis URL and returns a store that owns the client.

func (*Store) Close

func (s *Store) Close() error

Close releases the client if this store opened it, and does nothing if the client 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, because they are not in the expiry index at all.

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.

The hash, the topic's callback set, the topic set, and the expiry index are updated in one transaction, so a concurrent publish never sees a callback listed for a topic whose subscription has not been written.

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