-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
346 lines (306 loc) · 8.6 KB
/
connect.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package piper
import (
"context"
"fmt"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go"
"log"
"os"
"sync"
"time"
)
type Connection struct {
dsn string
backoffPolicy []time.Duration
conn *amqp.Connection
serviceChannel *amqp.Channel
mu sync.RWMutex
channelPool map[ChannelPoolItemKey]*amqp.Channel
consumers map[ChannelPoolItemKey]*Consumer
publishers map[ChannelPoolItemKey]*Publisher
channelPoolMu sync.RWMutex
consumersMu sync.RWMutex
publishersMu sync.RWMutex
isClosed bool
}
func NewConnection(dsn string, backoffPolicy []time.Duration) (*Connection, error) {
conn := &Connection{
dsn: dsn,
consumers: make(map[ChannelPoolItemKey]*Consumer),
publishers: make(map[ChannelPoolItemKey]*Publisher),
backoffPolicy: backoffPolicy,
}
return conn, nil
}
func (c *Connection) NativeConn() *amqp.Connection {
c.mu.RLock()
defer c.mu.RUnlock()
return c.conn
}
func (c *Connection) Channel() (*amqp.Channel, error) {
c.mu.RLock()
defer c.mu.RUnlock()
if c.conn == nil {
return nil, errors.New("connection is not defined")
}
channel, err := c.conn.Channel()
if err != nil {
return nil, errors.Wrap(err, "open a channel")
}
return channel, nil
}
func (c *Connection) Close(ctx context.Context) error {
c.SetClosed(true)
for _, consumer := range c.consumers {
consumer.Stop(ctx)
<-consumer.Done()
}
for _, publisher := range c.publishers {
publisher.Stop(ctx)
<-publisher.Done()
}
for _, ch := range c.channelPool {
err := ch.Close()
if err != nil {
return errors.Wrap(err, "close rabbitMQ channel")
}
}
if err := c.conn.Close(); err != nil {
return errors.Wrap(err, "close rabbitMQ connection")
}
return nil
}
func (c *Connection) SetClosed(value bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.isClosed = value
}
func (c *Connection) IsClosed() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.isClosed
}
func (c *Connection) connect() error {
var err error
if c.conn, err = amqp.Dial(c.dsn); err != nil {
return errors.Wrap(err, "connect to rabbitMQ")
}
c.serviceChannel = c.getServiceChannel()
c.channelPool = make(map[ChannelPoolItemKey]*amqp.Channel)
return nil
}
func (c *Connection) Connect(ctx context.Context) error {
if !c.IsClosed() {
if err := c.connect(); err != nil {
return errors.Wrap(err, "connect")
}
}
log.Printf("[AMQP CONNECT] starting connection watcher")
go func() {
for {
select {
case <-ctx.Done():
_ = c.Close(ctx)
log.Printf("[AMQP CONNECT] connection closed")
return
case _, ok := <-c.conn.NotifyClose(make(chan *amqp.Error)):
if !ok {
if c.IsClosed() {
log.Printf("[AMQP CONNECT] connection is closed")
return
}
log.Printf("[AMQP CONNECT] connection unexpected closed")
c.mu.Lock()
var connErr error
for _, timeout := range c.backoffPolicy {
if connErr := c.connect(); connErr != nil {
log.Printf("[ERROR] [AMQP CONNECT] connection failed, trying to reconnect to rabbitMQ")
time.Sleep(timeout)
continue
}
log.Printf("[AMQP CONNECT] reconnect to rabbitMQ %s", timeout)
break
}
if connErr != nil {
log.Printf("[ERROR] [AMQP CONNECT] error reconnect: %s", connErr)
os.Exit(1)
}
c.mu.Unlock()
}
}
}
}()
return nil
}
func (c *Connection) ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error {
c.mu.RLock()
defer c.mu.RUnlock()
return c.getServiceChannel().ExchangeDeclare(name, kind, durable, autoDelete, internal, noWait, args)
}
func (c *Connection) QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) {
c.mu.RLock()
defer c.mu.RUnlock()
return c.getServiceChannel().QueueDeclare(name, durable, autoDelete, exclusive, noWait, args)
}
func (c *Connection) getServiceChannel() (ch *amqp.Channel) {
if c.serviceChannel == nil {
c.serviceChannel = c.connectChannel()
return c.serviceChannel
}
if c.serviceChannel.IsClosed() {
c.serviceChannel = c.connectChannel()
return c.serviceChannel
}
return c.serviceChannel
}
func (c *Connection) connectChannel() (ch *amqp.Channel) {
channel, err := c.conn.Channel()
if err != nil {
log.Printf("[ERROR] [AMQP CONNECT] error getServiceChannel: %s", err)
os.Exit(1)
}
return channel
}
func (c *Connection) QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error {
c.mu.RLock()
defer c.mu.RUnlock()
return c.getServiceChannel().QueueBind(name, key, exchange, noWait, args)
}
func (c *Connection) Qos(routines, prefetch int, global bool) error {
c.mu.RLock()
defer c.mu.RUnlock()
return c.getServiceChannel().Qos(routines, prefetch, global)
}
func (c *Connection) addConsumerInPoll(poolKey ChannelPoolItemKey, consumer *Consumer) {
c.consumersMu.Lock()
defer c.consumersMu.Unlock()
_, ok := c.consumers[poolKey]
if !ok {
c.consumers[poolKey] = consumer
}
}
func (c *Connection) addPublisherInPoll(poolKey ChannelPoolItemKey, publisher *Publisher) {
c.publishersMu.Lock()
defer c.publishersMu.Unlock()
_, ok := c.publishers[poolKey]
if !ok {
c.publishers[poolKey] = publisher
}
}
func (c *Connection) AddConsumer(consumer *Consumer) (<-chan amqp.Delivery, error) {
c.mu.RLock()
defer c.mu.RUnlock()
poolKey := ChannelPoolItemKey{
Name: consumer.name,
Type: "consumer",
Exchange: consumer.config.Exchange,
RoutingKey: consumer.config.RoutingKey,
Queue: consumer.config.Queue,
}
ch, err := c.GetChannelFromPool(poolKey)
if err := ch.ExchangeDeclare(
consumer.config.Exchange,
consumer.config.ExchangeKind,
true,
false,
false,
false,
nil); err != nil {
return nil, fmt.Errorf("[CONSUMER][declareExchange][%s-%s]: %s", consumer.config.Exchange, consumer.config.ExchangeKind, err)
}
if _, err := ch.QueueDeclare(
consumer.config.Queue,
true,
false,
false,
false,
nil,
); err != nil {
return nil, fmt.Errorf("[CONSUMER][createChannel][%s]: %s", consumer.config.Queue, err)
}
if err := ch.QueueBind(
consumer.config.Queue,
consumer.config.RoutingKey,
consumer.config.Exchange,
false,
nil,
); err != nil {
return nil, fmt.Errorf("[CONSUMER][queueBind][%s]: %s", consumer.config.Queue, err)
}
if err != nil {
log.Printf("[AMQP CONNECT](%s) CONSUMER GetChannelFromPool error", consumer.config.RoutingKey)
return nil, errors.Wrap(err, "get channel from pool")
}
if err := ch.Qos(consumer.config.Routines, 0, false); err != nil {
log.Printf("[AMQP CONNECT](%s) CONSUMER Qos error", consumer.config.RoutingKey)
return nil, err
}
log.Printf("[AMQP CONNECT](%s) CONSUMER add channel to pool", consumer.config.RoutingKey)
c.addConsumerInPoll(poolKey, consumer)
return ch.Consume(consumer.config.Queue, consumer.name, false, false, false, false, nil)
}
func (c *Connection) Publish(publisher *Publisher, msg amqp.Publishing) error {
c.mu.RLock()
defer c.mu.RUnlock()
poolKey := ChannelPoolItemKey{
Name: publisher.name,
Type: "publisher",
Exchange: publisher.config.Exchange,
RoutingKey: publisher.config.RoutingKey,
Queue: "",
}
ch, err := c.GetChannelFromPool(poolKey)
if err != nil {
log.Printf("[AMQP CONNECT](%s) PUBLISHER GetChannelFromPool error", publisher.config.RoutingKey)
return errors.Wrap(err, "get channel from pool")
}
c.addPublisherInPoll(poolKey, publisher)
return ch.Publish(publisher.config.Exchange, publisher.config.RoutingKey, false, false, msg)
}
func (c *Connection) GetChannelFromPool(poolKey ChannelPoolItemKey) (*amqp.Channel, error) {
c.channelPoolMu.Lock()
defer c.channelPoolMu.Unlock()
var err error
ch, ok := c.channelPool[poolKey]
if !ok {
if c.conn == nil {
return nil, errors.New("connection is not defined")
}
ch, err = c.conn.Channel()
if err != nil {
return nil, errors.Wrap(err, "create channel")
}
c.channelPool[poolKey] = ch
c.channelNotifyHandler(poolKey)
}
return ch, nil
}
func (c *Connection) channelNotifyHandler(poolKey ChannelPoolItemKey) {
ch := c.channelPool[poolKey]
go func() {
log.Printf("starting channel watcher on channel: %s", poolKey.Name)
for {
select {
default:
_, ok := <-ch.NotifyClose(make(chan *amqp.Error))
if !ok {
if c.isClosed {
log.Printf("rabbitMQ channel is closed exit")
return
}
log.Printf("rrabbitMQ channel unexpected closed")
c.channelPoolMu.Lock()
delete(c.channelPool, poolKey)
c.channelPoolMu.Unlock()
c.consumersMu.Lock()
delete(c.consumers, poolKey)
c.consumersMu.Unlock()
c.publishersMu.Lock()
delete(c.publishers, poolKey)
c.publishersMu.Unlock()
return
}
}
}
}()
}