mq

package
v0.0.0-...-20269af Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package mq wrap the github.com/streadway/amqp library to be able to mock it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Delivery

type Delivery interface {
	Body() []byte
	Ack(bool) error
	Nack(bool, bool) error
}

Delivery interface provide a wrapper for the message and acknowledgment system of AMQP. It will allows the read the body of the message and either aknowledge it or non acknowledge it.

type MQ

type MQ struct {
	Conn    *amqp.Connection
	Channel *amqp.Channel
}

MQ struct is compliant to the MessageQueue interface.

func NewMQ

func NewMQ(amqpURL string) (mq MQ, err error)

NewMQ open a connection to AMQP server, open a channel of communication and then return a MQ struct holding the connection and the channel.

func (MQ) Consume

func (mq MQ) Consume(queueName string, r Receiver) error

Consume will start listening to the message queue using the provided queue name. It will call the Receiver function every time a message arrives.

func (MQ) DeclareQueue

func (mq MQ) DeclareQueue(queueName string) error

DeclareQueue declare a queue an set QoS

func (MQ) Publish

func (mq MQ) Publish(queueName string, body []byte) error

Publish provide a way to publish a message containing the provided body to the queue with name queueName

type Message

type Message struct {
	// contains filtered or unexported fields
}

Message is the wrapper for the Delivery struct of the github.com/streadway/amqp library. Its purpose is to met the be compliant with the Delivery interface in this package (mq) and help making the rest of the project testable.

func (Message) Ack

func (m Message) Ack(multiple bool) error

Ack delivers an acknowledgment that the message has been receive and treated. The multiple argument is true when the all the previous messages can be acknowledged as well.

func (Message) Body

func (m Message) Body() []byte

Body will return the body of the message.

func (Message) Nack

func (m Message) Nack(multiple, requeue bool) error

Nack delivers a negative acknowledgment signifying a failure in treating the message. If multiple is true, all the previous messages that weren't aknowledged yet are going to be negatively aknowledged. If requeue is true, it means that the message needs to be requeued.

type MessageQueue

type MessageQueue interface {
	DeclareQueue(string) error
	Publish(string, []byte) error
	// Consume should block until the underlying connection to is closed
	// or the Receiver function send a bool to the channel it's passed
	Consume(string, Receiver) error
}

MessageQueue interface help masking the complexity of the amqp library and reduce them to the needs of this project. The interface helps testing and only provide with the ability to declare a queue, publish on a queue and consume the elements coming on a queue.

type Receiver

type Receiver func(Delivery, chan bool)

Receiver is a function type that will be called every time a message arrives on the message queue. It will be provided with a Delivery. The Receiver can then use the Body of the Delivery to make the necessary computations and either acknowledge or non acknowledge the message for it to be requeued or not. The boolean channel can make the message queue stop consuming the queue. If so, it will be needed to call Consume again, because the function will return.

Jump to

Keyboard shortcuts

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