-
Notifications
You must be signed in to change notification settings - Fork 6
/
rabbitmq.go
201 lines (162 loc) · 4.36 KB
/
rabbitmq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"time"
"github.com/apex/log"
"github.com/pkg/errors"
"github.com/streadway/amqp"
)
// RabbitMQ stores rabbitmq's connection information
// it also handles disconnection (purpose of URL and QueueName storage)
type RabbitMQ struct {
URL string
Exchange string
Conn *amqp.Connection
Chann *amqp.Channel
Queue amqp.Queue
closeChann chan *amqp.Error
quitChann chan bool
}
func initRabbitMQ(config AMQP) (*RabbitMQ, error) {
rmq := &RabbitMQ{
URL: config.URL,
Exchange: config.Exchange,
}
err := rmq.load()
if err != nil {
return nil, err
}
rmq.quitChann = make(chan bool)
go rmq.handleDisconnect()
return rmq, err
}
func (rmq *RabbitMQ) load() error {
var err error
rmq.Conn, err = amqp.Dial(rmq.URL)
if err != nil {
return err
}
rmq.Chann, err = rmq.Conn.Channel()
if err != nil {
return err
}
log.Info("connection to rabbitMQ established")
rmq.closeChann = make(chan *amqp.Error)
rmq.Conn.NotifyClose(rmq.closeChann)
// declare exchange if not exist
err = rmq.Chann.ExchangeDeclare(rmq.Exchange, "direct", true, false, false, false, nil)
if err != nil {
return errors.Wrapf(err, "declaring exchange %q", rmq.Exchange)
}
args := make(amqp.Table)
args["x-delayed-type"] = "direct"
err = rmq.Chann.ExchangeDeclare("delayed", "x-delayed-message", true, false, false, false, args)
if err != nil {
return errors.Wrapf(err, "declaring exchange %q", "delayed")
}
err = declareConsumer(rmq)
if err != nil {
return err
}
return nil
}
// declareConsumer declares all queues and bindings for the consumer
func declareConsumer(rmq *RabbitMQ) error {
var err error
// rmq.Queue, err = rmq.Chann.QueueDeclare("user-created-queue", true, false, false, false, nil)
// if err != nil {
// return err
// }
// err = rmq.Chann.QueueBind(rmq.Queue.Name, "user.event.create", rmq.Exchange, false, nil)
// if err != nil {
// return err
// }
delayedQueue, err := rmq.Chann.QueueDeclare("user-published-queue", true, false, false, false, nil)
if err != nil {
return err
}
err = rmq.Chann.QueueBind(delayedQueue.Name, "user.event.publish", "delayed", false, nil)
if err != nil {
return err
}
// Set our quality of service. Since we're sharing 3 consumers on the same
// channel, we want at least 2 messages in flight.
err = rmq.Chann.Qos(2, 0, false)
if err != nil {
return err
}
published, err := rmq.Chann.Consume(
"user-published-queue",
"user-published-consumer",
false,
false,
false,
false,
nil)
if err != nil {
return err
}
go consume(published)
return nil
}
func consume(ds <-chan amqp.Delivery) {
for {
log.Debug("start listenning to consume")
select {
case d, ok := <-ds:
if !ok {
return
}
log.Infof("consume %s", string(d.Body))
d.Ack(false)
}
}
}
// Shutdown closes rabbitmq's connection
func (rmq *RabbitMQ) Shutdown() {
rmq.quitChann <- true
log.Info("shutting down rabbitMQ's connection...")
<-rmq.quitChann
}
// handleDisconnect handle a disconnection trying to reconnect every 5 seconds
func (rmq *RabbitMQ) handleDisconnect() {
for {
select {
case errChann := <-rmq.closeChann:
if errChann != nil {
log.Errorf("rabbitMQ disconnection: %v", errChann)
}
case <-rmq.quitChann:
rmq.Conn.Close()
log.Info("...rabbitMQ has been shut down")
rmq.quitChann <- true
return
}
log.Info("...trying to reconnect to rabbitMQ...")
time.Sleep(5 * time.Second)
if err := rmq.load(); err != nil {
log.Errorf("rabbitMQ error: %v", err)
}
}
}
// Publish sends the given body on the routingKey to the channel
func (rmq *RabbitMQ) Publish(routingKey string, body []byte) error {
return rmq.publish(rmq.Exchange, routingKey, body, 0)
}
// PublishWithDelay sends the given body on the routingKey to the channel with a delay
func (rmq *RabbitMQ) PublishWithDelay(routingKey string, body []byte, delay int64) error {
return rmq.publish("delayed", routingKey, body, delay)
}
func (rmq *RabbitMQ) publish(exchange string, routingKey string, body []byte, delay int64) error {
headers := make(amqp.Table)
log.Debugf("publishing to %q %q", routingKey, body)
if delay != 0 {
headers["x-delay"] = delay
}
return rmq.Chann.Publish(exchange, routingKey, false, false, amqp.Publishing{
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
ContentType: "application/json",
Body: body,
Headers: headers,
})
}