2016-12-01 10:52:22 -02:00
|
|
|
package amqp
|
|
|
|
|
|
|
|
import (
|
2017-10-09 19:33:27 -03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-03-12 13:32:19 -03:00
|
|
|
"runtime/debug"
|
2017-03-09 17:32:05 -03:00
|
|
|
"sync"
|
2016-12-01 10:52:22 -02:00
|
|
|
"time"
|
|
|
|
|
2018-02-10 14:38:29 -02:00
|
|
|
"github.com/eventials/goevents/messaging"
|
|
|
|
|
2017-07-17 10:14:51 -03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-12-01 10:52:22 -02:00
|
|
|
amqplib "github.com/streadway/amqp"
|
|
|
|
)
|
|
|
|
|
2019-03-12 13:32:19 -03:00
|
|
|
// ErrNotAcked indicated that published messages was not acked by RabbitMQ
|
|
|
|
var ErrNotAcked = errors.New("messge was not acked")
|
2017-10-09 19:33:27 -03:00
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
type message struct {
|
|
|
|
action string
|
2017-10-10 17:49:53 -03:00
|
|
|
msg amqplib.Publishing
|
2017-03-09 17:32:05 -03:00
|
|
|
}
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
// producer holds a amqp connection and channel to publish messages to.
|
|
|
|
type producer struct {
|
2019-03-12 16:52:17 -03:00
|
|
|
m sync.Mutex
|
|
|
|
wg sync.WaitGroup
|
|
|
|
conn *connection
|
|
|
|
channel *amqplib.Channel
|
|
|
|
notifyConfirm chan amqplib.Confirmation
|
|
|
|
connectionClosed <-chan error
|
|
|
|
closeQueue chan bool
|
|
|
|
config ProducerConfig
|
2017-03-09 17:32:05 -03:00
|
|
|
|
|
|
|
internalQueue chan message
|
|
|
|
|
2016-12-28 09:20:29 -02:00
|
|
|
exchangeName string
|
2017-03-09 17:32:05 -03:00
|
|
|
|
|
|
|
closed bool
|
|
|
|
closes []chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProducerConfig to be used when creating a new producer.
|
|
|
|
type ProducerConfig struct {
|
|
|
|
publishInterval time.Duration
|
2016-12-01 10:52:22 -02:00
|
|
|
}
|
|
|
|
|
2016-12-01 16:17:55 -02:00
|
|
|
// NewProducer returns a new AMQP Producer.
|
2017-03-09 22:51:57 -03:00
|
|
|
// Uses a default ProducerConfig with 2 second of publish interval.
|
2017-10-10 17:49:53 -03:00
|
|
|
func NewProducer(c messaging.Connection, exchange string) (*producer, error) {
|
2017-03-09 22:24:53 -03:00
|
|
|
return NewProducerConfig(c, exchange, ProducerConfig{
|
2017-03-09 17:32:05 -03:00
|
|
|
publishInterval: 2 * time.Second,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:24:53 -03:00
|
|
|
// NewProducerConfig returns a new AMQP Producer.
|
2017-10-10 17:49:53 -03:00
|
|
|
func NewProducerConfig(c messaging.Connection, exchange string, config ProducerConfig) (*producer, error) {
|
2019-03-12 16:52:17 -03:00
|
|
|
conn := c.(*connection)
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
producer := &producer{
|
2019-03-12 16:52:17 -03:00
|
|
|
conn: c.(*connection),
|
|
|
|
config: config,
|
|
|
|
internalQueue: make(chan message, 2),
|
|
|
|
exchangeName: exchange,
|
|
|
|
notifyConfirm: make(chan amqplib.Confirmation),
|
|
|
|
closeQueue: make(chan bool),
|
|
|
|
connectionClosed: conn.NotifyConnectionClose(),
|
2017-03-09 17:32:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
err := producer.setupTopology()
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-12 13:32:19 -03:00
|
|
|
|
|
|
|
go producer.handleReestablishedConnnection()
|
|
|
|
|
|
|
|
return producer, err
|
2017-03-09 17:32:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Publish publishes an action.
|
2017-10-10 17:49:53 -03:00
|
|
|
func (p *producer) Publish(action string, data []byte) {
|
2019-03-12 16:52:17 -03:00
|
|
|
// ignore messages published to a closed producer
|
|
|
|
if p.isClosed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:32:19 -03:00
|
|
|
messageID, _ := NewUUIDv4()
|
2017-10-10 17:49:53 -03:00
|
|
|
|
|
|
|
p.publishAmqMessage(action, amqplib.Publishing{
|
2019-03-12 13:32:19 -03:00
|
|
|
MessageId: messageID,
|
2017-10-10 17:49:53 -03:00
|
|
|
DeliveryMode: amqplib.Persistent,
|
2018-04-24 15:41:26 -03:00
|
|
|
Timestamp: time.Now().UTC(),
|
2017-10-10 17:49:53 -03:00
|
|
|
Body: data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *producer) publishAmqMessage(queue string, msg amqplib.Publishing) {
|
2019-03-12 16:52:17 -03:00
|
|
|
p.wg.Add(1)
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
p.internalQueue <- message{
|
|
|
|
action: queue,
|
|
|
|
msg: msg,
|
|
|
|
}
|
2017-03-09 17:32:05 -03:00
|
|
|
}
|
2016-12-01 10:52:22 -02:00
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
// NotifyClose returns a channel to be notified then this producer closes.
|
2017-10-10 17:49:53 -03:00
|
|
|
func (p *producer) NotifyClose() <-chan bool {
|
2018-06-12 14:48:18 -03:00
|
|
|
receiver := make(chan bool, 1)
|
|
|
|
|
|
|
|
p.m.Lock()
|
2017-03-09 17:32:05 -03:00
|
|
|
p.closes = append(p.closes, receiver)
|
2018-06-12 14:48:18 -03:00
|
|
|
p.m.Unlock()
|
2017-03-09 17:32:05 -03:00
|
|
|
|
|
|
|
return receiver
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:52:17 -03:00
|
|
|
func (p *producer) setClosed() {
|
2017-10-09 19:33:27 -03:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
p.closed = true
|
2019-03-12 16:52:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the producer's internal queue.
|
|
|
|
func (p *producer) Close() {
|
|
|
|
p.setClosed()
|
|
|
|
|
|
|
|
p.wg.Wait()
|
|
|
|
|
|
|
|
p.closeQueue <- true
|
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
close(p.internalQueue)
|
2019-03-12 16:52:17 -03:00
|
|
|
close(p.closeQueue)
|
2019-03-12 13:32:19 -03:00
|
|
|
|
|
|
|
p.channel.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeChannel takes a new channel to the queue,
|
|
|
|
// and updates the channel listeners to reflect this.
|
2019-03-12 16:52:17 -03:00
|
|
|
func (p *producer) changeChannel(channel *amqplib.Channel) {
|
2019-03-12 13:32:19 -03:00
|
|
|
p.channel = channel
|
2019-03-12 16:52:17 -03:00
|
|
|
p.notifyConfirm = make(chan amqplib.Confirmation)
|
2019-03-12 13:32:19 -03:00
|
|
|
p.channel.NotifyPublish(p.notifyConfirm)
|
2017-03-09 17:32:05 -03:00
|
|
|
}
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
func (p *producer) setupTopology() error {
|
2017-03-09 22:51:57 -03:00
|
|
|
log.WithFields(log.Fields{
|
2017-05-24 18:23:07 -03:00
|
|
|
"type": "goevents",
|
|
|
|
"sub_type": "producer",
|
2017-03-09 22:51:57 -03:00
|
|
|
}).Debug("Setting up topology...")
|
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2019-03-12 13:32:19 -03:00
|
|
|
channel, err := p.conn.openChannel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-09 17:32:05 -03:00
|
|
|
|
2019-03-12 13:32:19 -03:00
|
|
|
if p.exchangeName != "" {
|
2017-10-10 17:49:53 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-28 09:20:29 -02:00
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
err = channel.ExchangeDeclare(
|
|
|
|
p.exchangeName, // name
|
|
|
|
"topic", // type
|
|
|
|
true, // durable
|
|
|
|
false, // auto-delete
|
|
|
|
false, // internal
|
|
|
|
false, // no-wait
|
|
|
|
nil, // arguments
|
|
|
|
)
|
2017-10-09 19:33:27 -03:00
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-28 09:20:29 -02:00
|
|
|
}
|
|
|
|
|
2019-03-12 13:32:19 -03:00
|
|
|
err = channel.Confirm(false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Channel could not be put into confirm mode: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.changeChannel(channel)
|
|
|
|
|
2017-03-09 22:51:57 -03:00
|
|
|
log.WithFields(log.Fields{
|
2017-05-24 18:23:07 -03:00
|
|
|
"type": "goevents",
|
|
|
|
"sub_type": "producer",
|
2019-03-12 16:52:17 -03:00
|
|
|
}).Debug("Topology ready. Draining internal queue.")
|
|
|
|
|
|
|
|
go p.drainInternalQueue()
|
2017-03-09 22:51:57 -03:00
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
return nil
|
2016-12-01 10:52:22 -02:00
|
|
|
}
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
func (p *producer) handleReestablishedConnnection() {
|
2018-06-12 14:48:18 -03:00
|
|
|
rs := p.conn.NotifyReestablish()
|
|
|
|
|
2019-03-12 16:52:17 -03:00
|
|
|
for !p.isClosed() {
|
2018-06-12 14:48:18 -03:00
|
|
|
<-rs
|
2017-03-09 17:32:05 -03:00
|
|
|
|
|
|
|
err := p.setupTopology()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
2019-03-12 13:32:19 -03:00
|
|
|
"type": "goevents",
|
|
|
|
"sub_type": "producer",
|
|
|
|
"error": err,
|
2017-05-24 18:23:07 -03:00
|
|
|
}).Error("Error setting up topology after reconnection.")
|
2017-03-09 17:32:05 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 14:48:18 -03:00
|
|
|
func (p *producer) publishMessage(msg amqplib.Publishing, queue string) (err error) {
|
2019-03-12 16:52:17 -03:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"action": queue,
|
|
|
|
"body": msg.Body,
|
|
|
|
"message_id": msg.MessageId,
|
|
|
|
"type": "goevents",
|
|
|
|
"sub_type": "producer",
|
|
|
|
"exchange": p.exchangeName,
|
|
|
|
}).Debug("Publishing message to the exchange.")
|
|
|
|
|
2018-06-12 14:48:18 -03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2019-03-12 13:32:19 -03:00
|
|
|
debug.PrintStack()
|
|
|
|
|
2018-06-12 14:48:18 -03:00
|
|
|
switch x := r.(type) {
|
|
|
|
case string:
|
|
|
|
err = errors.New(x)
|
|
|
|
case error:
|
|
|
|
err = x
|
|
|
|
default:
|
|
|
|
err = errors.New("Unknown panic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !p.conn.IsConnected() {
|
2019-03-12 13:32:19 -03:00
|
|
|
err = errors.New("connection is not open")
|
2018-06-12 14:48:18 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:32:19 -03:00
|
|
|
err = p.channel.Publish(p.exchangeName, queue, false, false, msg)
|
2017-10-09 19:33:27 -03:00
|
|
|
|
|
|
|
if err != nil {
|
2018-06-12 14:48:18 -03:00
|
|
|
return
|
2017-10-09 19:33:27 -03:00
|
|
|
}
|
|
|
|
|
2019-03-12 13:32:19 -03:00
|
|
|
select {
|
|
|
|
case confirm := <-p.notifyConfirm:
|
|
|
|
if confirm.Ack {
|
2018-06-12 14:48:18 -03:00
|
|
|
return
|
2017-10-09 19:33:27 -03:00
|
|
|
}
|
2019-03-12 13:32:19 -03:00
|
|
|
case <-time.After(p.config.publishInterval):
|
|
|
|
err = ErrNotAcked
|
|
|
|
return
|
2017-10-09 19:33:27 -03:00
|
|
|
}
|
|
|
|
|
2018-06-12 14:48:18 -03:00
|
|
|
return
|
2017-10-09 19:33:27 -03:00
|
|
|
}
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
func (p *producer) isClosed() bool {
|
2017-10-09 19:33:27 -03:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
return p.closed
|
|
|
|
}
|
|
|
|
|
2017-10-10 17:49:53 -03:00
|
|
|
func (p *producer) drainInternalQueue() {
|
2019-03-12 16:52:17 -03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-p.closeQueue:
|
|
|
|
return
|
|
|
|
case <-p.connectionClosed:
|
|
|
|
return
|
|
|
|
case m := <-p.internalQueue:
|
2019-03-12 13:32:19 -03:00
|
|
|
// block until confirmation
|
2017-10-10 17:49:53 -03:00
|
|
|
err := p.publishMessage(m.msg, m.action)
|
2017-03-09 17:32:05 -03:00
|
|
|
|
|
|
|
if err != nil {
|
2017-03-09 22:51:57 -03:00
|
|
|
log.WithFields(log.Fields{
|
2017-10-30 16:51:47 -02:00
|
|
|
"action": m.action,
|
|
|
|
"body": m.msg.Body,
|
|
|
|
"message_id": m.msg.MessageId,
|
|
|
|
"error": err,
|
|
|
|
"type": "goevents",
|
|
|
|
"sub_type": "producer",
|
2017-03-09 22:51:57 -03:00
|
|
|
}).Error("Error publishing message to the exchange. Retrying...")
|
2019-03-12 16:52:17 -03:00
|
|
|
|
|
|
|
p.internalQueue <- m
|
|
|
|
} else {
|
|
|
|
p.wg.Done()
|
2017-03-09 17:32:05 -03:00
|
|
|
}
|
|
|
|
}
|
2016-12-01 10:52:22 -02:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
for _, c := range p.closes {
|
|
|
|
c <- true
|
|
|
|
}
|
2016-12-01 10:52:22 -02:00
|
|
|
}
|