amqp091-go Documentation

repository·main·Indexed 24 days ago

https://github.com/rabbitmq/amqp091-go

A Go implementation of the AMQP 0.9.1 protocol maintained by the RabbitMQ core team for interacting with RabbitMQ servers (version 2.0+). The library provides a functional interface for managing channels, queues, and exchanges, supporting PLAIN and EXTERNAL authentication mechanisms. It includes features for reliable publishing via publisher confirms, consumer prefetch configuration, and flow control handling.

Tokens
11.8K
Snippets
13
Records
78
Agent score
84%

What's inside amqp091-go

  1. Overview of amqp091-go

    main

    The amqp091-go library is a Go AMQP 0.9.1 client maintained by the RabbitMQ core team. It is designed to provide a functional interface that closely represents the AMQP 0.9.1 model specifically for use with RabbitMQ.

    Key Characteristics:

    • Compatibility: Supports RabbitMQ versions starting from 2.0.
    • Authentication: Supports PLAIN and EXTERNAL authentication mechanisms.
    • API Evolution: Unlike the original client, this library allows for reasonable breaking public API changes to improve the client over time.
    • Limitations:
      • Does not support AMQP 1.0 (which is a different protocol).
      • Does not support AMQP Protocol negotiation for forward/backward compatibility.
      • Does not guarantee ordering between basic.return and basic.ack frames sent over different channels due to the use of Go channels for protocol events.
  2. Migrating from streadway/amqp to amqp091-go

    main

    If you are migrating an existing project from the original streadway/amqp client to this maintained version, you can minimize code changes by using a package alias. This allows you to keep using the amqp identifier in your code while importing the new repository path.

    amqp "github.com/rabbitmq/amqp091-go"
  3. How DeferredConfirmation works

    main

    A DeferredConfirmation is a handle to a specific message's acknowledgement status. It is created during the publishing process and is tied to a unique DeliveryTag.

    It uses an internal done channel to signal when the server has responded. This allows for both blocking (Wait) and non-blocking (Acked) patterns. If the underlying connection or confirms object is closed, pending DeferredConfirmation objects are NACKed (set to false) to prevent goroutines from blocking indefinitely.

  4. How Connection and Topology Recovery work

    main

    The library provides a two-phase recovery mechanism to handle network interruptions:

    1. Connection Recovery: Re-establishes the TCP socket, performs the AMQP handshake (including SASL and tuning), and re-opens all previously active channels. It also re-applies QoS settings and channel-level confirms.
    2. Topology Recovery: Once channels are re-opened, the library re-declares the AMQP entities (exchanges, queues, and bindings) that were active before the drop.

    Transient vs. Durable Topology: When using TopologyRecoveryOnlyTransient, the client only re-declares 'transient' entities (e.g., auto-delete exchanges or exclusive/auto-delete queues) and their associated bindings. Durable, non-auto-delete entities are assumed to be retained by the broker and are not re-declared.

  5. Manage connection and channel lifecycle states

    main

    The amqp091 library uses LifeCycleState to represent the current status of a connection or channel. You can monitor these states to understand if a connection is active, recovering, or shutting down.

    Available states:

    • StateOpen: The connection or channel is active and open.
    • StateReconnecting: The connection or channel is actively undergoing automatic recovery.
    • StateClosing: The connection or channel is in the process of closing down.
    • StateClosed: The connection or channel is fully closed and shut down (terminal state).
  6. Monitor lifecycle state changes with StateChanged

    main

    When a connection or channel changes state, a StateChanged event is emitted. This event provides details about the transition, including the previous state, the new state, and potential errors or skipped entities.

    Key fields in StateChanged:

    • From: The LifeCycleState before the transition.
    • To: The LifeCycleState after the transition.
    • Err: Set only during a transition to StateClosed. It contains the fatal error that ended the recovery process (e.g., exhausted retries).
    • SkippedTopologyEntities: Set only during a StateReconnecting $\rightarrow$ StateOpen transition. It contains a list of TopologyRecoveryEntity objects that were skipped due to errors during recovery.
  7. How topology recovery modes work

    main

    The TopologyRecoveryMode determines which AMQP entities are automatically redeclared by the client after a reconnection. There are three modes:

    1. TopologyRecoveryAllEnabled (Default): Recovers everything tracked by the client, including exchanges, queues, bindings, and active consumers. Use this for standard client behavior.
    2. TopologyRecoveryOnlyTransient: Recovers only connection-scoped (transient) entities, such as exclusive or auto-delete queues/exchanges and their bindings. It skips durable entities, assuming they are managed externally. Note: Active consumers are always re-subscribed regardless of this mode.
    3. TopologyRecoveryDisabled: Disables topology and consumer recovery entirely. Connection and channel recovery will still occur if enabled, but the state (queues, exchanges, etc.) will not be restored.
  8. How automatic topology recovery works

    main

    The library automatically restores the state of the broker's topology upon reconnection. The recovery process follows these steps in order:

    1. Recover Exchanges: Re-declares all exchanges across all channels.
    2. Recover Queues: Re-declares all queues. If a server-generated queue (where DeclaredName was empty) receives a new name from the broker, the library automatically updates all subsequent bindings and consumer configurations to use this new name.
    3. Recover Queue-to-Exchange Bindings: Re-establishes bindings between queues and exchanges.
    4. Recover Exchange-to-Exchange Bindings: Re-establishes bindings between exchanges.
    5. Re-subscribe Consumers: Re-establishes consumers on the queues. This is done using basic.consume directly to ensure that existing delivery channels and goroutines are reused, meaning the application's consumer logic remains uninterrupted.
  9. Use the Channel type for AMQP operations

    main

    The Channel type is the primary context for performing AMQP operations. It represents an AMQP channel.

    Important: If any method on a Channel returns an error, that channel should be considered invalid and discarded. You must establish a new channel to continue operations.

  10. Configure automatic connection recovery

    main

    The amqp091.Config.Recovery field enables experimental automatic reconnection and topology recovery. When enabled, the connection and its channels will attempt to reconnect and recover topology (exchanges, queues, bindings, and consumers) after a network failure.

    Topology recovery scope is controlled by Recovery.TopologyRecoveryMode:

    • TopologyRecoveryAllEnabled (default): Recovers all tracked topology.
    • TopologyRecoveryOnlyTransient: Recovers only transient entities (exclusive/auto-delete queues/exchanges).
    • TopologyRecoveryDisabled: Skips topology and consumer recovery.

    Applications can monitor recovery state via Connection.NotifyStateChange and Channel.NotifyStateChange.

  11. How channel recovery works

    main

    Channel recovery in amqp091-go is a multi-step process designed to restore a channel to its functional state after a failure:

    1. reconnectChannel(): This is the core recovery mechanism. It attempts to open a fresh broker channel and performs basic setup (QoS and Confirmations). It uses a retry loop with backoff and jitter (up to 500ms) to avoid thundering herd issues. It does not recover topology.
    2. Topology Recovery: If ch.connection.IsTopologyRecoveryEnabled() is true, Reconnect() calls RecoverTopology to restore the AMQP entities (exchanges, queues, etc.) associated with the channel.
    3. openChannelSession(): This internal method handles the transition by resetting client-side state, opening the fresh broker channel, and restoring QoS/Confirm settings.
    4. reopenIfClosed(): This is used during topology recovery to handle 'soft errors' (like PRECONDITION_FAILED). If a broker error closes a channel during the recovery of other entities, this method attempts to reopen it so the recovery process can continue.