-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbit.go
324 lines (280 loc) · 7.57 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
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
package rabbitevents
import (
"context"
"encoding/json"
"fmt"
"log"
"sync"
"time"
"github.com/streadway/amqp"
)
const (
ExchangeTypeFanout = "fanout"
ExchangeTypeTopic = "topic"
)
type MessageHandler interface {
HandleMessage([]byte) error
}
type MessageHandleFunc func(context.Context, []byte) error
type BulkMessageHandleFunc func(context.Context, []amqp.Delivery) []*MessageError
type RabbitConfig interface {
GetUserName() string
GetPassword() string
GetHost() string
GetConnectTimeout() time.Duration
}
// ProcessDirectMessage Processes messages from the `exchange` queue. All calls bind to the same queue, and messages are load balanced over them.
// An error from the MessageHandler.HandleMessage Will caue messages not to be acked (and retried)
func ProcessDirectMessage(rabbitIni RabbitConfig, exchange, routingKey string, handler MessageHandler) {
for {
time.Sleep(time.Millisecond * 250)
conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s/", rabbitIni.GetUserName(), rabbitIni.GetPassword(), rabbitIni.GetHost()))
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:Dial Failed. Reason: %+v", err)
continue
}
ch, err := conn.Channel()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:Channel Failed. Reason: %+v", err)
err = conn.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:Channel Close Connection Failed. Reason: %+v", err)
}
continue
}
err = ch.ExchangeDeclare(
exchange, // name
"direct", // type
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // args
)
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:ExchangeDeclare Failed. Reason: %+v", err)
err = ch.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:ExchangeDeclare Close Channel Failed. Reason: %+v", err)
}
err = conn.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:ExchangeDeclare Close Connection Failed. Reason: %+v", err)
}
continue
}
q, err := ch.QueueDeclare(
fmt.Sprintf("%s:%s", exchange, routingKey), // queue name should be unique to the routingkey
false, // durable
false, // delete when unused
false, // not exclusive
false, // no-wait
nil, // args
)
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:QueueDeclare Failed. Reason: %+v", err)
err = ch.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:QueueDeclare Close Channel Failed. Reason: %+v", err)
}
err = conn.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:QueueDeclare Close Connection Failed. Reason: %+v", err)
}
continue
}
err = ch.QueueBind(
q.Name, // queue name
routingKey, // routing key
exchange, // exchange name
false, // no-wait
nil, //args
)
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:QueueBind Failed. Reason: %+v", err)
err = ch.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:QueueBind Close Channel Failed. Reason: %+v", err)
}
err = conn.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:QueueBind Close Connection Failed. Reason: %+v", err)
}
continue
}
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
forever := make(chan bool)
go func() {
defer close(forever)
for d := range msgs {
msg := d.Body
err = handler.HandleMessage(msg)
if err != nil {
log.Printf("%+v", err)
continue
}
err = d.Ack(false)
if err != nil {
log.Printf("%+v", err)
return
}
}
}()
<-forever
err = ch.Close()
if err != nil {
log.Printf("rabbit:ProcessDirectMessage:ch.Close Failed. Reason: %+v", err)
}
}
}
// SendChangeNotificationMessages Sends a bunch of messages on the change notification rabbit channel, without dialing everytime like a noob
func SendChangeNotificationMessages(ctx context.Context, messages []string, channel string, rabbitIni RabbitConfig) {
exchange := NewRabbitExchange(rabbitIni)
defer func() { _ = exchange.Close() }()
messageHandler := PackerString(exchange.SendTo(channel, ExchangeTypeFanout, false, true, ""))
err := messageHandler(ctx, messages)
if err != nil {
log.Printf("Error sending rabbit messages %+v\n", err)
}
}
type RabbitBatcher struct {
once sync.Once
stop chan struct{}
q chan string
QuiescenceTime time.Duration
MaxDelay time.Duration
MaxMessagesInBatch int
BatchSender func(context.Context, []string, string, RabbitConfig)
Config RabbitConfig
Channel string
}
var DefaultQuiescenceTime time.Duration = time.Millisecond * 5
var DefaultMaxDelay time.Duration = time.Millisecond * 50
var DefualtMaxMessagesInBatch = 1000
func (rb *RabbitBatcher) setup() {
rb.q = make(chan string, 1)
rb.stop = make(chan struct{})
if rb.MaxDelay == 0 {
rb.MaxDelay = DefaultMaxDelay
}
if rb.QuiescenceTime == 0 {
rb.QuiescenceTime = DefaultQuiescenceTime
}
if rb.BatchSender == nil {
rb.BatchSender = SendChangeNotificationMessages
}
if rb.MaxMessagesInBatch == 0 {
rb.MaxMessagesInBatch = DefualtMaxMessagesInBatch
}
}
func (rb *RabbitBatcher) Process() {
rb.once.Do(rb.setup)
var quiescence *time.Timer
var timeout *time.Timer
var timeoutC <-chan time.Time
var quiescenceC <-chan time.Time
messages := make([]string, 0)
for {
select {
case message := <-rb.q:
{
messages = append(messages, message)
if len(messages) == 1 {
timeout = time.NewTimer(rb.MaxDelay)
quiescence = time.NewTimer(rb.QuiescenceTime)
timeoutC = timeout.C
quiescenceC = quiescence.C
} else {
quiescence.Reset(rb.QuiescenceTime)
}
if len(messages) >= rb.MaxMessagesInBatch {
rb.BatchSender(context.Background(), messages, rb.Channel, rb.Config)
messages = messages[:0]
timeoutC = nil
quiescenceC = nil
}
}
case <-timeoutC:
{
rb.BatchSender(context.Background(), messages, rb.Channel, rb.Config)
messages = messages[:0]
timeoutC = nil
quiescenceC = nil
}
case <-quiescenceC:
{
rb.BatchSender(context.Background(), messages, rb.Channel, rb.Config)
messages = messages[:0]
timeoutC = nil
quiescenceC = nil
}
case <-rb.stop:
{
if len(messages) > 0 {
rb.BatchSender(context.Background(), messages, rb.Channel, rb.Config)
}
return
}
}
}
}
func (rb *RabbitBatcher) QueueMessage(message string) {
rb.once.Do(rb.setup)
rb.q <- message
}
func (rb *RabbitBatcher) Stop() {
close(rb.stop)
}
var defaultBatcher = &RabbitBatcher{}
func init() {
go defaultBatcher.Process()
}
func UnPacker(handler MessageHandleFunc) MessageHandleFunc {
return func(ctx context.Context, packed []byte) error {
if ctx == nil {
return ErrNilContext
}
if ctx.Err() != nil {
return ctx.Err()
}
messages := make([]string, 0)
err := json.Unmarshal(packed, &messages)
if err != nil {
return err
}
for _, val := range messages {
err = handler(ctx, []byte(val))
if err != nil {
return err
}
}
return nil
}
}
func PackerString(handler MessageHandleFunc) func(ctx context.Context, messages []string) error {
return func(ctx context.Context, messages []string) error {
if ctx == nil {
return ErrNilContext
}
if ctx.Err() != nil {
return ctx.Err()
}
data, err := json.Marshal(messages)
if err != nil {
return err
}
err = handler(ctx, data)
if err != nil {
return err
}
return nil
}
}