Documentation ¶
Overview ¶
Package server consolidates all the methods and functions required to handle connections and reconnections against RabbitMQ servers.
Usage ¶
The way to use it is by calling the New method. This will generate a Server that will take care of all the errors and reconnection handling. Then from the *amqp.Connection pointer returned by Server.Get() you can start exchanges and subscribe or publish to queues.
r, err := server.New( server.Address("amqp://user:pass@host.tld:port"), ) if err != nil { // handle error } reconn := r.NotifyReconnect() closer := r.NotifyClose() errors := r.NotifyErrors() for r.Loop() { select { case err := <-closer: // The connection was closed definitively, nothing else will be sent here or anything. // If it was closed because of an error, the err it will not be nil. Handle the error. if err != nil { // do something with the error here... } case <-reconn: // There was a reconnect, re-declare channels and queues so it can use the new connection. case err := <-errors: // Do something with the error or something... } }
The way rabbit can die is by either a reconnection error or because Server.Shutdown was called. It will notify via the Server.NotifyClose channel of type error. If it's nil it means it was closed cleanly.
In the implementation you can use Server.IsOpen() to know if the connection is open or not, if it's not you should not try to send messages as a publisher.
Index ¶
Constants ¶
const ENOCONN = "no connection available"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
An Option represents an optional value that can be passed to the New constructor of Server.
func Address ¶
Address receives an URL string value passed for the server connection string of Rabbit's. The default value that will be used is amqp://guest:guest@localhost:5672/. For the URI specification used by RabbitMQ check https://www.rabbitmq.com/uri-spec.html .
func ReconnectionLimits ¶
ReconnectionLimits receives an integer of how many times to attempt to reconnect. Default value is 5.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
A Server represents a RabbitMQ connection and it's states.
func New ¶
New returns an initialized and connected *Server using the environment variables as values. If a logger is passed it will send as informational messages the reconnections and shutdowns.
func (*Server) Channel ¶
Channel returns an *amqp.Channel that can be used to declare exchanges and publish/consume from queue.
func (*Server) IsOpen ¶
IsOpen returns a boolean that communicates the current state of the connection against the RabbitMQ server. As soon as the connection is established with a Server.connect() call it will be return true. During a amqp.NotifyClose or Server.Shutdown it will be set to false. During a reconnect it will be false until the reconnect succeeds.
If you want to be sure the connection is open, check with Server.Open before making any operation against the RabbitMQ server.
func (*Server) Loop ¶
Loop returns true if the connection is open or there's an active attempt to reconnect and get the Server to a working condition. This is specially useful to keep for-loops listening to the channels generated by Server as long as they actually exist.
func (*Server) NotifyClose ¶
NotifyClose returns a receiving-only channel with the error interface that will be only be called once, if the RabbitMQ connection is closed. The error returned will be nil if the close was created by the Server.Shutdown() method, else it will return an error.
func (*Server) NotifyErrors ¶
NotifyErrors returns a receiving-only channel with the error interface that will be receive messages each time something throws an error within the Server representation. Receiving an error does not means something fatal happened. If something fatal happens the error will be received in a closing channel. (Check the NotifyClose's method documentation.)
func (*Server) NotifyReconnect ¶
NotifyReconnect returns a receiving-only channel with true when a reconnect succeeds. In case of a reconnection event, the channels and queues linked to the Server connection need to be rebuilt.