Documentation
¶
Overview ¶
Package amqp is a fast AMQP 0-9-1 client without dependencies, for brokers such as LavinMQ and RabbitMQ.
It has two APIs.
High-level API ¶
NewClient returns a Client that keeps a connection to the broker. It reconnects when the connection is lost and then recovers the exchanges, queues, bindings and subscriptions declared through it. Messages are published as persistent and confirmed by the broker by default, and subscriptions acknowledge messages when the handler returns:
c, err := amqp.NewClient(ctx, "amqp://guest:guest@localhost", nil)
if err != nil {
return err
}
defer c.Close()
q, err := c.Queue(ctx, "jobs", nil) // durable
if err != nil {
return err
}
sub, err := q.Subscribe(ctx, func(ctx context.Context, d *amqp.Delivery) error {
return process(d.Body) // ack on nil, requeue on error
}, &amqp.SubscribeOptions{Prefetch: 50, Workers: 4})
if err != nil {
return err
}
defer sub.Cancel(ctx)
err = q.Publish(ctx, amqp.Message{Body: "hello"}) // waits for the confirm
With Codecs message bodies are serialized and encoded according to their content type and content encoding, see ClientOptions and Delivery.Decode. Client.RPCServer and Client.RPCCall implement request/reply over direct reply-to.
Low-level API ¶
Dial returns a Connection and Connection.Channel opens a Channel with methods that map one-to-one to the protocol, e.g. Channel.QueueDeclare, Channel.BasicPublish and Channel.BasicConsume. Nothing is recovered if the connection is lost:
conn, err := amqp.Dial(ctx, "amqp://guest:guest@localhost", nil)
if err != nil {
return err
}
defer conn.Close()
ch, err := conn.Channel(ctx)
if err != nil {
return err
}
q, err := ch.QueueDeclare(ctx, "", amqp.QueueDeclareOptions{Exclusive: true})
if err != nil {
return err
}
err = ch.BasicPublish(ctx, "", q.Name, amqp.Publishing{Body: []byte("hi")})
Concurrency and performance ¶
Connections, channels and clients are safe for concurrent use. Synchronous methods called concurrently on one channel are pipelined. Frames are written to a buffer and flushed by a background goroutine, so that concurrent or rapid publishes and acks are sent in batches with few syscalls. Received messages are buffered per consumer, so a slow consumer never stalls the connection; limit the buffer with a prefetch count.
Errors ¶
Exceptions from the broker are returned as *Error, with a reply code such as NotFound or AccessRefused, check them with IsCode. A channel exception closes the channel, a connection exception the connection. All errors caused by a closed channel or connection match ErrClosed with errors.Is.
Example ¶
package main
import (
"context"
"fmt"
"log"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
c, err := amqp.NewClient(ctx, "amqp://guest:guest@localhost", nil)
if err != nil {
log.Fatal(err)
}
defer c.Close()
// A server-named, exclusive, auto-delete queue
q, err := c.Queue(ctx, "", nil)
if err != nil {
log.Fatal(err)
}
received := make(chan string)
sub, err := q.Subscribe(ctx, func(ctx context.Context, d *amqp.Delivery) error {
received <- string(d.Body)
return nil // acknowledges the message
}, nil)
if err != nil {
log.Fatal(err)
}
defer sub.Cancel(ctx)
// Waits until the broker has confirmed the message
if err := q.Publish(ctx, amqp.Message{Body: "Hello world"}); err != nil {
log.Fatal(err)
}
fmt.Println(<-received)
}
Output:
Index ¶
- Constants
- Variables
- func IsCode(err error, code uint16) bool
- type Channel
- func (ch *Channel) BasicAck(deliveryTag uint64, multiple bool) error
- func (ch *Channel) BasicCancel(ctx context.Context, consumerTag string) error
- func (ch *Channel) BasicConsume(ctx context.Context, queue string, opts ConsumeOptions) (*Consumer, error)
- func (ch *Channel) BasicGet(ctx context.Context, queue string, noAck bool) (msg *Delivery, ok bool, err error)
- func (ch *Channel) BasicNack(deliveryTag uint64, multiple, requeue bool) error
- func (ch *Channel) BasicPublish(ctx context.Context, exchange, routingKey string, msg Publishing) error
- func (ch *Channel) BasicPublishConfirm(ctx context.Context, exchange, routingKey string, msg Publishing) (*Confirmation, error)
- func (ch *Channel) BasicQos(ctx context.Context, prefetchCount uint16, global bool) error
- func (ch *Channel) BasicRecover(ctx context.Context, requeue bool) error
- func (ch *Channel) BasicReject(deliveryTag uint64, requeue bool) error
- func (ch *Channel) Close() error
- func (ch *Channel) CloseReason(code uint16, reason string) error
- func (ch *Channel) ConfirmSelect(ctx context.Context) error
- func (ch *Channel) Connection() *Connection
- func (ch *Channel) Done() <-chan struct{}
- func (ch *Channel) Err() error
- func (ch *Channel) ExchangeBind(ctx context.Context, destination, source, routingKey string, args Table) error
- func (ch *Channel) ExchangeDeclare(ctx context.Context, name, kind string, opts ExchangeDeclareOptions) error
- func (ch *Channel) ExchangeDelete(ctx context.Context, name string, ifUnused bool) error
- func (ch *Channel) ExchangeUnbind(ctx context.Context, destination, source, routingKey string, args Table) error
- func (ch *Channel) Flow(ctx context.Context, active bool) error
- func (ch *Channel) ID() uint16
- func (ch *Channel) IsClosed() bool
- func (ch *Channel) OnReturn(fn func(*Return))
- func (ch *Channel) QueueBind(ctx context.Context, queue, exchange, routingKey string, args Table) error
- func (ch *Channel) QueueDeclare(ctx context.Context, name string, opts QueueDeclareOptions) (QueueInfo, error)
- func (ch *Channel) QueueDelete(ctx context.Context, queue string, opts QueueDeleteOptions) (uint32, error)
- func (ch *Channel) QueuePurge(ctx context.Context, queue string) (uint32, error)
- func (ch *Channel) QueueUnbind(ctx context.Context, queue, exchange, routingKey string, args Table) error
- func (ch *Channel) TxCommit(ctx context.Context) error
- func (ch *Channel) TxRollback(ctx context.Context) error
- func (ch *Channel) TxSelect(ctx context.Context) error
- func (ch *Channel) WaitForConfirms(ctx context.Context) error
- type Client
- func (c *Client) Close() error
- func (c *Client) Connection(ctx context.Context) (*Connection, error)
- func (c *Client) DefaultExchange() *Exchange
- func (c *Client) DirectExchange(ctx context.Context, name string) (*Exchange, error)
- func (c *Client) Done() <-chan struct{}
- func (c *Client) Err() error
- func (c *Client) Exchange(ctx context.Context, name, kind string, opts *ExchangeOptions) (*Exchange, error)
- func (c *Client) FanoutExchange(ctx context.Context, name string) (*Exchange, error)
- func (c *Client) HeadersExchange(ctx context.Context, name string) (*Exchange, error)
- func (c *Client) Publish(ctx context.Context, exchange, routingKey string, msg Message) error
- func (c *Client) Queue(ctx context.Context, name string, opts *QueueOptions) (*Queue, error)
- func (c *Client) RPCCall(ctx context.Context, queue string, msg Message) (*Delivery, error)
- func (c *Client) RPCClient(ctx context.Context) (*RPCClient, error)
- func (c *Client) RPCServer(ctx context.Context, queue string, handler RPCHandler, opts *SubscribeOptions) (*Subscription, error)
- func (c *Client) TopicExchange(ctx context.Context, name string) (*Exchange, error)
- type ClientOptions
- type Codecs
- type Config
- type Confirmation
- type Connection
- func (c *Connection) Blocked() bool
- func (c *Connection) Channel(ctx context.Context) (*Channel, error)
- func (c *Connection) ChannelMax() uint16
- func (c *Connection) Close() error
- func (c *Connection) CloseReason(reason string) error
- func (c *Connection) Done() <-chan struct{}
- func (c *Connection) Err() error
- func (c *Connection) FrameMax() uint32
- func (c *Connection) Heartbeat() time.Duration
- func (c *Connection) IsClosed() bool
- func (c *Connection) LocalAddr() net.Addr
- func (c *Connection) RemoteAddr() net.Addr
- func (c *Connection) ServerProperties() Table
- func (c *Connection) UpdateSecret(ctx context.Context, secret, reason string) error
- type ConsumeOptions
- type Consumer
- func (c *Consumer) Cancel(ctx context.Context) error
- func (c *Consumer) Channel() *Channel
- func (c *Consumer) Deliveries() <-chan *Delivery
- func (c *Consumer) Done() <-chan struct{}
- func (c *Consumer) Err() error
- func (c *Consumer) Next(ctx context.Context) (*Delivery, error)
- func (c *Consumer) Queue() string
- func (c *Consumer) Tag() string
- type Decimal
- type Delivery
- type Encoder
- type Error
- type Exchange
- func (x *Exchange) Bind(ctx context.Context, source, routingKey string, args Table) error
- func (x *Exchange) Delete(ctx context.Context, ifUnused bool) error
- func (x *Exchange) Kind() string
- func (x *Exchange) Name() string
- func (x *Exchange) Publish(ctx context.Context, routingKey string, msg Message) error
- func (x *Exchange) Unbind(ctx context.Context, source, routingKey string, args Table) error
- type ExchangeDeclareOptions
- type ExchangeOptions
- type Handler
- type Message
- type Properties
- type Publishing
- type Queue
- func (q *Queue) Bind(ctx context.Context, exchange, bindingKey string, args Table) error
- func (q *Queue) Delete(ctx context.Context, opts QueueDeleteOptions) (n uint32, err error)
- func (q *Queue) Get(ctx context.Context, noAck bool) (msg *Delivery, ok bool, err error)
- func (q *Queue) Name() string
- func (q *Queue) Publish(ctx context.Context, msg Message) error
- func (q *Queue) Purge(ctx context.Context) (n uint32, err error)
- func (q *Queue) Subscribe(ctx context.Context, handler Handler, opts *SubscribeOptions) (*Subscription, error)
- func (q *Queue) Unbind(ctx context.Context, exchange, bindingKey string, args Table) error
- type QueueDeclareOptions
- type QueueDeleteOptions
- type QueueInfo
- type QueueOptions
- type RPCClient
- type RPCHandler
- type Return
- type Serializer
- type SubscribeOptions
- type Subscription
- type Table
- type URI
Examples ¶
Constants ¶
const ( Direct = "direct" Fanout = "fanout" Topic = "topic" Headers = "headers" )
Exchange types
const ( Transient uint8 = 1 Persistent uint8 = 2 )
Delivery modes for Properties.DeliveryMode.
const ( ReplySuccess = 200 ContentTooLarge = 311 NoRoute = 312 NoConsumers = 313 ConnectionForced = 320 InvalidPath = 402 AccessRefused = 403 NotFound = 404 ResourceLocked = 405 PreconditionFailed = 406 FrameError = 501 SyntaxError = 502 CommandInvalid = 503 ChannelError = 504 UnexpectedFrame = 505 ResourceError = 506 NotAllowed = 530 NotImplemented = 540 InternalError = 541 )
Reply codes used by the broker when closing a connection or channel, see Error.Code.
const DefaultMaxDecodedSize = 128 << 20
DefaultMaxDecodedSize is the decoded size limit of Gzip and Deflate.
const Version = "1.2.1"
Version of the library, sent to the broker in the client properties.
Variables ¶
var ( // ErrUnsupportedContentType is returned when no Serializer is // registered for a message's content type. ErrUnsupportedContentType = errors.New("amqp: unsupported content type") // ErrUnsupportedContentEncoding is returned when no Encoder is // registered for a message's content encoding. ErrUnsupportedContentEncoding = errors.New("amqp: unsupported content encoding") // ErrDecodedTooLarge is returned when a compressed body decodes to more // than the encoder's limit, see [NewGzip]. ErrDecodedTooLarge = errors.New("amqp: decoded body exceeds the size limit") )
var ( // ErrClosed reports that the connection, channel or client is closed. // Every error caused by a closed connection or channel matches it with // errors.Is, including an [*Error] from the broker and network errors. ErrClosed = errors.New("amqp: closed") // ErrPublishNacked is returned when the broker negatively acknowledges // a published message, i.e. it couldn't take responsibility for it. ErrPublishNacked = errors.New("amqp: message nacked by the broker") // ErrConsumerCancelled is returned by a consumer after it has been // cancelled by the client and all buffered deliveries are read. ErrConsumerCancelled = errors.New("amqp: consumer cancelled") // ErrConsumerCancelledByServer is returned by a consumer the broker // cancelled, e.g. because its queue was deleted. ErrConsumerCancelledByServer = errors.New("amqp: consumer cancelled by the broker") // ErrUnconfirmed is matched by the error for a published message whose // channel closed before the broker confirmed it. The message may or may // not have been routed. The error also matches ErrClosed. ErrUnconfirmed = errors.New("amqp: channel closed before the message was confirmed") // ErrNoConfirmMode is returned when waiting for confirms on a channel // that isn't in confirm mode. ErrNoConfirmMode = errors.New("amqp: channel is not in confirm mode") // ErrChannelMax is returned when all channel ids are in use. ErrChannelMax = errors.New("amqp: channel max reached") )
var Deflate = NewDeflate(DefaultMaxDecodedSize)
Deflate encodes bodies with zlib-wrapped deflate (RFC 1950), which is what HTTP's "deflate" content encoding means. It decodes at most DefaultMaxDecodedSize bytes.
var ErrAlreadyAcknowledged = errors.New("amqp: delivery already acknowledged or rejected")
ErrAlreadyAcknowledged is returned when acknowledging or rejecting a delivery a second time, or one delivered with no-ack, which would otherwise make the broker close the channel.
var ErrUnroutable = errors.New("amqp: message could not be routed to any queue")
ErrUnroutable is returned by an RPC call when no queue received the request, i.e. there's no server for the method.
var Gzip = NewGzip(DefaultMaxDecodedSize)
Gzip encodes bodies with gzip (RFC 1952). It decodes at most DefaultMaxDecodedSize bytes, protecting against compression bombs.
Functions ¶
func IsCode ¶
IsCode reports whether err is an *Error with the given reply code.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"log"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
conn, err := amqp.Dial(ctx, "amqp://localhost", nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ch, err := conn.Channel(ctx)
if err != nil {
log.Fatal(err)
}
_, err = ch.QueueDeclare(ctx, "might-not-exist", amqp.QueueDeclareOptions{Passive: true})
switch {
case amqp.IsCode(err, amqp.NotFound):
fmt.Println("no such queue") // the channel is closed now
case errors.Is(err, amqp.ErrClosed):
fmt.Println("connection lost")
case err != nil:
log.Fatal(err)
}
}
Output:
Types ¶
type Channel ¶
type Channel struct {
// contains filtered or unexported fields
}
Channel is an AMQP channel, a lightweight virtual connection multiplexed over a Connection. It's safe for concurrent use: synchronous methods (declarations, bindings etc.) from multiple goroutines are pipelined, the broker answers them in order.
A channel is closed by the broker if an operation fails, e.g. declaring a queue passively that doesn't exist. Its methods then return an *Error, and a new channel has to be opened.
func (*Channel) BasicAck ¶
BasicAck acknowledges a delivery, or with multiple all deliveries up to and including deliveryTag.
func (*Channel) BasicCancel ¶
BasicCancel cancels a consumer. Deliveries already received can still be read from the consumer.
func (*Channel) BasicConsume ¶
func (ch *Channel) BasicConsume(ctx context.Context, queue string, opts ConsumeOptions) (*Consumer, error)
BasicConsume starts a consumer on a queue. Read deliveries with Consumer.Next or Consumer.Deliveries. Set a prefetch limit with Channel.BasicQos first, as deliveries are buffered in memory until read.
func (*Channel) BasicGet ¶
func (ch *Channel) BasicGet(ctx context.Context, queue string, noAck bool) (msg *Delivery, ok bool, err error)
BasicGet polls a queue for a message. ok is false if the queue is empty. Unless noAck the message has to be acknowledged.
func (*Channel) BasicNack ¶
BasicNack rejects a delivery, or with multiple all deliveries up to and including deliveryTag. With requeue the messages are put back in the queue, otherwise they're dropped or dead-lettered.
func (*Channel) BasicPublish ¶
func (ch *Channel) BasicPublish(ctx context.Context, exchange, routingKey string, msg Publishing) error
BasicPublish publishes a message to an exchange. It returns when the message is written to the connection's buffer, it's then sent to the broker in the background. If the broker has blocked the connection it waits until it's unblocked or ctx is done.
Use Channel.BasicPublishConfirm to know when the broker has taken responsibility for the message.
func (*Channel) BasicPublishConfirm ¶
func (ch *Channel) BasicPublishConfirm(ctx context.Context, exchange, routingKey string, msg Publishing) (*Confirmation, error)
BasicPublishConfirm publishes a message and returns a confirmation to wait on. It puts the channel in confirm mode if it isn't already. Waiting for each confirmation in turn limits the throughput to one message per round trip, so publish many messages concurrently (or pipeline them) and wait for the confirmations afterwards.
Example ¶
package main
import (
"context"
"fmt"
"log"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
conn, err := amqp.Dial(ctx, "amqp://localhost", nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ch, err := conn.Channel(ctx)
if err != nil {
log.Fatal(err)
}
// Publish a batch, then wait for all confirms; waiting for each one in
// turn would limit the throughput to one message per round trip.
confirms := make([]*amqp.Confirmation, 0, 1000)
for i := range 1000 {
c, err := ch.BasicPublishConfirm(ctx, "amq.topic", "numbers", amqp.Publishing{Body: fmt.Append(nil, i)})
if err != nil {
log.Fatal(err)
}
confirms = append(confirms, c)
}
for _, c := range confirms {
if err := c.Wait(ctx); err != nil {
log.Fatal(err)
}
}
}
Output:
func (*Channel) BasicQos ¶
BasicQos limits the number of unacknowledged messages delivered to each consumer on the channel (or, with global, shared by all consumers on the channel in RabbitMQ). Zero means unlimited.
func (*Channel) BasicRecover ¶
BasicRecover asks the broker to redeliver all unacknowledged messages on the channel. Not supported by all brokers with requeue=false.
func (*Channel) BasicReject ¶
BasicReject rejects a single delivery, see Channel.BasicNack.
func (*Channel) Close ¶
Close closes the channel gracefully. Unacknowledged messages are requeued by the broker. It returns nil if the channel is already closed.
func (*Channel) CloseReason ¶
CloseReason closes the channel with a reply code and text, which the broker might log.
func (*Channel) ConfirmSelect ¶
ConfirmSelect puts the channel in confirm mode, the broker then acks or nacks every published message.
func (*Channel) Connection ¶
func (ch *Channel) Connection() *Connection
Connection returns the connection the channel belongs to.
func (*Channel) Done ¶
func (ch *Channel) Done() <-chan struct{}
Done returns a channel that's closed when the channel is closed.
func (*Channel) Err ¶
Err returns why the channel was closed, or nil if it's open. See Connection.Err.
func (*Channel) ExchangeBind ¶
func (ch *Channel) ExchangeBind(ctx context.Context, destination, source, routingKey string, args Table) error
ExchangeBind binds the destination exchange to the source exchange, so that messages routed by the source with a matching routing key are routed by the destination too.
func (*Channel) ExchangeDeclare ¶
func (ch *Channel) ExchangeDeclare(ctx context.Context, name, kind string, opts ExchangeDeclareOptions) error
ExchangeDeclare declares an exchange of the given kind, e.g. Direct, Fanout, Topic, Headers or a plugin type such as "x-consistent-hash".
func (*Channel) ExchangeDelete ¶
ExchangeDelete deletes an exchange. With ifUnused it's only deleted if it has no bindings.
func (*Channel) ExchangeUnbind ¶
func (ch *Channel) ExchangeUnbind(ctx context.Context, destination, source, routingKey string, args Table) error
ExchangeUnbind removes an exchange-to-exchange binding.
func (*Channel) Flow ¶
Flow asks the broker to pause (active=false) or resume deliveries. Not supported by RabbitMQ.
func (*Channel) OnReturn ¶
OnReturn sets a function that's called with messages published with Mandatory that the broker couldn't route. It's called from the connection's read loop, so it must not block or call synchronous methods on the connection. The broker sends the return before the publish confirm, so when a confirmation arrives the return has been processed.
func (*Channel) QueueBind ¶
func (ch *Channel) QueueBind(ctx context.Context, queue, exchange, routingKey string, args Table) error
QueueBind binds a queue to an exchange. Args are matched against message headers by headers exchanges.
func (*Channel) QueueDeclare ¶
func (ch *Channel) QueueDeclare(ctx context.Context, name string, opts QueueDeclareOptions) (QueueInfo, error)
QueueDeclare declares a queue, creating it if it doesn't exist. Pass an empty name to let the broker generate a unique name.
func (*Channel) QueueDelete ¶
func (ch *Channel) QueueDelete(ctx context.Context, queue string, opts QueueDeleteOptions) (uint32, error)
QueueDelete deletes a queue and returns the number of messages it had.
func (*Channel) QueuePurge ¶
QueuePurge removes all ready messages from a queue and returns how many were removed.
func (*Channel) QueueUnbind ¶
func (ch *Channel) QueueUnbind(ctx context.Context, queue, exchange, routingKey string, args Table) error
QueueUnbind removes a binding between a queue and an exchange.
func (*Channel) TxRollback ¶
TxRollback abandons the current transaction.
func (*Channel) WaitForConfirms ¶
WaitForConfirms waits until all messages published on the channel before the call are confirmed. It returns ErrPublishNacked if any message was nacked since the previous call, and an error matching ErrUnconfirmed if the channel closed with messages unconfirmed.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the high-level API. It keeps a connection to the broker, reconnecting when it's lost, and recovers the exchanges, queues, bindings and subscriptions declared through it. Messages are published as persistent and confirmed by the broker by default.
Operations wait (bounded by their context) while the client is reconnecting. A Client is safe for concurrent use.
Example (Codecs) ¶
package main
import (
"context"
"fmt"
"log"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
c, err := amqp.NewClient(ctx, "amqp://localhost", &amqp.ClientOptions{
Config: amqp.Config{Codecs: amqp.DefaultCodecs()},
DefaultContentType: "application/json",
DefaultContentEncoding: "gzip",
})
if err != nil {
log.Fatal(err)
}
defer c.Close()
type Order struct {
ID int `json:"id"`
Items []string `json:"items"`
}
q, err := c.Queue(ctx, "orders", nil)
if err != nil {
log.Fatal(err)
}
// Serialized as JSON and gzip compressed
if err := q.Publish(ctx, amqp.Message{Body: Order{ID: 1, Items: []string{"book"}}}); err != nil {
log.Fatal(err)
}
_, err = q.Subscribe(ctx, func(ctx context.Context, d *amqp.Delivery) error {
var o Order
if err := d.Decode(&o); err != nil {
d.Reject(false) // can't be processed, don't requeue
return err
}
fmt.Println(o.ID, o.Items)
return nil
}, nil)
if err != nil {
log.Fatal(err)
}
}
Output:
func NewClient ¶
NewClient connects to the broker at url and returns a client that keeps the connection alive. It fails if the first connection attempt fails. opts may be nil.
func (*Client) Close ¶
Close stops the client: subscriptions are stopped (waiting for running handlers to return, their context is cancelled), and the connection is closed. Unacknowledged messages are requeued by the broker. As Close waits for handlers it must not be called from one, do that in a new goroutine.
func (*Client) Connection ¶
func (c *Client) Connection(ctx context.Context) (*Connection, error)
Connection returns the current connection, waiting for the client to reconnect if necessary. Channels opened on it aren't recovered.
func (*Client) DefaultExchange ¶
DefaultExchange returns the default exchange, which routes messages to the queue named by the routing key.
func (*Client) DirectExchange ¶
DirectExchange declares a durable direct exchange, use "amq.direct" for the built-in one.
func (*Client) Done ¶
func (c *Client) Done() <-chan struct{}
Done returns a channel that's closed when the client is closed or gave up reconnecting.
func (*Client) Exchange ¶
func (c *Client) Exchange(ctx context.Context, name, kind string, opts *ExchangeOptions) (*Exchange, error)
Exchange declares an exchange of the given kind (Direct, Fanout, Topic, Headers or a plugin type) and returns a handle to it. With nil options the exchange is durable. Exchanges named "amq.*" are built in, they're only checked for existence. The exchange is redeclared when the client reconnects.
func (*Client) FanoutExchange ¶
FanoutExchange declares a durable fanout exchange, use "amq.fanout" for the built-in one.
func (*Client) HeadersExchange ¶
HeadersExchange declares a durable headers exchange, use "amq.headers" for the built-in one.
func (*Client) Publish ¶
Publish publishes a message to an exchange and waits for the broker to confirm it. Messages are persistent unless DeliveryMode is set to Transient. Publish from multiple goroutines for higher throughput, the confirms are pipelined.
If the connection is lost before the message is confirmed an error is returned, the message may or may not have reached the broker.
func (*Client) Queue ¶
Queue declares a queue and returns a handle to it. The queue (and its bindings) are redeclared when the client reconnects.
With nil options a named queue is declared durable, and a queue with an empty name is declared exclusive and auto-delete with a name generated by the broker (which changes on reconnect).
func (*Client) RPCCall ¶
RPCCall sends a request to an RPC server consuming from queue and waits for the reply, use a context with a deadline to not wait forever. It uses a shared RPCClient.
Example ¶
package main
import (
"context"
"fmt"
"log"
"time"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
c, err := amqp.NewClient(ctx, "amqp://localhost", nil)
if err != nil {
log.Fatal(err)
}
defer c.Close()
_, err = c.RPCServer(ctx, "upcase", func(ctx context.Context, req *amqp.Delivery) (amqp.Message, error) {
return amqp.Message{Body: fmt.Sprintf("%X", req.Body)}, nil
}, nil)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
reply, err := c.RPCCall(ctx, "upcase", amqp.Message{Body: "hi"})
if err != nil {
log.Fatal(err)
}
fmt.Println(string(reply.Body))
}
Output:
func (*Client) RPCServer ¶
func (c *Client) RPCServer(ctx context.Context, queue string, handler RPCHandler, opts *SubscribeOptions) (*Subscription, error)
RPCServer serves RPC requests from a queue (declared durable if it doesn't exist). Replies are sent to the request's ReplyTo with its CorrelationID, and with its content type and encoding unless the reply sets them. Cancel the returned subscription to stop serving.
func (*Client) TopicExchange ¶
TopicExchange declares a durable topic exchange, use "amq.topic" for the built-in one.
Example ¶
package main
import (
"context"
"log"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
c, err := amqp.NewClient(ctx, "amqp://localhost", nil)
if err != nil {
log.Fatal(err)
}
defer c.Close()
events, err := c.TopicExchange(ctx, "events")
if err != nil {
log.Fatal(err)
}
q, err := c.Queue(ctx, "user-events", nil)
if err != nil {
log.Fatal(err)
}
// Bindings are recovered after reconnects too
if err := q.Bind(ctx, events.Name(), "user.*", nil); err != nil {
log.Fatal(err)
}
err = events.Publish(ctx, "user.created", amqp.Message{
Body: `{"id": 1}`,
Properties: amqp.Properties{ContentType: "application/json"},
})
if err != nil {
log.Fatal(err)
}
}
Output:
type ClientOptions ¶
type ClientOptions struct {
// Config is used for every connection the client makes.
Config
// ReconnectInterval is the delay before the second reconnect attempt
// (the first is immediate), doubled for each failed attempt up to
// MaxReconnectInterval. Defaults to 1s and 30s. A connection that's
// lost within MaxReconnectInterval of being established counts as a
// failed attempt, so a broker that closes connections right away isn't
// reconnected to in a tight loop.
ReconnectInterval time.Duration
MaxReconnectInterval time.Duration
// MaxRetries is the number of consecutive failed reconnect attempts
// before the client gives up and closes. Zero retries forever. Each
// attempt is bounded by Config.ConnectTimeout, 30s by default.
MaxRetries int
// OnConnect is called after each successful connection, including the
// first one, once the topology is recovered. The client's methods can
// be used in it, e.g. to declare topology that isn't recovered
// automatically. Its context is cancelled if the connection is lost.
// What's declared with that context (queues, exchanges, bindings) isn't
// recovered after a reconnect, as OnConnect declares it again, and
// subscriptions started with it stop when the connection is lost.
// An error from the first call makes NewClient fail, later errors are
// logged. Later calls run in their own goroutine, one at a time.
OnConnect func(ctx context.Context, c *Client) error
// OnDisconnect is called when the connection is lost.
OnDisconnect func(err error)
// OnFailed is called when the client gives up reconnecting, see
// MaxRetries.
OnFailed func(err error)
// OnReturn is called with messages published with Mandatory that
// couldn't be routed. It must not block.
OnReturn func(*Return)
// DefaultContentType and DefaultContentEncoding are used when a
// published message doesn't set them.
DefaultContentType string
DefaultContentEncoding string
}
ClientOptions are the options for NewClient.
type Codecs ¶
type Codecs struct {
// contains filtered or unexported fields
}
Codecs is a registry of serializers by content type and encoders by content encoding. Register all codecs before the registry is used, it's not safe to modify concurrently with use.
func DefaultCodecs ¶
func DefaultCodecs() *Codecs
DefaultCodecs returns a registry with the built-in codecs: "application/json" (JSON), "text/plain" (Text), "gzip" (Gzip) and "deflate" (Deflate).
func (*Codecs) Marshal ¶
func (c *Codecs) Marshal(v any, p *Properties) ([]byte, error)
Marshal serializes v according to p.ContentType and then encodes it according to p.ContentEncoding. A []byte or string v isn't serialized, only encoded.
func (*Codecs) RegisterEncoder ¶
RegisterEncoder registers an encoder for a content encoding.
func (*Codecs) RegisterSerializer ¶
func (c *Codecs) RegisterSerializer(contentType string, s Serializer) *Codecs
RegisterSerializer registers a serializer for a content type. Parameters such as "; charset=utf-8" are ignored when looking up a content type.
type Config ¶
type Config struct {
// ConnectionName is shown in the broker's management UI. Defaults to
// the program name.
ConnectionName string
// Heartbeat is the requested heartbeat interval. Zero uses the broker's
// suggestion, a negative value disables heartbeats.
Heartbeat time.Duration
// FrameMax is the largest frame size the client accepts, defaults to
// 131072. The smaller of the client's and broker's values is used.
FrameMax uint32
// ChannelMax is the maximum number of open channels, defaults to 2047.
// The smaller of the client's and broker's values is used.
ChannelMax uint16
// ConnectTimeout bounds the TCP connect, TLS handshake and AMQP
// handshake, in addition to the context passed to Dial. Defaults to
// the URI's connection_timeout, and for the Client's reconnects to 30s.
ConnectTimeout time.Duration
// TLSConfig is used for amqps:// URIs. ServerName defaults to the host.
TLSConfig *tls.Config
// Dial overrides how the network connection is established, e.g. to
// use a proxy or a UNIX socket. The connection is wrapped in TLS
// afterwards if the URI scheme is amqps.
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
// ClientProperties are added to the client properties sent to the broker.
ClientProperties Table
// Codecs used by [Delivery.Decode] (and the [Client] when publishing).
Codecs *Codecs
// Logger receives warnings, such as frames for unknown channels.
// Defaults to slog.Default().
Logger *slog.Logger
// OnBlocked is called when the broker blocks publishing on the
// connection, e.g. because of a memory or disk alarm. It's called from
// the connection's read loop and must not block.
OnBlocked func(reason string)
// OnUnblocked is called when the broker lifts the block.
OnUnblocked func()
}
Config holds the optional settings for a connection. Settings in the URI query string are used for the fields left unset here.
type Confirmation ¶
type Confirmation struct {
// contains filtered or unexported fields
}
Confirmation is the broker's confirmation of a published message, see Channel.BasicPublishConfirm.
func (*Confirmation) Acked ¶
func (c *Confirmation) Acked() bool
Acked reports whether the broker acked the message, valid after Done is closed.
func (*Confirmation) DeliveryTag ¶
func (c *Confirmation) DeliveryTag() uint64
DeliveryTag returns the publish sequence number of the message.
func (*Confirmation) Done ¶
func (c *Confirmation) Done() <-chan struct{}
Done returns a channel that's closed when the broker has acked or nacked the message, or the channel was closed before that.
func (*Confirmation) Wait ¶
func (c *Confirmation) Wait(ctx context.Context) error
Wait waits for the confirmation. It returns nil if the broker acked the message, ErrPublishNacked if it was nacked, or an error matching ErrUnconfirmed (and ErrClosed) if the channel closed before the message was confirmed. The message may or may not have been routed then, and Channel.Err tells why the channel closed.
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection is an AMQP connection. It's safe for concurrent use.
Frames are appended to a write buffer and written to the socket by a background goroutine, which coalesces frames written concurrently, or in quick succession, into fewer syscalls (see writer.go).
func Dial ¶
Dial connects to the broker at uri, e.g. "amqp://guest:guest@localhost/vhost". cfg may be nil. The context bounds the TCP connect, the TLS handshake and the AMQP handshake.
Example ¶
package main
import (
"context"
"fmt"
"log"
"time"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
conn, err := amqp.Dial(ctx, "amqp://guest:guest@localhost", &amqp.Config{
ConnectionName: "example",
Heartbeat: 10 * time.Second,
})
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ch, err := conn.Channel(ctx)
if err != nil {
log.Fatal(err)
}
q, err := ch.QueueDeclare(ctx, "", amqp.QueueDeclareOptions{Exclusive: true, AutoDelete: true})
if err != nil {
log.Fatal(err)
}
if err := ch.BasicQos(ctx, 100, false); err != nil {
log.Fatal(err)
}
consumer, err := ch.BasicConsume(ctx, q.Name, amqp.ConsumeOptions{})
if err != nil {
log.Fatal(err)
}
err = ch.BasicPublish(ctx, "", q.Name, amqp.Publishing{
Properties: amqp.Properties{ContentType: "text/plain"},
Body: []byte("Hello world"),
})
if err != nil {
log.Fatal(err)
}
d, err := consumer.Next(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(d.Body))
d.Ack()
}
Output:
func (*Connection) Blocked ¶
func (c *Connection) Blocked() bool
Blocked reports whether the broker has blocked publishing on the connection.
func (*Connection) Channel ¶
func (c *Connection) Channel(ctx context.Context) (*Channel, error)
Channel opens a new channel.
func (*Connection) ChannelMax ¶
func (c *Connection) ChannelMax() uint16
ChannelMax returns the negotiated maximum channel id.
func (*Connection) Close ¶
func (c *Connection) Close() error
Close closes the connection gracefully, waiting for the broker to confirm. All channels and consumers are closed.
func (*Connection) CloseReason ¶
func (c *Connection) CloseReason(reason string) error
CloseReason is like Close, the reason might be logged by the broker.
func (*Connection) Done ¶
func (c *Connection) Done() <-chan struct{}
Done returns a channel that's closed when the connection is closed, for any reason. Err then returns the reason.
func (*Connection) Err ¶
func (c *Connection) Err() error
Err returns why the connection was closed: ErrClosed if closed by the client, an *Error if closed by the broker, or a network error (that matches ErrClosed). It returns nil while the connection is open.
func (*Connection) FrameMax ¶
func (c *Connection) FrameMax() uint32
FrameMax returns the negotiated maximum frame size.
func (*Connection) Heartbeat ¶
func (c *Connection) Heartbeat() time.Duration
Heartbeat returns the negotiated heartbeat interval, 0 if disabled.
func (*Connection) IsClosed ¶
func (c *Connection) IsClosed() bool
IsClosed reports whether the connection is closed.
func (*Connection) LocalAddr ¶
func (c *Connection) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (*Connection) RemoteAddr ¶
func (c *Connection) RemoteAddr() net.Addr
RemoteAddr returns the broker's network address.
func (*Connection) ServerProperties ¶
func (c *Connection) ServerProperties() Table
ServerProperties returns the properties the broker sent in the handshake, such as "product", "version" and "capabilities".
func (*Connection) UpdateSecret ¶
func (c *Connection) UpdateSecret(ctx context.Context, secret, reason string) error
UpdateSecret updates the credentials of the connection, e.g. a renewed OAuth 2 token, without reconnecting.
type ConsumeOptions ¶
type ConsumeOptions struct {
// Tag identifies the consumer, the broker generates one if empty.
Tag string
// NoAck makes the broker consider messages acknowledged as soon as
// they're delivered.
NoAck bool
// Exclusive requests to be the only consumer of the queue.
Exclusive bool
// NoLocal isn't supported by RabbitMQ or LavinMQ.
NoLocal bool
// Args are optional arguments, e.g. "x-stream-offset" or "x-priority".
Args Table
}
ConsumeOptions are the options for Channel.BasicConsume.
type Consumer ¶
type Consumer struct {
// contains filtered or unexported fields
}
Consumer receives deliveries from a queue, see Channel.BasicConsume. Deliveries are buffered in memory until read, so the connection never waits for a slow consumer. Limit the buffer with Channel.BasicQos.
func (*Consumer) Cancel ¶
Cancel cancels the consumer. Deliveries already buffered can still be read, after that Next returns ErrConsumerCancelled.
func (*Consumer) Deliveries ¶
Deliveries returns a Go channel of deliveries, which is closed when the consumer is cancelled or its channel closed, Err then tells why. The returned channel must be read until it's closed. Consumer.Next is slightly faster.
Example ¶
package main
import (
"context"
"fmt"
"log"
amqp "github.com/cloudamqp/amqp-client.go"
)
func main() {
ctx := context.Background()
conn, err := amqp.Dial(ctx, "amqp://localhost", nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ch, err := conn.Channel(ctx)
if err != nil {
log.Fatal(err)
}
ch.BasicQos(ctx, 10, false)
consumer, err := ch.BasicConsume(ctx, "jobs", amqp.ConsumeOptions{})
if err != nil {
log.Fatal(err)
}
shutdown := make(chan struct{})
for {
select {
case d, ok := <-consumer.Deliveries():
if !ok {
log.Println("consumer stopped:", consumer.Err())
return
}
fmt.Println(string(d.Body))
d.Ack()
case <-shutdown:
consumer.Cancel(ctx)
}
}
}
Output:
func (*Consumer) Done ¶
func (c *Consumer) Done() <-chan struct{}
Done returns a channel that's closed when the consumer stops receiving deliveries, there might still be buffered deliveries to read.
func (*Consumer) Next ¶
Next returns the next delivery, waiting for one if none is buffered. When the consumer is cancelled it returns the buffered deliveries first and then the reason: ErrConsumerCancelled, ErrConsumerCancelledByServer or the channel's error. Next can be called from multiple goroutines.
type Delivery ¶
type Delivery struct {
Properties
Body []byte
ConsumerTag string // empty for basic.get
DeliveryTag uint64 // identifies the delivery on its channel
Redelivered bool // the message has been delivered before
Exchange string // the exchange the message was published to
RoutingKey string // the routing key the message was published with
MessageCount uint32 // messages left in the queue, only for basic.get
// contains filtered or unexported fields
}
Delivery is a message delivered to a consumer or fetched with Channel.BasicGet.
func (*Delivery) Ack ¶
Ack acknowledges the delivery, the broker then removes the message. It returns ErrAlreadyAcknowledged if the delivery is already acknowledged or rejected, or was delivered with no-ack.
func (*Delivery) Acknowledged ¶
Acknowledged reports whether the delivery is acknowledged or rejected, with its own methods or the channel's with multiple, or was delivered with no-ack.
func (*Delivery) Decode ¶
Decode decodes the body into v according to the message's content encoding and content type, using the connection's Config.Codecs or DefaultCodecs. v can also be a *[]byte or *string to get the body with only the content encoding decoded.
type Encoder ¶
Encoder applies a content encoding, such as "gzip", to message bodies.
func NewDeflate ¶ added in v1.2.0
NewDeflate returns a deflate encoder that fails to decode bodies larger than maxDecodedSize with ErrDecodedTooLarge.
type Error ¶
type Error struct {
Code uint16
Reason string
ClassID uint16 // class of the method that caused the error, or 0
MethodID uint16 // method that caused the error, or 0
// Connection is true when the whole connection was closed, otherwise
// only the channel was closed.
Connection bool
// Server is true when the broker sent the exception.
Server bool
}
Error is an exception from the broker (or the client) that closed a channel or a connection. Code is one of the reply codes, such as NotFound or PreconditionFailed.
type Exchange ¶
type Exchange struct {
// contains filtered or unexported fields
}
Exchange is a handle to an exchange declared with Client.Exchange.
func (*Exchange) Bind ¶
Bind binds the exchange (as destination) to the source exchange, so that messages matching the routing key are routed to this exchange too. The binding is recovered when the client reconnects.
func (*Exchange) Delete ¶
Delete deletes the exchange. With ifUnused it's only deleted if it has no bindings. It's no longer recovered on reconnect.
func (*Exchange) Publish ¶
Publish publishes a message to the exchange and waits for the broker to confirm it. See Client.Publish.
type ExchangeDeclareOptions ¶
type ExchangeDeclareOptions struct {
// Passive only checks if the exchange exists, the channel is closed
// with a NotFound error otherwise.
Passive bool
// Durable exchanges survive broker restarts.
Durable bool
// AutoDelete exchanges are deleted when their last binding is removed.
AutoDelete bool
// Internal exchanges can't be published to directly, only via
// exchange-to-exchange bindings.
Internal bool
// Args are optional arguments, e.g. "alternate-exchange".
Args Table
}
ExchangeDeclareOptions are the options for Channel.ExchangeDeclare.
type ExchangeOptions ¶
ExchangeOptions are the options for Client.Exchange, see ExchangeDeclareOptions for their meaning.
type Handler ¶
Handler processes a delivery for a Subscription. The message is acknowledged when the handler returns nil and rejected and requeued when it returns an error (or panics), unless the handler acknowledged or rejected it itself, e.g. with d.Reject(false) to dead-letter a message that can't be processed. ctx is cancelled when the client is closed.
type Message ¶
type Message struct {
Properties
// Body is a []byte or string, which is sent as is, or a value that's
// serialized according to ContentType by the client's codecs.
// Bodies are also encoded according to ContentEncoding (e.g. gzip) if
// there are codecs.
Body any
// Mandatory makes the broker return the message, see
// [ClientOptions.OnReturn], if it can't be routed to a queue.
Mandatory bool
}
Message is a message published with the high-level API.
type Properties ¶
type Properties struct {
ContentType string // MIME content type, e.g. "application/json"
ContentEncoding string // MIME content encoding, e.g. "gzip"
Headers Table // Application headers
DeliveryMode uint8 // Transient (1) or Persistent (2)
Priority uint8 // 0 to 9
CorrelationID string // Correlate a reply with a request
ReplyTo string // Address to reply to, usually a queue name
Expiration string // Message TTL in milliseconds, as a string
MessageID string // Application message identifier
Timestamp time.Time // Message timestamp, second precision
Type string // Message type name
UserID string // Creating user id, validated by the broker
AppID string // Creating application id
}
Properties are the basic class content properties of a message. Zero values are not sent on the wire.
type Publishing ¶
type Publishing struct {
Properties
Body []byte
// Mandatory makes the broker return the message, see
// [Channel.OnReturn], if it can't be routed to any queue.
Mandatory bool
}
Publishing is a message to publish.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue is a handle to a queue declared with Client.Queue.
func (*Queue) Bind ¶
Bind binds the queue to an exchange. The binding is recovered when the client reconnects.
func (*Queue) Delete ¶
Delete deletes the queue and returns the number of messages it had. It is no longer recovered on reconnect.
func (*Queue) Get ¶
Get fetches a message from the queue, ok is false if it's empty. Unless noAck the message has to be acknowledged. Gets share a channel, which the broker closes if a get fails (e.g. because the queue was deleted); the messages fetched on it but not yet acknowledged are then requeued.
func (*Queue) Name ¶
Name returns the queue's name. The name of a queue declared with an empty name changes when the client reconnects.
func (*Queue) Publish ¶
Publish publishes a message directly to the queue, via the default exchange, and waits for the broker to confirm it. See Client.Publish.
func (*Queue) Purge ¶
Purge removes all ready messages from the queue and returns how many were removed.
func (*Queue) Subscribe ¶
func (q *Queue) Subscribe(ctx context.Context, handler Handler, opts *SubscribeOptions) (*Subscription, error)
Subscribe consumes messages from the queue, calling handler for each, see Subscription.
type QueueDeclareOptions ¶
type QueueDeclareOptions struct {
// Passive only checks if the queue exists, the channel is closed with
// a NotFound error otherwise.
Passive bool
// Durable queues survive broker restarts. Note that messages also have
// to be published as persistent to survive.
Durable bool
// Exclusive queues can only be used by this connection and are deleted
// when it closes.
Exclusive bool
// AutoDelete queues are deleted when their last consumer is cancelled.
AutoDelete bool
// Args are optional arguments, e.g. "x-queue-type", "x-max-length" or
// "x-dead-letter-exchange".
Args Table
}
QueueDeclareOptions are the options for Channel.QueueDeclare.
type QueueDeleteOptions ¶
type QueueDeleteOptions struct {
IfUnused bool // only delete if the queue has no consumers
IfEmpty bool // only delete if the queue has no messages
}
QueueDeleteOptions are the options for Channel.QueueDelete.
type QueueInfo ¶
type QueueInfo struct {
Name string // the queue name, generated by the broker if declared with ""
MessageCount uint32 // number of messages ready in the queue
ConsumerCount uint32 // number of consumers
}
QueueInfo is the broker's reply to a queue declaration.
type QueueOptions ¶
QueueOptions are the options for Client.Queue, see QueueDeclareOptions for their meaning.
type RPCClient ¶
type RPCClient struct {
// contains filtered or unexported fields
}
RPCClient makes RPC calls using direct reply-to. Calls can be made concurrently, and the client recovers after reconnects.
type RPCHandler ¶
RPCHandler handles an RPC request and returns the reply. If it returns an error the request is rejected (dead-lettered if the queue has a dead-letter exchange) and no reply is sent.
type Return ¶
type Return struct {
Properties
Body []byte
ReplyCode uint16 // e.g. NoRoute
ReplyText string
Exchange string
RoutingKey string
}
Return is a message the broker couldn't route, published with the Mandatory flag.
type Serializer ¶
Serializer converts values to and from message bodies of a content type, such as "application/json".
var JSON Serializer = jsonSerializer{}
JSON serializes values with encoding/json.
var Text Serializer = textSerializer{}
Text serializes values with fmt, e.g. numbers and fmt.Stringers. It can only decode into *string and *[]byte.
type SubscribeOptions ¶
type SubscribeOptions struct {
// Prefetch is the maximum number of unacknowledged messages, defaults
// to 20, or twice the number of workers if that's higher.
Prefetch uint16
// Workers is the number of goroutines calling the handler, defaults to
// one, which processes messages in order.
Workers int
// NoAck makes the broker consider messages acknowledged when they're
// delivered, they're lost if the client fails to process them.
NoAck bool
// Exclusive requests to be the only consumer of the queue.
Exclusive bool
// ConsumerTag identifies the consumer, generated by the broker if empty.
ConsumerTag string
// Args are optional arguments, e.g. {"x-stream-offset": "first"}.
Args Table
}
SubscribeOptions are the options for Queue.Subscribe.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription is a consumer that survives reconnects: when the connection (or just the consumer's channel) is lost it consumes again as soon as possible. Messages that were being processed when the connection was lost are redelivered by the broker, so handlers should be idempotent.
func (*Subscription) Cancel ¶
func (s *Subscription) Cancel(ctx context.Context) error
Cancel stops the subscription. It waits for running handlers to return (or ctx to be done), messages that weren't processed are requeued. When called from the subscription's handler, with the handler's context, it doesn't wait for the handlers.
func (*Subscription) ConsumerTag ¶
func (s *Subscription) ConsumerTag() string
ConsumerTag returns the tag of the current consumer, it changes when the subscription is recovered if the broker generated it.
func (*Subscription) Done ¶
func (s *Subscription) Done() <-chan struct{}
Done returns a channel that's closed when the subscription stops, either because it was cancelled, the client was closed or it couldn't be recovered, e.g. because the queue was deleted. Err then tells why.
func (*Subscription) Err ¶
func (s *Subscription) Err() error
Err returns why the subscription stopped, or nil while it's active.
func (*Subscription) Queue ¶
func (s *Subscription) Queue() *Queue
Queue returns the queue the subscription consumes from.
type Table ¶
Table is an AMQP field table, used for message headers and for the arguments to declarations, bindings and consumers.
These Go types can be encoded:
nil void
bool boolean ('t')
int8, uint8 short-short int ('b', 'B')
int16, uint16 short int ('s', 'u')
int32, uint32 long int ('I', 'i')
int, int64, uint, long-long int ('l')
uint64 (≤ MaxInt64)
float32, float64 float ('f'), double ('d')
Decimal decimal ('D')
string long string ('S')
[]byte byte array ('x')
time.Time timestamp ('T'), second precision
Table, nested table ('F')
map[string]any
[]any, []string, array ('A')
[]Table, []int,
[]int64
Decoded tables only contain bool, int8, uint8, int16, uint16, int32, uint32, int64, float32, float64, Decimal, string, []byte, time.Time, Table, []any and nil values.
type URI ¶
type URI struct {
TLS bool
Host string
Port int
Username string
Password string
Vhost string
// Query parameters recognized by ParseURI, zero when absent.
Heartbeat time.Duration // heartbeat=<seconds>, -1 when explicitly 0 (disabled)
FrameMax uint32 // frame_max
ChannelMax uint16 // channel_max
ConnectionName string // name or connection_name
ConnectTimeout time.Duration // connection_timeout=<milliseconds>
AuthMechanism string // auth_mechanism, "PLAIN" or "EXTERNAL"
InsecureTLS bool // verify=verify_none
ServerName string // server_name_indication
}
URI is a parsed AMQP URI, see https://www.rabbitmq.com/docs/uri-spec.