rabbitmq

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 19 Imported by: 0

README

messaging-rabbitmq

RabbitMQ backend for messaging: quorum queues over AMQP 0-9-1, with the management API as the source of every queue fact.

Go Reference Pipeline coverage

Part of the phpboyscout Go toolkit. This is a provider module of messaging, and its documentation lives with the core: messaging.go.phpboyscout.uk. Its decisions are spec 0009.

One backend

New
wire AMQP 0-9-1 through rabbitmq/amqp091-go; the HTTP management API for what the wire cannot say
delivery modes AtMostOnce, AtLeastOnce
at-least-once an operator-created quorum queue named by DurableName, bound to the operator's topic exchange with the pattern; verified through management at attach
at-most-once a transient queue per source the backend makes and the broker deletes, acknowledged as each message is handed over
survives a restart yes: quorum queues are durable and every publish is persistent
attempt x-delivery-count plus one, the broker's count of failed deliveries
lease the consumer's own timer; the broker's consumer_timeout is nothing it relies on
patterns yes, in the topic grammar (> becomes #), verified against the queue's bindings

Operating it

The exchange, every at-least-once queue, its bindings and its dead-letter configuration are the operator's; the backend resolves and verifies them and creates nothing durable. A queue must be quorum, must have a dead-letter exchange with dead-letter-strategy: at-least-once and overflow: reject-publish, and must have an effective delivery-limit of at least MaxDeliver - 1, because the broker counts failed deliveries and the adapter counts attempts. Management must be enabled and reachable, or Open fails naming the endpoint.

A publish is never deduplicated. RabbitMQ has no publisher-side identity, so a publish retried after an ambiguous failure — a timeout, or a connection lost between the broker writing the message and its confirm arriving — is stored twice and delivered twice. Consumers here must be idempotent. JetStream and SQS FIFO deduplicate on the event's identity and would otherwise have hidden that.

Choosing a wire format

Settings.Format writes every event this backend publishes. Unset, the writer is CloudEvents binary with the ce- prefix in the headers table and the payload's type on the message's content_type property, which is what this module has always written. No CloudEvents binding covers AMQP 0-9-1, so the prefix follows HTTP's.

Settings.Reads is the chain a received message is read with. Exactly one format must claim a message. Unset, the chain is binary and then structured, so a consumer can read structured before any producer writes it. To move a deployment to structured, upgrade the readers first and switch the writers after. In structured mode, content_type is application/cloudevents+json and the attributes travel in the body.

rabbitmq.New(rabbitmq.Settings{URL: url, Exchange: "events", Format: cloudevents.Structured()})
  • String entries no format takes, such as a tracing header, reach a handler through Delivery.Foreign(). They are never sent on. The broker's own entries that are not strings, such as x-delivery-count and x-death, are not carried.
  • A message no format can read never reaches a handler. That covers a message no format claims, one two formats both claim (the reader refuses to guess between them), and one a format claims but cannot decode. It's logged and counted in Unreadable. Under AtLeastOnce it is rejected at once, back to the queue while it has attempts left and to the dead-letter exchange on its last. Under AtMostOnce the broker acknowledged it at delivery, and it is dropped (spec 0015).

To exchange messages with .NET services, the MassTransit and NServiceBus formats work the same way, as a writer, in Reads beside the defaults, or both. The suite runs under each:

rabbitmq.New(rabbitmq.Settings{
	URL: url, Exchange: "events",
	Format: masstransit.MassTransit(),
	Reads:  []envelope.Format{cloudevents.Binary(cloudevents.HTTP), cloudevents.Structured(), masstransit.MassTransit()},
})

MassTransit writes application/vnd.masstransit+json on content_type and refuses at publish an ID that is not a UUID and a payload that is not JSON. NServiceBus writes its own headers into the table and the payload's type on content_type. Each format's rules are on its package: go/envelope-masstransit and go/envelope-nservicebus.

The CloudEvents formats are go/envelope-cloudevents's, the event is go/envelope's, and the decisions are spec 0014.

Testing

The unit gate needs nothing. The conformance rows run against rabbitmq:4.3-management in a container under ./test/integration, gated by INT_TEST_INTEGRATION=1 (just test-integration), in CI's Docker-in-Docker integration job; the fixture restarts the same container to prove survival. The suite runs once per format, MassTransit and NServiceBus included, each on its own exchange and queues.

Status

Pre-1.0, tracking the core. The backend is built and green under conformance.RunDurable against RabbitMQ 4.3.5 in a container, restart included.

Documentation

Overview

Package rabbitmq implements the backend contract of gitlab.com/phpboyscout/go/messaging over RabbitMQ: AMQP 0-9-1 for the wire, through rabbitmq/amqp091-go, and the HTTP management API for every fact about a queue the wire cannot return.

At-least-once consumes an operator-created quorum queue named by the durable name, bound to the operator's topic exchange with the subscription's pattern; the backend verifies the queue's type, its effective delivery limit, its dead-letter configuration and its bindings through management, and refuses naming what it found. At-most-once builds its own transient topology per source and acknowledges each message as it hands it over. The attempt is the broker's x-delivery-count plus one; the lease is the consumer's own; a publish is confirmed, persistent and mandatory.

A publish is never deduplicated. RabbitMQ has no publisher-side identity, so a publish retried after an ambiguous failure — a timeout, a lost connection between the broker writing the message and the confirm arriving — is stored twice and delivered twice. Consumers on this backend must be idempotent, which JetStream and SQS FIFO would otherwise have hidden.

Settings.Format writes what the backend publishes, CloudEvents binary with the ce- prefix when unset, and Settings.Reads is the chain it reads with, binary then structured when unset. String headers no format takes reach a handler as Delivery.Foreign (messaging spec 0014).

Specified by https://gitlab.com/phpboyscout/go/messaging/-/wikis/specs/0009-the-rabbitmq-backend

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.NewSentinel("messaging_rabbitmq.closed", "messaging-rabbitmq: the session is closed")

ErrClosed is returned by Publish and Subscribe after Close.

View Source
var ErrManagement = errors.NewSentinel("messaging_rabbitmq.management",
	"messaging-rabbitmq: the management API is not answering; this backend verifies every queue through it")

ErrManagement is returned by Open when the management API is unreachable or disabled: every claim about a queue comes from it (0009 D1).

View Source
var ErrNoExchange = errors.NewSentinel("messaging_rabbitmq.no_exchange",
	"messaging-rabbitmq: the exchange this backend is bound to does not exist; a bus binds to an exchange and never creates one")

ErrNoExchange is returned by Open when the configured exchange does not exist: the bus binds, and never creates (0005 D9, 0009 D2).

View Source
var ErrNoQueue = errors.NewSentinel("messaging_rabbitmq.no_queue",
	"messaging-rabbitmq: the queue this subscription consumes does not exist; a bus resolves a queue and never creates one")

ErrNoQueue is returned by Subscribe when the queue a subscription names does not exist.

View Source
var ErrUnroutable = errors.NewSentinel("messaging_rabbitmq.unroutable",
	"messaging-rabbitmq: no queue is bound for this subject; the exchange returned the message")

ErrUnroutable is returned by Publish when no queue is bound for the subject: refused rather than dropped (0009 D11).

Functions

func New

func New(s Settings) messaging.Backend

New is the RabbitMQ backend (0009 D1): one constructor for 3.x and 4.x, quorum queues for every at-least-once identity, the management API for every fact about them.

Types

type ByName

type ByName struct{}

ByName is the default Topology: the durable name is the queue.

func (ByName) Queue

Queue returns the durable name.

type Management

type Management struct {
	// URL is the API's base, such as http://host:15672.
	URL string

	Username string
	Password string

	// VHost is the virtual host, "/" when empty.
	VHost string
}

Management is the HTTP management API's endpoint and credentials.

type Settings

type Settings struct {
	// URL is the AMQP 0-9-1 endpoint, amqp:// or amqps://, credentials
	// included.
	URL string

	// Management is the HTTP management API.
	Management Management

	// Exchange is the operator's topic exchange: the subject space. It must
	// exist; Open fails naming it otherwise (0009 D2).
	Exchange string

	// Topology resolves the queue a subscription consumes. Nil means the
	// durable name is the queue's name (0009 D3). An operator's tool may
	// create on resolve; a test fixture is one.
	Topology Topology

	// DefaultAckWait is the lease when a subscription asks for none. Zero
	// means thirty seconds (0009 D9): the broker's own timeout is not a lease.
	DefaultAckWait time.Duration

	// Logger receives what the backend cannot return. Nil discards.
	Logger *slog.Logger

	// Format writes every event this backend publishes. Unset, it is
	// CloudEvents binary with the ce- prefix (0014 D4, D6).
	Format envelope.Format

	// Reads is the chain a received message is read with, strictly: one
	// format must claim it. Unset, it is binary with the ce- prefix, then
	// structured.
	Reads []envelope.Format
}

Settings is what the backend is built from (0009 D1). Both endpoints are required: management is a dependency, not a hedge, because every claim this backend makes about a queue comes from it.

type Topology

type Topology interface {
	// Queue is the name of the quorum queue for an at-least-once
	// subscription. The spec is the subscription's, so an operator's tool
	// can see what it asks for.
	Queue(ctx context.Context, spec messaging.SourceSpec) (string, error)
}

Topology is the operator's answer to which queue a subscription consumes (0009 D3). The module verifies and never creates; a Topology that creates is the operator's tool.

Jump to

Keyboard shortcuts

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