pubsub

package
v1.11.8 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package pubsub provides development-only in-memory publish/subscribe plus Redis and RabbitMQ adapters. Consumers must document delivery semantics, idempotency, retry, dead-letter, and shutdown behavior.

Index

Constants

This section is empty.

Variables

View Source
var ErrRabbitMQNotConfigured = errors.New("pubsub: RabbitMQ provider is not configured")

ErrRabbitMQNotConfigured indicates an uninitialized RabbitMQ bus.

View Source
var ErrRedisNotConfigured = errors.New("pubsub: Redis provider is not configured")

ErrRedisNotConfigured indicates an uninitialized Redis bus.

Functions

This section is empty.

Types

type Bus

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

Bus represents an in-memory or Redis event message bus.

func New

func New() *Bus

New initializes an event bus instance.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, topic string, payload any) error

Publish broadcasts an event payload onto a named topic.

func (*Bus) Subscribe

func (b *Bus) Subscribe(topic string, handler func(ctx context.Context, payload any))

Subscribe attaches an event handler function to a named topic.

type RabbitMQBus

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

RabbitMQBus publishes confirmed persistent messages and consumes durable queues.

func NewRabbitMQBus

func NewRabbitMQBus(ctx context.Context, config RabbitMQConfig) (*RabbitMQBus, error)

NewRabbitMQBus connects, verifies, and configures a RabbitMQ publisher.

func (*RabbitMQBus) Close

func (bus *RabbitMQBus) Close() error

Close releases the publish channel and connection once.

func (*RabbitMQBus) Publish

func (bus *RabbitMQBus) Publish(
	ctx context.Context, exchange, routingKey string, payload []byte,
) error

Publish sends a bounded persistent message and waits for broker confirmation.

func (*RabbitMQBus) Subscribe

func (bus *RabbitMQBus) Subscribe(
	ctx context.Context,
	queueName, bindingKey string,
	handler func(context.Context, []byte) error,
) error

Subscribe declares a durable queue and blocks until ctx cancellation or an AMQP/handler error. Failed handler messages are negatively acknowledged and requeued.

type RabbitMQConfig

type RabbitMQConfig struct {
	URL               string
	Exchange          string
	ExchangeType      string
	TLS               *tls.Config
	ConnectionTimeout time.Duration
	ConfirmTimeout    time.Duration
	Heartbeat         time.Duration
	MaxPayloadBytes   int
	Prefetch          int
	QuorumQueues      bool
}

RabbitMQConfig defines connection, exchange, delivery, and capacity policy.

type RedisBus

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

RedisBus provides at-most-once distributed notifications. Use the durable Kafka or RabbitMQ adapters when messages must survive subscriber downtime.

func NewRedisBus

func NewRedisBus(ctx context.Context, config RedisConfig) (*RedisBus, error)

NewRedisBus creates and verifies an owned Redis connection.

func NewRedisBusFromClient

func NewRedisBusFromClient(
	ctx context.Context, client redis.UniversalClient, config RedisConfig,
) (*RedisBus, error)

NewRedisBusFromClient verifies and wraps an application-owned Redis client.

func (*RedisBus) Close

func (bus *RedisBus) Close() error

Close releases the Redis client only when the bus owns it.

func (*RedisBus) Publish

func (bus *RedisBus) Publish(ctx context.Context, channel string, payload any) error

Publish sends one bounded JSON notification with at-most-once semantics.

func (*RedisBus) Subscribe

func (bus *RedisBus) Subscribe(ctx context.Context, channel string, handler func(msg any)) error

Subscribe blocks until ctx is cancelled or the Redis subscription fails.

type RedisConfig

type RedisConfig struct {
	URL             string
	ChannelPrefix   string
	MaxPayloadBytes int
	PingTimeout     time.Duration
}

RedisConfig defines connection, channel namespace, and payload limits.

Jump to

Keyboard shortcuts

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