redis

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package redis implements pubsub.Broker on Redis (or Valkey, ElastiCache, Memorystore) PUBLISH/SUBSCRIBE.

A Broker holds one dedicated pub/sub connection on which channels are subscribed and unsubscribed dynamically as local subscribers come and go; incoming messages are dispatched to local handlers through per-subscription queues. go-redis health-checks that connection and transparently reconnects and resubscribes after failures (messages published while disconnected are lost, which matches the at-most-once contract). Publishing uses the client's regular connection pool.

Redis pub/sub channels are global to a server (not per logical database), so every topic is prefixed (default "backyard:", or "backyard:<db>:" when the URL selects a database other than 0) to keep deployments that share a server apart.

Index

Constants

View Source
const (
	DefaultSubscribeTimeout = 10 * time.Second
)

Defaults for Options.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

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

Broker is a Redis-backed pubsub.Broker.

func New

func New(ctx context.Context, opts Options) (*Broker, error)

New connects to Redis, verifies connectivity with PING and starts the subscription reader.

func (*Broker) Close

func (b *Broker) Close() error

Close stops all subscriptions, closes the pub/sub connection and, when the broker created the client from a URL, the client. It is idempotent.

func (*Broker) Publish

func (b *Broker) Publish(ctx context.Context, topic string, payload []byte) error

Publish sends payload to every subscriber of topic on all instances.

func (*Broker) Subscribe

func (b *Broker) Subscribe(ctx context.Context, topic string, h pubsub.Handler) (func(), error)

Subscribe registers h for topic. When this is the first local subscriber of topic, Subscribe returns only after Redis confirmed the channel subscription, so a message published after Subscribe returns is delivered.

type Options

type Options struct {
	// URL is a redis:// or rediss:// URL (see redis.ParseURL). Ignored when
	// Client is set.
	URL string
	// Client, if set, is used instead of dialing URL. It is not closed by
	// Broker.Close. Use this to share a client or to connect to a cluster.
	Client goredis.UniversalClient
	// Prefix is prepended to every topic to form the Redis channel name.
	// Default "backyard:" (plus "<db>:" when the URL selects db > 0).
	Prefix string
	// SubscribeTimeout bounds how long Subscribe waits for Redis to confirm a
	// new channel subscription when the caller's context has no earlier
	// deadline (default 10s).
	SubscribeTimeout time.Duration
	// Logger receives operational logs (default slog.Default()).
	Logger *slog.Logger
	// QueueLen and QueueBytes bound the undelivered messages per local
	// subscription (defaults: 10000 messages, 64 MiB).
	QueueLen   int
	QueueBytes int
}

Options configure a Broker.

Jump to

Keyboard shortcuts

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