forked from TheThingsArchive/jolie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbit.go
133 lines (117 loc) · 3.19 KB
/
rabbit.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
package main
import (
"encoding/json"
"github.com/streadway/amqp"
"github.com/thethingsnetwork/server-shared"
"log"
"os"
"time"
)
const (
RABBIT_ATTEMPTS = 20
RABBIT_EXCHANGE = "messages"
)
type RabbitConsumer struct {
conn *amqp.Connection
channel *amqp.Channel
gatewayStatuses <-chan amqp.Delivery
rxPackets <-chan amqp.Delivery
}
func ConnectRabbitConsumer() (Consumer, error) {
var err error
for i := 0; i < RABBIT_ATTEMPTS; i++ {
uri := os.Getenv("AMQP_URI")
conn, err := amqp.Dial(uri)
if err != nil {
log.Printf("Failed to connect to %s: %s", uri, err.Error())
time.Sleep(time.Duration(2) * time.Second)
} else {
consumer := &RabbitConsumer{
conn: conn,
}
log.Printf("Connected to %s", uri)
return consumer, nil
}
}
return nil, err
}
func (d *RabbitConsumer) Configure() error {
c, err := d.conn.Channel()
if err != nil {
log.Printf("Failed to open channel: %s", err.Error())
return err
}
err = c.ExchangeDeclare(RABBIT_EXCHANGE, "topic", true, false, false, false, nil)
if err != nil {
log.Printf("Failed to declare exchange: %s", err.Error())
return err
}
err = c.Qos(3, 0, false)
if err != nil {
log.Printf("Failed to set quality of service: %s", err.Error())
return err
}
d.channel = c
return nil
}
func (d *RabbitConsumer) Consume() (*shared.ConsumerQueues, error) {
var err error
d.gatewayStatuses, err = d.consumeQueue("gateway.status", "gateway.status")
if err != nil {
log.Printf("Failed to consume gateway statuses: %s", err.Error())
return nil, err
}
d.rxPackets, err = d.consumeQueue("gateway.rx", "gateway.rx")
if err != nil {
log.Printf("Failed to consume RX packets: %s", err.Error())
return nil, err
}
queues := &shared.ConsumerQueues{
make(chan *shared.GatewayStatus),
make(chan *shared.RxPacket),
}
go d.handleDeliveries(queues)
return queues, nil
}
func (d *RabbitConsumer) consumeQueue(queueName, bindingKey string) (<-chan amqp.Delivery, error) {
_, err := d.channel.QueueDeclare(queueName, true, false, false, false, nil)
if err != nil {
log.Printf("Failed to declare queue: %s", err.Error())
return nil, err
}
err = d.channel.QueueBind(queueName, bindingKey, RABBIT_EXCHANGE, false, nil)
if err != nil {
log.Printf("Failed to bind queue: %s", err.Error())
return nil, err
}
deliveries, err := d.channel.Consume(queueName, "", false, false, false, false, nil)
if err != nil {
log.Printf("Failed to consume: %s", err.Error())
return nil, err
}
return deliveries, nil
}
func (d *RabbitConsumer) handleDeliveries(queues *shared.ConsumerQueues) {
for {
select {
case delivery := <-d.gatewayStatuses:
var status shared.GatewayStatus
err := json.Unmarshal(delivery.Body, &status)
if err != nil {
log.Printf("Failed to unmarshal gateway status: %s (%q)", err.Error(), delivery.Body)
continue
}
queues.GatewayStatuses <- &status
delivery.Ack(false)
case delivery := <-d.rxPackets:
var rxPacket shared.RxPacket
err := json.Unmarshal(delivery.Body, &rxPacket)
if err != nil {
log.Printf("Failed to unmarshal RX packet: %s (%q)", err.Error(), delivery.Body)
continue
}
queues.RxPackets <- &rxPacket
delivery.Ack(false)
}
}
}