amqp

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: 14 Imported by: 0

README

messaging-amqp

AMQP 1.0 backend for messaging: one protocol, generically, with a required Management seam for what the protocol cannot say; Apache ActiveMQ Artemis first.

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 0008.

One backend, one seam

New(settings, management)
wire AMQP 1.0 through Azure/go-amqp; nothing broker-specific in the spine
management a Management the constructor requires: what a broker offers by configuration, what a queue is, and the queues the backend owns. Artemis ships, speaking the broker's management address over the same connection
delivery modes AtMostOnce, AtLeastOnce
at-least-once an operator-created durable multicast queue named by DurableName, on the address of the pattern, verified at attach
at-most-once a volatile source terminus per fan-out source; the group's own queue for competing; accepted before hand-off
survives a restart what Management.Features says: on Artemis, only with the broker-global delivery-count persistence on, and then one attempt is lost to an orderly restart, declared
attempt delivery-count plus one
lease the consumer's own timer; holding is extending
patterns yes, in the address grammar (> becomes #), verified

Operating it

The address, every at-least-once queue and its address settings are the operator's; the backend verifies and creates nothing durable. An at-least-once queue must be durable multicast on the pattern's address, with no filter, so that it receives all of the address. The address must have a dead-letter-address with a queue bound and a max-delivery-attempts of at least MaxDeliver, and auto-create must be off on those addresses so a missing queue fails naming it rather than appearing volatile. SurvivesRestart is declared from the broker's configuration, not assumed.

Choosing a wire format

Settings.Format writes every event this backend publishes. Unset, the writer is CloudEvents binary for the AMQP binding, which is what this module has always written: attributes as application properties under the cloudEvents_ prefix, and the payload's type on the message's content-type property.

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. Binary reads the cloudEvents: prefix the binding also permits, as this module always has. A consumer can read structured before any producer writes it, so 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.

amqp.New(ctx, amqp.Settings{URL: url, Format: cloudevents.Structured()}, amqp.Artemis{})
  • String application properties no format takes, such as a tracing header or Artemis's _AMQ_DUPL_ID, reach a handler through Delivery.Foreign(). They are never sent on.
  • 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 handed back at once while it has attempts left, and rejected to the dead-letter address on its last. Under AtMostOnce it is accepted and dropped. Either way its credit is returned (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:

settings.Format = masstransit.MassTransit()
settings.Reads = []envelope.Format{cloudevents.Binary(cloudevents.AMQP), cloudevents.Structured(), masstransit.MassTransit()}

MassTransit writes application/vnd.masstransit+json on the content-type property and refuses at publish an ID that is not a UUID and a payload that is not JSON. NServiceBus writes its own headers as application properties 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 apache/artemis:2.57.0-alpine 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, and each run destroys its queues.

Status

Pre-1.0, tracking the core. The backend is built and green under conformance.RunDurable against Artemis 2.57.0 in a container, restart included. It declares Capabilities.RestartLosesLastFailure, because Artemis writes the delivery count as it delivers and an orderly restart returns a message numbered for the deliveries it made rather than the failures it saw (0005 D12, 0008 finding 16).

Documentation

Overview

Package amqp implements the backend contract of gitlab.com/phpboyscout/go/messaging over AMQP 1.0, through Azure/go-amqp, generically: nothing broker-specific in the module's spine, and what the protocol cannot say asked through a Management seam the constructor requires. Apache ActiveMQ Artemis is the first target, with a Management that speaks the broker's management address over the same connection.

At-least-once attaches to an operator-created durable multicast queue named by the durable name on the address translated from the pattern, and verifies it through Management at attach; at-most-once attaches with a volatile source terminus and takes the queue the broker gives the link. The attempt is delivery-count plus one; the lease is the consumer's own; a publish is durable and waits for the broker's disposition.

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

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

Index

Constants

This section is empty.

Variables

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

ErrClosed is returned by Publish and Subscribe after Close: the message went nowhere, or the source would outlive its session, and a caller must know.

View Source
var ErrManagementRefused = errors.NewSentinel("messaging_amqp.management_refused", "messaging-amqp: the broker refused the management request")

ErrManagementRefused is a management request the broker answered with a failure that is not "no such object".

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

ErrNoQueue is returned by Subscribe when the at-least-once queue a subscription names does not exist: the bus binds, and never creates (0005 D9, 0008 D3).

View Source
var ErrNotFound = errors.NewSentinel("messaging_amqp.not_found", "messaging-amqp: the broker has no such object")

ErrNotFound is Management's answer for a queue or address that does not exist.

View Source
var ErrUnrouted = errors.NewSentinel("messaging_amqp.unrouted",
	"messaging-amqp: no queue on the broker receives from this subject's address; a publish to it would be accepted and kept by nobody")

ErrUnrouted is returned by Publish when nothing on the broker would receive a message on the subject's address: accepted and gone (0008 D11).

Functions

func New

func New(ctx context.Context, s Settings, management Management) (messaging.Backend, error)

New is the AMQP 1.0 backend (0008 D1, D6): one protocol, generic, with the Management it requires. It dials once to read the broker's Features, so the capabilities it declares are the broker's configuration and constant thereafter; a Management is required, because without one the backend could only guess.

Types

type Artemis

type Artemis struct{}

Artemis is the Management for Apache ActiveMQ Artemis (0008 D1, OQ1): it speaks the broker's management address over the same connection — attribute reads and operation invocations as messages to activemq.management with a dynamic reply address — and needs 2.44.0 or later. No second endpoint, no HTTP.

func (Artemis) Create

func (a Artemis) Create(ctx context.Context, sess *goamqp.Session, spec QueueSpec) error

Create is createQueue with the JSON configuration and ignore-if-exists.

func (Artemis) Delete

func (a Artemis) Delete(ctx context.Context, sess *goamqp.Session, _, queue string) error

Delete is destroyQueue.

func (Artemis) Describe

func (a Artemis) Describe(ctx context.Context, sess *goamqp.Session, address, queue string) (QueueInfo, error)

Describe reads the queue's own attributes and its address's settings.

func (Artemis) Features

func (Artemis) Features(ctx context.Context, sess *goamqp.Session) (Features, error)

Features reads the broker-global settings the capabilities depend on (0008 D6, D8, D11). An attribute the broker does not expose reads as false: a feature that cannot be verified is not declared.

func (Artemis) Routes

func (a Artemis) Routes(ctx context.Context, sess *goamqp.Session, address string) (bool, error)

Routes (0008 D11): a queue on the address, or a wildcard address that matches it — a wildcard address's queues receive from every address it matches, and the broker lists them under the wildcard, not the subject.

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 Features

type Features struct {
	// PersistsDeliveryCount says the delivery count survives a restart with
	// the count intact. On Artemis, only with the broker-global setting on.
	PersistsDeliveryCount bool

	// Persists says a durable message routed to a durable queue is on disk
	// before its disposition: persistence on, journal sync on.
	Persists bool

	// Deduplicates says the broker keeps a duplicate-id cache the sender can
	// key.
	Deduplicates bool

	// DeadLetters says an exhausted or rejected message goes somewhere, not
	// nowhere, where the address is configured for it.
	DeadLetters bool
}

Features is what a broker offers by configuration (0008 D1): known at construction, constant thereafter, and the source of every capability this backend declares rather than assumes.

type Management

type Management interface {
	// Features is what the broker offers by configuration.
	Features(ctx context.Context, sess *goamqp.Session) (Features, error)

	// Describe reports a queue on an address as the broker holds it.
	// ErrNotFound if it does not exist.
	Describe(ctx context.Context, sess *goamqp.Session, address, queue string) (QueueInfo, error)

	// Routes says whether a publish to the address reaches any queue: one on
	// the address itself, or one on a wildcard address that matches it. A
	// publish that reaches none is accepted and gone (0008 D11).
	Routes(ctx context.Context, sess *goamqp.Session, address string) (bool, error)

	// Create makes a queue the backend owns; Delete removes one it owns.
	Create(ctx context.Context, sess *goamqp.Session, spec QueueSpec) error
	Delete(ctx context.Context, sess *goamqp.Session, address, queue string) error
}

Management is what the module cannot learn over the protocol, asked through the broker's own way of saying it (0008 D1). Every method takes the session so a Management can speak over the same connection.

type QueueInfo

type QueueInfo struct {
	Address     string
	RoutingType string // "MULTICAST" or "ANYCAST"
	Durable     bool
	Filter      string

	// From the address's settings.
	MaxDeliveryAttempts int
	DeadLetterAddress   string
	DeadLetterHasQueue  bool
}

QueueInfo is a queue as the broker holds it (0008 D3).

type QueueSpec

type QueueSpec struct {
	Name        string
	Address     string
	RoutingType string
	Durable     bool
	Filter      string
}

QueueSpec is a queue the backend asks Management to make (0008 D3).

type Settings

type Settings struct {
	// URL is the AMQP 1.0 endpoint, amqp:// or amqps://.
	URL string

	// Username and Password are SASL PLAIN credentials; both empty means
	// anonymous.
	Username string
	Password string

	// DefaultAckWait is the lease when a subscription asks for none. Zero
	// means thirty seconds (0008 D9): the protocol has no default of its own.
	DefaultAckWait time.Duration

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

	// Topology resolves the queue an at-least-once subscription attaches to.
	// Nil means the durable name is the queue's name (0008 D3). An operator's
	// tool may create on resolve; a test fixture is one.
	Topology Topology
	// Format writes every event this backend publishes. Unset, it is
	// CloudEvents binary for the AMQP binding (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 for the AMQP binding, then
	// structured.
	Reads []envelope.Format
}

Settings is what the backend is built from (0008 D1, D2).

type Topology

type Topology interface {
	// Queue is the queue's name on the pattern's address. The session is
	// passed so an operator's tool can use Management over the same
	// connection.
	Queue(ctx context.Context, sess *goamqp.Session, address string, spec messaging.SourceSpec) (string, error)
}

Topology is the operator's answer to which queue an at-least-once subscription attaches to (0008 D3). The module verifies and never creates.

Jump to

Keyboard shortcuts

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