Documentation
¶
Overview ¶
Package pubsub provides a simple publish-subscribe messaging system.
The package defines low-level interfaces for publishing and subscribing to topics with []byte payloads. It's designed to be a dumb transport layer - users should build their own adapters for type safety, filtering, and business logic.
Two implementations are provided:
- InMemory: Channel-based, single-process pub/sub
- Postgres: LISTEN/NOTIFY-based, multi-process pub/sub
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed is returned when operations are attempted on a closed broker. ErrClosed = errors.New("pubsub: broker is closed") )
Common errors.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker interface {
Publisher
Subscriber
}
Broker combines Publisher and Subscriber interfaces. Most implementations provide both capabilities.
type InMemory ¶
type InMemory struct {
// contains filtered or unexported fields
}
InMemory is a simple in-memory broker using Go channels. It's suitable for single-process applications, testing, and development. Messages are not persisted and are lost if no subscribers are active.
func (*InMemory) Publish ¶
Publish sends a message to all subscribers of the topic. If no subscribers exist, the message is dropped (fire-and-forget). Each subscriber's handler is called in its own goroutine.
type Postgres ¶
type Postgres struct {
// contains filtered or unexported fields
}
Postgres is a broker that uses PostgreSQL's LISTEN/NOTIFY for pub/sub. It's suitable for multi-process applications where events need to be shared across different instances or services connected to the same database.
Unlike InMemory, Postgres can distribute messages across multiple processes, but it still provides no durability - messages are lost if no subscribers are listening.
func NewPostgres ¶
NewPostgres creates a new Postgres broker using the provided connection pool. The pool must remain open for the lifetime of the broker.
func (*Postgres) Publish ¶
Publish sends a message to all subscribers of the topic across all processes. It uses PostgreSQL's NOTIFY command. The payload is sent as the notification payload.
type Publisher ¶
type Publisher interface {
// Publish sends a message to the specified topic.
// The payload is delivered to all active subscribers.
// Publishing is fire-and-forget - if no subscribers exist, the message is dropped.
Publish(ctx context.Context, topic string, payload []byte) error
// Close releases any resources held by the publisher.
Close() error
}
Publisher publishes messages to topics.
type Subscriber ¶
type Subscriber interface {
// Subscribe registers a handler for the specified topic.
// The handler is called asynchronously for each message published to the topic.
// Multiple subscribers to the same topic each receive a copy of every message.
//
// The subscription remains active until the context is canceled or Close is called.
// Handlers should be fast and non-blocking. For slow operations, handlers should
// spawn goroutines or use channels to bridge to synchronous code.
Subscribe(ctx context.Context, topic string, handler func([]byte)) error
// Close releases any resources held by the subscriber and stops all handlers.
Close() error
}
Subscriber subscribes to topics and receives messages via handlers.