Documentation
¶
Index ¶
- Constants
- type Authentication
- type Client
- type Config
- type ConnectedStruct
- type ConsumeConfig
- type Delivery
- type Exchange
- type ExchangeConfig
- type ExchangeType
- type HandleResponse
- type HandlerFunc
- type PostHandleFunc
- type PreHandleFunc
- type PublishConfig
- type Publisher
- type Queue
- type QueueBindConfig
- type Table
Constants ¶
const ( // A direct exchange routes messages to queues based on a specified routing key. // When a message is published to a direct exchange with a particular routing key, // it will be delivered to the queue(s) that are bound to the same exchange with a matching routing key. ExchangeTypeDirect = ExchangeType("direct") // A fanout exchange broadcasts messages to all queues that are bound to it, // regardless of routing keys. It is a simple publish/subscribe mechanism where // all queues receive a copy of each message sent to the exchange. ExchangeTypeFanout = ExchangeType("fanout") // A topic exchange is more flexible than direct exchanges. // It routes messages to queues based on wildcard patterns in the routing keys. // Routing keys can include wildcards like '*' (matches one word) and '#' (matches zero or more words), // allowing for complex message routing based on patterns. ExchangeTypeTopic = ExchangeType("topic") // Headers exchanges use message header attributes to determine message routing, rather than routing keys. // The exchange will match headers against predefined criteria to determine which queues should receive the message. ExchangeTypeHeaders = ExchangeType("headers") )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Authentication ¶ added in v1.1.0
Authentication interface provides a means for different SASL authentication mechanisms to be used during connection tuning.
type Client ¶ added in v1.1.0
type Client interface {
// Close closes the AMQP connection
Close() error
// Ping checks if the AMQP connection is active
Ping() error
// StartExchange starts a AMQP exchange with its own channel and returns the exchange as an entity
StartExchange(exchangeName string, exchangeType ExchangeType, conf ...ExchangeConfig) (Exchange, error)
// CreatePublisher creates a new publisher with its own channel to publish messages on an exchange, given the exchange name.
//
// When the optional NoWait flag is set to true, the publisher will not be created in confirmation mode.
// This means that when a message is published using this publisher, the library will not wait for confirmation a from the server.
//
// The NoWait flag should be used when your server does not support publishers in confirmation mode, or when you specifically want the publisher to be asynchronous.
CreatePublisher(exchangeName string, NoWait ...bool) (Publisher, error)
}
Client represents a Client connection, and contains functions to manage the AMQP client
type Config ¶
type Config struct {
// The SASL mechanisms to try in the client request, and the successful
// mechanism used on the Connection object.
// If SASL is nil, PlainAuth from the URL is used.
SASL []Authentication
// Vhost specifies the namespace of permissions, exchanges, queues and
// bindings on the server. Dial sets this to the path parsed from the URL.
Vhost string
ChannelMax int // 0 max channels means 2^16 - 1
FrameSize int // 0 max bytes means unlimited
Heartbeat time.Duration // less than 1s uses the server's interval
// TLSClientConfig specifies the client configuration of the TLS connection
// when establishing a tls transport.
// If the URL uses an amqps scheme, then an empty tls.Config with the
// ServerName from the URL is used.
TLSClientConfig *tls.Config
// Properties is table of properties that the client advertises to the server.
// This is an optional setting - if the application does not set this,
// the underlying library will use a generic set of client properties.
Properties Table
// Connection locale that we expect to always be en_US
// Even though servers must return it as per the AMQP 0-9-1 spec,
// we are not aware of it being used other than to satisfy the spec requirements
Locale string
// Dial returns a net.Conn prepared for a TLS handshake with TSLClientConfig,
// then an AMQP connection handshake.
// If Dial is nil, net.DialTimeout with a 30s connection and 30s deadline is
// used during TLS and AMQP handshaking.
Dial func(network, addr string) (net.Conn, error)
}
Config represents the optional configuration that the user can provide when connecting to the AMQP client
type ConnectedStruct ¶ added in v1.1.0
type ConnectedStruct interface {
// OnClose defines the function to execute when the AMQP channel for this struct is closed in any way.
//
// OnClose will start a new goroutine that listens to this connection 'closed' events.
OnClose(f func(err *amqp.Error))
}
ConnectedStruct represents a struct that's connected to an AMQP channel
type ConsumeConfig ¶
type ConsumeConfig struct {
// When AutoAck is set to true, it means that as soon as a message is delivered to the consumer,
// RabbitMQ automatically considers the message as acknowledged (ack) without the consumer having to send an explicit acknowledgment.
//
// default: false
AutoAck bool
// When a consumer is declared as Exclusive,
// it means that no other consumer can access the same queue on the same channel/connection.
// Exclusive consumers are often used for scenarios where you want to ensure that
// only one consumer processes messages from a specific queue.
//
// default: false
Exclusive bool
// The NoLocal parameter, when set to true,
// prevents a consumer from receiving messages that it publishes to the same connection.
// In other words, it prevents consumers from consuming their own messages.
//
// default: false
NoLocal bool
// When NoWait is set to True,
// it means that the method will not wait for a response from the server to confirm the consumption.
// In other words, it makes the declaration non-blocking.
// If any error occurs during the declaration, it won't be reported immediately.
//
// default: false
NoWait bool
// The name for the queue consumer.
// When a consumer name is not provided, the library will generate one based on the queue information.
ConsumerName string
// When declaring an consumer in AMQP, you can include a set of optional arguments to customize its behavior
// These arguments are provided as a collection of key-value pairs, where the keys represent specific configuration options,
// and the values determine the settings for those options.
Args Table
}
ConsumeConfig represents the configuration that can be provided when consuming a queue
type Exchange ¶
type Exchange interface {
ConnectedStruct
// BindQueue declares a new queue on the exchange given a queue config and binds it to the exchange
BindQueue(queueName, routingKey string, conf ...QueueBindConfig) (Queue, error)
// Before adds functions that will be called in the exchange before the message handling
Before(funcs ...PreHandleFunc)
// After adds functions that will be called in the exchange after the message handling
After(funcs ...PostHandleFunc)
// Name returns the exchange name
Name() string
// PreHandleFuncs returns the pre handle funcs for the exchange
PreHandleFuncs() []PreHandleFunc
// PostHandleFuncs returns the post handle funcs for the exchange
PostHandleFuncs() []PostHandleFunc
}
Exchange represents a AMQP message exchange
type ExchangeConfig ¶
type ExchangeConfig struct {
// When a exchange is declared as durable,
// it means that RabbitMQ will make efforts to ensure that the component survives server restarts or failures.
// Messages and configuration related to durable components are stored on disk.
// This is useful when you want to ensure that important data is not lost in case of server failures.
//
// default: false
Durable bool
// An auto-delete exchange is automatically deleted by RabbitMQ once there are no consumers or bindings left for it.
// This is often used for temporary components that are only needed for a specific duration or purpose.
// It's a way to clean up resources automatically when they are no longer needed.
//
// default: false
AutoDelete bool
// When an exchange is declared as internal,
// it means that it can't be directly published to by clients.
// Internal exchanges are used for internal RabbitMQ mechanisms and cannot be used for normal publishing of messages.
// They are useful for building advanced routing topologies within RabbitMQ.
//
// default: false
Internal bool
// When you declare a exchange with the "NoWait" option,
// it means that the method will not wait for a response from the server to confirm the declaration.
// This can improve declaration speed but comes with the trade-off that you won't receive an immediate response indicating success or failure.
// It's often used when you want to declare components quickly and are willing to skip the confirmation step.
//
// default: false
NoWait bool
// When declaring an exchange in AMQP, you can include a set of optional arguments to customize the behavior of the exchange
// These arguments are provided as a collection of key-value pairs, where the keys represent specific configuration options,
// and the values determine the settings for those options.
Args Table
}
ExchangeConfig represents the configuration for a rabbitmq exchange
type ExchangeType ¶
type ExchangeType string
ExchangeType represents a exchange type that defines its behaviour
func (ExchangeType) ToString ¶
func (t ExchangeType) ToString() string
ToString returns the string notation of the exchange type
type HandleResponse ¶
type HandleResponse struct {
// Nack defines if the message should NOT be acknowledged, and should be requeued (default: false)
Nack bool
// Err is the error that could have occurred during the message handling (default: nil)
Err error
}
HandleResponse represents the response when handling a message
type HandlerFunc ¶
type HandlerFunc func(context.Context, Delivery) HandleResponse
HandlerFunc represents a funcion that handles amqp messages
type PostHandleFunc ¶
type PostHandleFunc func(context.Context, Delivery, HandleResponse)
PostHandleFunc represents a middleware function to be called after handling amqp messages.
It has access to a copy of the message handling context, the message, and the handle response.
type PreHandleFunc ¶
PreHandleFunc represents a middleware function to be called before handling amqp messages.
It can alter the messaging context and even the message itself before anything is done
type PublishConfig ¶
type PublishConfig struct {
// When you set the mandatory flag to true while publishing a message,
// it indicates that the message must be routed to at least one queue.
// If the message cannot be routed to any queue, RabbitMQ will return the message to the publisher.
// This flag is typically used when you want to ensure that your message is not lost and must be delivered to at least one queue.
//
// default: false
Mandatory bool
// When you set the immediate flag to true while publishing a message,
// it indicates that the message should be delivered to a consumer as soon as possible.
// If there are no available consumers to immediately accept the message, RabbitMQ will return the message to the publisher.
//
// default: false
Imediate bool
// Message publishing is, by default, an asynchronous event.
// However, when the WaitConfirmation flag is set to true, and the publisher was created in confirmation mode,
// the Publish method will wait for the server to return a response confirming that the message was indeed published.
// It is important to note that this could potentially increase the message publishing time, depending on your server's latency.
//
// default: false
WaitConfirmation bool
// Application or exchange specific fields,
// the headers exchange will inspect this field.
Headers Table
ContentType string // MIME content type
ContentEncoding string // MIME content encoding
DeliveryMode uint8 // Transient (0 or 1) or Persistent (2)
Priority uint8 // 0 to 9
CorrelationId string // correlation identifier
ReplyTo string // address to to reply to (ex: RPC)
Expiration string // message expiration spec
MessageId string // message identifier
Timestamp time.Time // message timestamp
Type string // message type name
UserId string // creating user id - ex: "guest"
AppId string // creating application id
}
PublishConfig represents the configuration to publish a message
type Publisher ¶
type Publisher interface {
ConnectedStruct
// Publish publishes a message payload, in bytes format, on the publisher exchange.
// The user can also provide a routing-key to publish the message and some extra configuration for that message, if needed.
//
// It is important to note that the message publishing, by default, is asynchronous.
// However, you can make it synchronous by setting the WaitConfirmation flag from the PublishConfig as true.
Publish(payload []byte, key string, conf ...PublishConfig) error
// PublishJSON encodes the 'payload' param into a json string, and publishes it on the publisher exchange.
// The user can also provide a routing-key to publish the message and some extra configuration for that message, if needed.
//
// It is important to note that the message publishing, by default, is asynchronous.
// However, you can make it synchronous by setting the WaitConfirmation flag from the PublishConfig as true.
PublishJSON(payload any, key string, conf ...PublishConfig) error
}
Publisher represents a AMQP message publisher
type Queue ¶
type Queue interface {
// Consume subscribes a consumer in the routing key to handle the messages.
//
// Consume will start a new goroutine that listens to message publishings
// and handles them with the provided handler function
Consume(handlerFn HandlerFunc, conf ...ConsumeConfig) error
// Before adds functions that will be called in the queue before the message handling
Before(funcs ...PreHandleFunc)
// After adds functions that will be called in the queue after the message handling
After(funcs ...PostHandleFunc)
// Name returns the queue name
Name() string
// Exchange returns the Exchange that the queue is on
Exchange() Exchange
// RoutingKey returns the queue routing-key that was used to bind to the exchange
RoutingKey() string
// PreHandleFuncs returns the pre handle funcs for the queue
PreHandleFuncs() []PreHandleFunc
// PostHandleFuncs returns the post handle funcs for the queue
PostHandleFuncs() []PostHandleFunc
}
Queue represents a AMQP queue
type QueueBindConfig ¶
type QueueBindConfig struct {
// When a queue is declared as durable,
// it means that RabbitMQ will make efforts to ensure that the component survives server restarts or failures.
// Messages and configuration related to durable components are stored on disk.
// This is useful when you want to ensure that important data is not lost in case of server failures.
//
// default: false
Durable bool
// An auto-delete queue is automatically deleted by RabbitMQ once there are no consumers or bindings left for it.
// This is often used for temporary components that are only needed for a specific duration or purpose.
// It's a way to clean up resources automatically when they are no longer needed.
//
// default: false
AutoDelete bool
// When you declare a queue as exclusive,
// it means that the queue can only be accessed by the current connection.
// The queue will be automatically deleted by RabbitMQ when the connection that declared it is closed.
// Exclusive queues are often used in scenarios where you want to ensure that a queue is used only by a single consumer or where you want to create a temporary work queue for a particular client or session.
//
// When you declare a queue as not exclusive, multiple connections can access it concurrently.
// The queue will not be automatically deleted when the connection that declared it is closed.
// It will remain in RabbitMQ until it is explicitly deleted or until the server decides to remove it due to other factors, such as lack of use or expiration.
// Non-exclusive queues are typically used for more persistent, shared message processing scenarios where multiple consumers may need to access the same queue.
//
// default: false
Exclusive bool
// When you declare a queue with the "NoWait" option,
// it means that the method will not wait for a response from the server to confirm the declaration.
// This can improve declaration speed but comes with the trade-off that you won't receive an immediate response indicating success or failure.
// It's often used when you want to declare components quickly and are willing to skip the confirmation step.
//
// default: false
NoWait bool
// When declaring an queue in RabbitMQ, you can include a set of optional arguments to customize its behavior
// These arguments are provided as a collection of key-value pairs, where the keys represent specific configuration options,
// and the values determine the settings for those options.
Args Table
}
QueueConfig represents the configuration for binding a queue to an exchange
type Table ¶
Table represents the amqp extra arguments when executing AMQP actions.
When you do something using AMQP, like declaring an exchanges and queues, or publishing messages, you can include a set of optional arguments to customize its behavior.
These arguments are provided as a collection of key-value pairs, where the keys represent specific configuration options, and the values determine the settings for those options.
