-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluffle.go
76 lines (66 loc) · 2 KB
/
fluffle.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
package fluffle
import (
"fmt"
"github.com/rabbitmq/amqp091-go"
"time"
)
// this holds a channel in turn, a single transaction
type rabbitChannel struct {
amqpChan *amqp091.Channel
isConnected bool
}
type RetryType string
type QueueProperties struct {
Name string
PrefetchCount int
}
// publish publishes messages to a reconnecting session to a fanout exchange.
// It receives from the application specific source of messages.
func publish(channels chan *rabbitChannel, qProperties QueueProperties) {
for channel := range channels {
logger.Trace("publishing...", nil)
for {
if pErr := CreateQueue(channel.amqpChan, qProperties); pErr != nil {
logger.Error("failed to initialize queue while publishing", pErr, map[string]interface{}{
"message": "cannot declare queue while publishing",
"queue_name": qProperties.Name,
"error": pErr,
})
time.Sleep(reConnectTime)
} else {
break
}
}
}
}
// subscribe consumes deliveries from an exclusive queue from a fanout exchange and sends to the application specific messages chan.
func subscribe(messages chan<- amqp091.Delivery, qProperties QueueProperties) {
for {
channel := getChannel().amqpChan
if pErr := CreateQueue(channel, qProperties); pErr != nil {
logger.Error("failed to initialize queue while publishing", pErr, nil)
return
}
if err := channel.Qos(qProperties.PrefetchCount, 0, false); err != nil {
logger.Error("RabbitMQ", err, map[string]interface{}{
"message": "cannot set QOS",
"queue_name": qProperties.Name,
"error": err,
})
return
}
deliveries, err := channel.Consume(qProperties.Name, "", false, false, false, false, nil)
if err != nil {
logger.Error("RabbitMQ", err, map[string]interface{}{
"message": fmt.Sprintf("cannot consume from: %q, %v", qProperties.Name, err),
"queue_name": qProperties.Name,
"error": err,
})
return
}
logger.Trace("subscribed...", nil)
for msg := range deliveries {
messages <- msg
}
}
}