Documentation
¶
Overview ¶
Package rabbitmq wraps github.com/furdarius/rabbitroutine with the lifecycle contract used across core: typed Config, functional Options, Shutdown methods, and explicit Resource/Runner roles.
The package exposes:
- Connector — low-level dial + reconnect manager (used by both Publisher and ConsumerHost).
- Publisher — Resource. Connects in the constructor; reconnect loop runs as an internal background goroutine and stops in Shutdown.
- ConsumerHost — Runner. Holds a set of consumers and runs their amqp loops under the same connection.
- Consumer[I] — generic per-queue consumer logic (declare + consume + marshal). Attached to a ConsumerHost.
Publisher is a Resource (not a Runner): callers register it for Shutdown only, not for Run. ConsumerHost is the Runner that hosts consumer loops.
Index ¶
- Variables
- type BindQueueConfig
- type BodyMarshaller
- type Config
- type Connector
- type ConnectorInterface
- type ConnectorOption
- func WithAMQPNotifiedListener(fn func(rabbitroutine.AMQPNotified)) ConnectorOption
- func WithDialedListener(fn func(rabbitroutine.Dialed)) ConnectorOption
- func WithReconnectAttempts(n uint) ConnectorOption
- func WithReconnectWait(d time.Duration) ConnectorOption
- func WithRetriedListener(fn func(rabbitroutine.Retried)) ConnectorOption
- type ConsumeOpts
- type Consumer
- type ConsumerConfig
- type ConsumerHost
- type ConsumerOption
- func WithBindQueue(c BindQueueConfig) ConsumerOption
- func WithConsumeOpts(c ConsumeOpts) ConsumerOption
- func WithConsumerConfig(c ConsumerConfig) ConsumerOption
- func WithConsumerLogger(l *slog.Logger) ConsumerOption
- func WithExchange(c ExchangeConfig) ConsumerOption
- func WithQueue(c QueueConfig) ConsumerOption
- func WithWorkerCount(count int) ConsumerOption
- type ExchangeConfig
- type ExchangeKind
- type HostOption
- type Message
- type Processor
- type PublishOpts
- type Publisher
- type PublisherOption
- func WithPublishLinearDelay(d time.Duration) PublisherOption
- func WithPublishMaxAttempts(n uint) PublisherOption
- func WithPublisherConnector(c ConnectorInterface) PublisherOption
- func WithPublisherConnectorOptions(opts ...ConnectorOption) PublisherOption
- func WithPublisherLogger(l *slog.Logger) PublisherOption
- type QueueConfig
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedMessage = errors.New("unsupported message type")
ErrUnsupportedMessage is returned by the default body marshaller when the AMQP delivery's ContentType is not "application/json" and no custom marshaller was provided.
Functions ¶
This section is empty.
Types ¶
type BindQueueConfig ¶
BindQueueConfig governs ch.QueueBind.
type BodyMarshaller ¶
BodyMarshaller decodes a raw AMQP delivery body into a typed payload.
type Config ¶
Config describes a RabbitMQ connection. Plain fields, no struct tags — consumer apps map their viper keys to fields explicitly inside their own config.NewConfig().
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector wraps a rabbitroutine.Connector with reconnect configuration and listener wiring. Created via NewConnector or implicitly inside NewPublisher / NewConsumerHost.
func NewConnector ¶ added in v1.3.0
func NewConnector(cfg Config, opts ...ConnectorOption) *Connector
NewConnector builds a Connector from the given Config and options. Does NOT dial — use Connect (or have NewPublisher / NewConsumerHost call it for you).
func (*Connector) Connect ¶
Connect dials the broker. Subsequent reconnects are managed internally. Blocks until the first connection is established or ctx expires.
func (*Connector) Inner ¶ added in v1.3.0
func (c *Connector) Inner() *rabbitroutine.Connector
Inner returns the underlying *rabbitroutine.Connector. Use for advanced listener wiring or to integrate with code that expects the raw type.
func (*Connector) StartConsumer ¶
StartConsumer attaches a rabbitroutine.Consumer and runs its loop until ctx is cancelled. Typically called by ConsumerHost.Run, not directly.
type ConnectorInterface ¶ added in v1.3.0
type ConnectorInterface interface {
// Connect blocks until the broker is reachable and the connection is
// established. Subsequent reconnects are handled internally by
// rabbitroutine.
Connect(ctx context.Context) error
// Inner returns the underlying *rabbitroutine.Connector for advanced
// use (custom listeners, channel pools).
Inner() *rabbitroutine.Connector
// StartConsumer attaches a rabbitroutine.Consumer to the connector and
// runs its loop until ctx is cancelled.
StartConsumer(ctx context.Context, consumer rabbitroutine.Consumer) error
}
ConnectorInterface is the surface used by Publisher and ConsumerHost to drive the underlying rabbitroutine.Connector. Mainly an extension seam: tests can substitute fakes; production code uses *Connector.
The concrete struct is Connector; the interface is ConnectorInterface to keep call sites readable and avoid name collision.
type ConnectorOption ¶ added in v1.3.0
type ConnectorOption func(*connectorOptions)
ConnectorOption configures a Connector.
func WithAMQPNotifiedListener ¶
func WithAMQPNotifiedListener(fn func(rabbitroutine.AMQPNotified)) ConnectorOption
WithAMQPNotifiedListener attaches a callback fired when the broker sends an AMQP notification (channel close, blocked, etc).
func WithDialedListener ¶
func WithDialedListener(fn func(rabbitroutine.Dialed)) ConnectorOption
WithDialedListener attaches a callback fired on every successful dial.
func WithReconnectAttempts ¶ added in v1.3.0
func WithReconnectAttempts(n uint) ConnectorOption
WithReconnectAttempts overrides how many reconnect attempts the underlying rabbitroutine.Connector makes before giving up. Default: 3.
func WithReconnectWait ¶ added in v1.3.0
func WithReconnectWait(d time.Duration) ConnectorOption
WithReconnectWait overrides the delay between reconnect attempts. Default: 2 seconds.
func WithRetriedListener ¶
func WithRetriedListener(fn func(rabbitroutine.Retried)) ConnectorOption
WithRetriedListener attaches a callback fired on every reconnect retry. Useful for surfacing reconnect activity in logs or metrics.
type ConsumeOpts ¶
ConsumeOpts governs ch.Qos prefetch settings.
type Consumer ¶
type Consumer[I any] struct { // contains filtered or unexported fields }
Consumer is a per-queue consumer with declare-and-consume logic. Attach to a ConsumerHost via host.AddConsumer(consumer).
func NewConsumer ¶
func NewConsumer[I any]( queue string, processor Processor[I], opts ...ConsumerOption, ) *Consumer[I]
NewConsumer creates a Consumer for the given queue and processor. By default the delivery body is JSON-unmarshalled into the payload type I; call SetMarshaller to override.
All ConsumerOption values configure declare/consume behavior.
func (*Consumer[I]) Consume ¶
Consume sets QoS, opens the consume stream, and processes deliveries until ctx is cancelled. Called by rabbitroutine.
Concurrency: a bounded worker pool sized by WithWorkerCount (default 1) pulls from the delivery channel. On ctx cancellation or channel close, Consume stops distributing new work and waits for all in-flight workers to finish their current Ack/Nack before returning. This prevents Ack-after-close panics and gives in-flight messages a chance to settle.
func (*Consumer[I]) Declare ¶
Declare runs ExchangeDeclare / QueueDeclare / QueueBind on the channel as dictated by the configured options. Called by rabbitroutine on every successful (re)connect.
func (*Consumer[I]) SetMarshaller ¶ added in v1.3.0
func (c *Consumer[I]) SetMarshaller(m BodyMarshaller[I])
SetMarshaller installs a custom body marshaller. Passing nil resets to the default JSON unmarshaller.
Must be called before the consumer starts processing deliveries (i.e., before the hosting ConsumerHost's Run).
type ConsumerConfig ¶
ConsumerConfig governs ch.Consume.
type ConsumerHost ¶ added in v1.3.0
type ConsumerHost struct {
// contains filtered or unexported fields
}
ConsumerHost is a Runner that owns a connection and runs a set of rabbitroutine.Consumer instances under it.
Implements lifecycle.Runner.
func NewConsumerHost ¶ added in v1.3.0
func NewConsumerHost(cfg Config, opts ...HostOption) *ConsumerHost
NewConsumerHost constructs a ConsumerHost. Use AddConsumer to register consumers before calling Run (typically via app.App).
func (*ConsumerHost) AddConsumer ¶ added in v1.3.0
func (h *ConsumerHost) AddConsumer(consumer rabbitroutine.Consumer)
AddConsumer registers a rabbitroutine.Consumer to be started when Run is called. Must be called before Run.
type ConsumerOption ¶ added in v1.3.0
type ConsumerOption func(*consumerBase)
ConsumerOption configures a Consumer. Non-generic so option sites don't need to repeat the payload type parameter.
rabbitmq.NewConsumer[OrderEvent]("orders", processor,
rabbitmq.WithExchange(exchangeCfg),
rabbitmq.WithQueue(queueCfg),
)
To install a custom body marshaller (which IS type-dependent), use Consumer.SetMarshaller after construction.
func WithBindQueue ¶
func WithBindQueue(c BindQueueConfig) ConsumerOption
WithBindQueue instructs Declare to bind the queue to the exchange.
func WithConsumeOpts ¶
func WithConsumeOpts(c ConsumeOpts) ConsumerOption
WithConsumeOpts overrides QoS prefetch settings.
func WithConsumerConfig ¶
func WithConsumerConfig(c ConsumerConfig) ConsumerOption
WithConsumerConfig overrides the consume-call settings (AutoAck, Exclusive, etc.).
func WithConsumerLogger ¶ added in v1.3.0
func WithConsumerLogger(l *slog.Logger) ConsumerOption
WithConsumerLogger attaches a *slog.Logger used for per-message error logging (marshal failures, nack/ack errors). Defaults to slog.Default() when omitted.
func WithExchange ¶
func WithExchange(c ExchangeConfig) ConsumerOption
WithExchange instructs Declare to create an exchange.
func WithQueue ¶
func WithQueue(c QueueConfig) ConsumerOption
WithQueue instructs Declare to create the queue.
func WithWorkerCount ¶ added in v1.3.0
func WithWorkerCount(count int) ConsumerOption
WithWorkerCount sets the number of concurrent goroutines processing deliveries for this consumer. Default: 1 (ordered processing — matches the default PrefetchCount=1).
Raising this above 1 gives higher throughput at the cost of losing message order within the queue. Keep ≤ PrefetchCount (ConsumeOpts) or the extra workers starve.
Any value < 1 is normalized to 1.
type ExchangeConfig ¶
type ExchangeConfig struct {
Name string
Kind ExchangeKind
Durable bool
AutoDelete bool
Internal bool
NoWait bool
Args amqp.Table
}
ExchangeConfig governs ch.ExchangeDeclare.
type ExchangeKind ¶
type ExchangeKind string
ExchangeKind enumerates the AMQP exchange types supported by Consumer.
const ( ExchangeKindDirect ExchangeKind = "direct" ExchangeKindFanout ExchangeKind = "fanout" ExchangeKindHeaders ExchangeKind = "headers" ExchangeKindTopic ExchangeKind = "topic" )
type HostOption ¶ added in v1.3.0
type HostOption func(*hostOptions)
HostOption configures a ConsumerHost.
func WithHostConnector ¶ added in v1.3.0
func WithHostConnector(c ConnectorInterface) HostOption
WithHostConnector lets the caller inject a pre-built connector (e.g., shared with a Publisher).
func WithHostConnectorOptions ¶ added in v1.3.0
func WithHostConnectorOptions(opts ...ConnectorOption) HostOption
WithHostConnectorOptions threads ConnectorOption values into the internally-created *Connector when WithHostConnector is not used.
func WithHostLogger ¶ added in v1.3.0
func WithHostLogger(l *slog.Logger) HostOption
WithHostLogger attaches a *slog.Logger used for lifecycle events.
type Message ¶
type Message[I any] struct { Payload I }
Message is the typed payload delivered to a Processor.
type Processor ¶
type Processor[I any] interface { Process(ctx context.Context, msg Message[I]) error GetName() string }
Processor is what callers implement to handle messages off a queue. The returned error decides whether the underlying delivery is Acked (nil error) or Nacked (non-nil error).
GetName is used as the AMQP consumer tag and shows up in logs / broker admin tools.
type PublishOpts ¶
type PublishOpts struct {
ContentType string
MessageID string
AppID string
UserID string
Priority uint8
Type string
}
PublishOpts captures per-publish AMQP message metadata. Body is always JSON-encoded from the message argument; the fields here populate the envelope only.
type Publisher ¶
type Publisher struct {
// contains filtered or unexported fields
}
Publisher publishes JSON messages to RabbitMQ exchanges with automatic retry/reconnect via rabbitroutine.RetryPublisher.
Publisher is a Resource (not a Runner): the connection's reconnect loop is an internal goroutine started in the constructor and stopped in Shutdown.
Implements lifecycle.Resource.
func NewPublisher ¶ added in v1.3.0
NewPublisher constructs a Publisher and dials the broker. Returns once the connection is established (or fails).
Lifetime of the reconnect loop:
- The ctx passed here bounds only the INITIAL dial. Once NewPublisher returns successfully, the internal reconnect loop is intentionally detached from that ctx (rooted in context.Background) — otherwise a short-lived startup ctx would kill reconnection during normal operation.
- To stop the reconnect loop, call Shutdown. This is the ONLY correct way to terminate the Publisher; relying on external ctx cancellation will not stop the loop.
If no connector is injected via WithPublisherConnector, an internal *Connector is built from cfg + WithPublisherConnectorOptions.
func (*Publisher) Publish ¶
func (p *Publisher) Publish( ctx context.Context, exchange, routingKey string, message any, opts PublishOpts, ) error
Publish JSON-encodes message and publishes it to exchange with routingKey and the metadata in opts. Wraps rabbitroutine.RetryPublisher.Publish, so transient failures retry up to WithPublishMaxAttempts.
type PublisherOption ¶ added in v1.3.0
type PublisherOption func(*publisherOptions)
PublisherOption configures a Publisher.
func WithPublishLinearDelay ¶ added in v1.3.0
func WithPublishLinearDelay(d time.Duration) PublisherOption
WithPublishLinearDelay overrides the linear retry delay between publish attempts. Default: 10ms.
func WithPublishMaxAttempts ¶ added in v1.3.0
func WithPublishMaxAttempts(n uint) PublisherOption
WithPublishMaxAttempts overrides rabbitroutine.PublishMaxAttemptsSetup. Default: 16.
func WithPublisherConnector ¶ added in v1.3.0
func WithPublisherConnector(c ConnectorInterface) PublisherOption
WithPublisherConnector lets callers inject a pre-built ConnectorInterface (e.g., shared between Publisher and ConsumerHost, or a fake in tests). When omitted, NewPublisher constructs its own *Connector from the Config.
func WithPublisherConnectorOptions ¶ added in v1.3.0
func WithPublisherConnectorOptions(opts ...ConnectorOption) PublisherOption
WithPublisherConnectorOptions threads ConnectorOption values into the internally-created *Connector when WithPublisherConnector is not used. Ignored when an external connector is supplied.
func WithPublisherLogger ¶ added in v1.3.0
func WithPublisherLogger(l *slog.Logger) PublisherOption
WithPublisherLogger attaches a *slog.Logger used for lifecycle events. Defaults to slog.Default() when omitted.