server

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2019 License: MIT Imports: 6 Imported by: 0

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

View Source
const ENOCONN = "no connection available"

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Server) error

An Option represents an optional value that can be passed to the New constructor of Server.

func Address

func Address(addr string) Option

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 Logger

func Logger(l *log.Logger) Option

Logger receives a standard log.Logger interface to use as a logger.

func ReconnectionLimits

func ReconnectionLimits(limit int32) Option

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

func New(opts ...Option) (*Server, error)

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

func (r *Server) Channel() (*amqp.Channel, error)

Channel returns an *amqp.Channel that can be used to declare exchanges and publish/consume from queue.

func (*Server) IsOpen

func (r *Server) IsOpen() bool

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

func (r *Server) Loop() bool

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

func (r *Server) NotifyClose() <-chan error

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

func (r *Server) NotifyErrors() <-chan error

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

func (r *Server) NotifyReconnect() <-chan bool

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.

func (*Server) Shutdown

func (r *Server) Shutdown(reason string) error

Shutdown returns an error if the shutdown process of the RabbitMQ connection returned an error. A reason is expected and it will be shown in the logs if *Server.log is not nil.

Jump to

Keyboard shortcuts

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