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