-
Notifications
You must be signed in to change notification settings - Fork 479
/
rabbitmq.go
260 lines (213 loc) · 6.09 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
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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package rabbitmq
import (
"errors"
"fmt"
"math"
"strconv"
"time"
"github.com/streadway/amqp"
"github.com/dapr/components-contrib/bindings"
contrib_metadata "github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/logger"
)
const (
host = "host"
queueName = "queueName"
exclusive = "exclusive"
durable = "durable"
deleteWhenUnused = "deleteWhenUnused"
prefetchCount = "prefetchCount"
maxPriority = "maxPriority"
rabbitMQQueueMessageTTLKey = "x-message-ttl"
rabbitMQMaxPriorityKey = "x-max-priority"
defaultBase = 10
defaultBitSize = 0
)
// RabbitMQ allows sending/receiving data to/from RabbitMQ.
type RabbitMQ struct {
connection *amqp.Connection
channel *amqp.Channel
metadata rabbitMQMetadata
logger logger.Logger
queue amqp.Queue
}
// Metadata is the rabbitmq config.
type rabbitMQMetadata struct {
Host string `json:"host"`
QueueName string `json:"queueName"`
Exclusive bool `json:"exclusive,string"`
Durable bool `json:"durable,string"`
DeleteWhenUnused bool `json:"deleteWhenUnused,string"`
PrefetchCount int `json:"prefetchCount"`
MaxPriority *uint8 `json:"maxPriority"` // Priority Queue deactivated if nil
defaultQueueTTL *time.Duration
}
// NewRabbitMQ returns a new rabbitmq instance.
func NewRabbitMQ(logger logger.Logger) *RabbitMQ {
return &RabbitMQ{logger: logger}
}
// Init does metadata parsing and connection creation.
func (r *RabbitMQ) Init(metadata bindings.Metadata) error {
err := r.parseMetadata(metadata)
if err != nil {
return err
}
conn, err := amqp.Dial(r.metadata.Host)
if err != nil {
return err
}
ch, err := conn.Channel()
if err != nil {
return err
}
ch.Qos(r.metadata.PrefetchCount, 0, true)
r.connection = conn
r.channel = ch
q, err := r.declareQueue()
if err != nil {
return err
}
r.queue = q
return nil
}
func (r *RabbitMQ) Operations() []bindings.OperationKind {
return []bindings.OperationKind{bindings.CreateOperation}
}
func (r *RabbitMQ) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
pub := amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: req.Data,
}
contentType, ok := contrib_metadata.TryGetContentType(req.Metadata)
if ok {
pub.ContentType = contentType
}
ttl, ok, err := contrib_metadata.TryGetTTL(req.Metadata)
if err != nil {
return nil, err
}
// The default time to live has been set in the queue
// We allow overriding on each call, by setting a value in request metadata
if ok {
// RabbitMQ expects the duration in ms
pub.Expiration = strconv.FormatInt(ttl.Milliseconds(), 10)
}
priority, ok, err := contrib_metadata.TryGetPriority(req.Metadata)
if err != nil {
return nil, err
}
if ok {
pub.Priority = priority
}
err = r.channel.Publish("", r.metadata.QueueName, false, false, pub)
if err != nil {
return nil, err
}
return nil, nil
}
func (r *RabbitMQ) parseMetadata(metadata bindings.Metadata) error {
m := rabbitMQMetadata{}
if val, ok := metadata.Properties[host]; ok && val != "" {
m.Host = val
} else {
return errors.New("rabbitMQ binding error: missing host address")
}
if val, ok := metadata.Properties[queueName]; ok && val != "" {
m.QueueName = val
} else {
return errors.New("rabbitMQ binding error: missing queue Name")
}
if val, ok := metadata.Properties[durable]; ok && val != "" {
d, err := strconv.ParseBool(val)
if err != nil {
return fmt.Errorf("rabbitMQ binding error: can't parse durable field: %s", err)
}
m.Durable = d
}
if val, ok := metadata.Properties[deleteWhenUnused]; ok && val != "" {
d, err := strconv.ParseBool(val)
if err != nil {
return fmt.Errorf("rabbitMQ binding error: can't parse deleteWhenUnused field: %s", err)
}
m.DeleteWhenUnused = d
}
if val, ok := metadata.Properties[prefetchCount]; ok && val != "" {
parsedVal, err := strconv.ParseInt(val, defaultBase, defaultBitSize)
if err != nil {
return fmt.Errorf("rabbitMQ binding error: can't parse prefetchCount field: %s", err)
}
m.PrefetchCount = int(parsedVal)
}
if val, ok := metadata.Properties[exclusive]; ok && val != "" {
d, err := strconv.ParseBool(val)
if err != nil {
return fmt.Errorf("rabbitMQ binding error: can't parse exclusive field: %s", err)
}
m.Exclusive = d
}
if val, ok := metadata.Properties[maxPriority]; ok && val != "" {
parsedVal, err := strconv.ParseUint(val, defaultBase, defaultBitSize)
if err != nil {
return fmt.Errorf("rabbitMQ binding error: can't parse maxPriority field: %s", err)
}
maxPriority := uint8(parsedVal)
if parsedVal > 255 {
// Overflow
maxPriority = math.MaxUint8
}
m.MaxPriority = &maxPriority
}
ttl, ok, err := contrib_metadata.TryGetTTL(metadata.Properties)
if err != nil {
return err
}
if ok {
m.defaultQueueTTL = &ttl
}
r.metadata = m
return nil
}
func (r *RabbitMQ) declareQueue() (amqp.Queue, error) {
args := amqp.Table{}
if r.metadata.defaultQueueTTL != nil {
// Value in ms
ttl := *r.metadata.defaultQueueTTL / time.Millisecond
args[rabbitMQQueueMessageTTLKey] = int(ttl)
}
if r.metadata.MaxPriority != nil {
args[rabbitMQMaxPriorityKey] = *r.metadata.MaxPriority
}
return r.channel.QueueDeclare(r.metadata.QueueName, r.metadata.Durable, r.metadata.DeleteWhenUnused, r.metadata.Exclusive, false, args)
}
func (r *RabbitMQ) Read(handler func(*bindings.ReadResponse) ([]byte, error)) error {
msgs, err := r.channel.Consume(
r.queue.Name,
"",
false,
false,
false,
false,
nil,
)
if err != nil {
return err
}
forever := make(chan bool)
go func() {
for d := range msgs {
_, err := handler(&bindings.ReadResponse{
Data: d.Body,
})
if err == nil {
r.channel.Ack(d.DeliveryTag, false)
}
}
}()
<-forever
return nil
}