|
| 1 | +package amqp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + uuid "github.com/google/uuid" |
| 8 | + "github.com/pkg/errors" |
| 9 | + "github.com/prometheus/client_golang/prometheus" |
| 10 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 11 | + "github.com/streadway/amqp" |
| 12 | + "github.com/syncromatics/go-kit/v2/log" |
| 13 | +) |
| 14 | + |
| 15 | +// ExchangeSubscription is a service for subscribing to an AMQP exchange |
| 16 | +type ExchangeSubscription struct { |
| 17 | + amqpURL string |
| 18 | + queueName string |
| 19 | + exchangeName string |
| 20 | + |
| 21 | + connection *amqp.Connection |
| 22 | + |
| 23 | + activeConsumers prometheus.Gauge |
| 24 | + messagesConsumed prometheus.Counter |
| 25 | + messagesAcked prometheus.Counter |
| 26 | + messagesNacked prometheus.Counter |
| 27 | + messagesRejected prometheus.Counter |
| 28 | +} |
| 29 | + |
| 30 | +// NewExchangeSubscription creates a new ExchangeSubscription |
| 31 | +func NewExchangeSubscription(amqpURL string, exchangeName string) *ExchangeSubscription { |
| 32 | + queueName := fmt.Sprintf("%s.%s", exchangeName, uuid.New()) |
| 33 | + |
| 34 | + labels := prometheus.Labels{ |
| 35 | + "amqp_queue": queueName, |
| 36 | + "amqp_exchange": exchangeName, |
| 37 | + } |
| 38 | + |
| 39 | + activeConsumers := promauto.NewGauge(prometheus.GaugeOpts{ |
| 40 | + Name: "amqp_consumers_total", |
| 41 | + Help: "The total number of consumers connected to the queue that is subscribed to the exchange", |
| 42 | + ConstLabels: labels, |
| 43 | + }) |
| 44 | + |
| 45 | + messagesConsumed := promauto.NewCounter(prometheus.CounterOpts{ |
| 46 | + Name: "amqp_messages_recv_total", |
| 47 | + Help: "The total number of received messages", |
| 48 | + ConstLabels: labels, |
| 49 | + }) |
| 50 | + |
| 51 | + messagesAcked := promauto.NewCounter(prometheus.CounterOpts{ |
| 52 | + Name: "amqp_messages_ack_total", |
| 53 | + Help: "The total number of acknowledged messages", |
| 54 | + ConstLabels: labels, |
| 55 | + }) |
| 56 | + |
| 57 | + messagesNacked := promauto.NewCounter(prometheus.CounterOpts{ |
| 58 | + Name: "amqp_messages_nack_total", |
| 59 | + Help: "The total number of negatively acknowledged messages", |
| 60 | + ConstLabels: labels, |
| 61 | + }) |
| 62 | + |
| 63 | + messagesRejected := promauto.NewCounter(prometheus.CounterOpts{ |
| 64 | + Name: "amqp_messages_reject_total", |
| 65 | + Help: "The total number of rejected messages", |
| 66 | + ConstLabels: labels, |
| 67 | + }) |
| 68 | + |
| 69 | + return &ExchangeSubscription{ |
| 70 | + amqpURL: amqpURL, |
| 71 | + queueName: queueName, |
| 72 | + exchangeName: exchangeName, |
| 73 | + |
| 74 | + activeConsumers: activeConsumers, |
| 75 | + messagesConsumed: messagesConsumed, |
| 76 | + messagesAcked: messagesAcked, |
| 77 | + messagesNacked: messagesNacked, |
| 78 | + messagesRejected: messagesRejected, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// EnsureExchangeSubscriptionIsReady ensures that the necessary transient queue exists and is bound to the exchange |
| 83 | +func (es *ExchangeSubscription) EnsureExchangeSubscriptionIsReady() error { |
| 84 | + var err error |
| 85 | + es.connection, err = amqp.Dial(es.amqpURL) |
| 86 | + if err != nil { |
| 87 | + return errors.Wrap(err, "failed to connect to broker") |
| 88 | + } |
| 89 | + |
| 90 | + channel, err := es.connection.Channel() |
| 91 | + if err != nil { |
| 92 | + return errors.Wrap(err, "failed to open channel to broker") |
| 93 | + } |
| 94 | + defer channel.Close() |
| 95 | + |
| 96 | + _, err = channel.QueueDeclare(es.queueName, false, true, true, false, nil) |
| 97 | + if err != nil { |
| 98 | + return errors.Wrap(err, "failed to declare queue") |
| 99 | + } |
| 100 | + |
| 101 | + err = channel.QueueBind(es.queueName, "#", es.exchangeName, false, nil) |
| 102 | + if err != nil { |
| 103 | + return errors.Wrap(err, "failed to bind queue to exchange") |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// Message represents a message in-flight from an AMQP broker |
| 110 | +type Message struct { |
| 111 | + // Headers are the collection of metadata passed along with the Body |
| 112 | + Headers map[string]interface{} |
| 113 | + // Body is the unmodified byte array containing the message |
| 114 | + Body []byte |
| 115 | + // Ack acknowledges the successful processing of the message |
| 116 | + Ack func() error |
| 117 | + // Nack acknowledges the failed processing of the message and instructs the message to be requeued |
| 118 | + Nack func() error |
| 119 | +} |
| 120 | + |
| 121 | +// Consume starts consuming messages |
| 122 | +// |
| 123 | +// Any messages that are not explicitly Acked or Nacked by this consumer before the connection is terminated will be automatically requeued. |
| 124 | +func (es *ExchangeSubscription) Consume(outerCtx context.Context) (<-chan *Message, error) { |
| 125 | + channel, err := es.connection.Channel() |
| 126 | + if err != nil { |
| 127 | + return nil, errors.Wrap(err, "failed to open channel for consumer") |
| 128 | + } |
| 129 | + |
| 130 | + consumer := fmt.Sprintf("%s.consumer", es.queueName) |
| 131 | + rawMessages, err := channel.Consume(es.queueName, consumer, false, true, false, false, nil) |
| 132 | + if err != nil { |
| 133 | + return nil, errors.Wrap(err, "failed to start consuming messages from queue") |
| 134 | + } |
| 135 | + |
| 136 | + ctx, cancel := context.WithCancel(outerCtx) |
| 137 | + messages := make(chan *Message) |
| 138 | + go func() { |
| 139 | + es.activeConsumers.Inc() |
| 140 | + defer es.activeConsumers.Dec() |
| 141 | + for { |
| 142 | + select { |
| 143 | + case msg, ok := <-rawMessages: |
| 144 | + if !ok { |
| 145 | + cancel() |
| 146 | + continue |
| 147 | + } |
| 148 | + |
| 149 | + es.messagesConsumed.Inc() |
| 150 | + |
| 151 | + message := &Message{ |
| 152 | + Headers: msg.Headers, |
| 153 | + Body: msg.Body, |
| 154 | + Ack: func() error { |
| 155 | + es.messagesAcked.Inc() |
| 156 | + return msg.Ack(false) |
| 157 | + }, |
| 158 | + Nack: func() error { |
| 159 | + es.messagesNacked.Inc() |
| 160 | + return msg.Nack(false, true) |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + select { |
| 165 | + case messages <- message: |
| 166 | + case <-ctx.Done(): |
| 167 | + err = message.Nack() |
| 168 | + if err != nil { |
| 169 | + log.Warn("failed to nack in-flight message", |
| 170 | + "err", err, |
| 171 | + "consumer", consumer, |
| 172 | + ) |
| 173 | + } |
| 174 | + } |
| 175 | + case <-ctx.Done(): |
| 176 | + close(messages) |
| 177 | + |
| 178 | + err := channel.Cancel(consumer, false) |
| 179 | + if err != nil { |
| 180 | + log.Error("failed to cancel consumer", |
| 181 | + "err", err, |
| 182 | + "consumer", consumer, |
| 183 | + ) |
| 184 | + } |
| 185 | + |
| 186 | + err = channel.Close() |
| 187 | + if err != nil { |
| 188 | + log.Error("failed to close channel for consumer", |
| 189 | + "err", err, |
| 190 | + "consumer", consumer, |
| 191 | + ) |
| 192 | + } |
| 193 | + return |
| 194 | + } |
| 195 | + } |
| 196 | + }() |
| 197 | + |
| 198 | + return messages, nil |
| 199 | +} |
| 200 | + |
| 201 | +// ExchangeName is the name of the exchange to which this is subscribed |
| 202 | +func (es *ExchangeSubscription) ExchangeName() string { |
| 203 | + return es.exchangeName |
| 204 | +} |
0 commit comments