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 ¶
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 ¶
New connects to Redis, verifies connectivity with PING and starts the subscription reader.
func (*Broker) Close ¶
Close stops all subscriptions, closes the pub/sub connection and, when the broker created the client from a URL, the client. It is idempotent.
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.