forked from adjust/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
446 lines (373 loc) · 13.7 KB
/
queue.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package rmq
import (
"fmt"
"log"
"strings"
"time"
"github.com/adjust/uniuri"
"gopkg.in/redis.v3"
)
const (
connectionsKey = "rmq::connections" // Set of connection names
connectionHeartbeatTemplate = "rmq::connection::{connection}::heartbeat" // expires after {connection} died
connectionQueuesTemplate = "rmq::connection::{connection}::queues" // Set of queues consumers of {connection} are consuming
connectionQueueConsumersTemplate = "rmq::connection::{connection}::queue::[{queue}]::consumers" // Set of all consumers from {connection} consuming from {queue}
connectionQueueUnackedTemplate = "rmq::connection::{connection}::queue::[{queue}]::unacked" // List of deliveries consumers of {connection} are currently consuming
queuesKey = "rmq::queues" // Set of all open queues
queueReadyTemplate = "rmq::queue::[{queue}]::ready" // List of deliveries in that {queue} (right is first and oldest, left is last and youngest)
queueRejectedTemplate = "rmq::queue::[{queue}]::rejected" // List of rejected deliveries from that {queue}
phConnection = "{connection}" // connection name
phQueue = "{queue}" // queue name
phConsumer = "{consumer}" // consumer name (consisting of tag and token)
defaultBatchTimeout = time.Second
purgeBatchSize = 100
)
type Queue interface {
Publish(payload string) bool
PublishBytes(payload []byte) bool
SetPushQueue(pushQueue Queue)
StartConsuming(prefetchLimit int, pollDuration time.Duration) bool
StopConsuming() bool
AddConsumer(tag string, consumer Consumer) string
AddBatchConsumer(tag string, batchSize int, consumer BatchConsumer) string
AddBatchConsumerWithTimeout(tag string, batchSize int, timeout time.Duration, consumer BatchConsumer) string
PurgeReady() int
PurgeRejected() int
ReturnRejected(count int) int
ReturnAllRejected() int
Close() bool
}
type redisQueue struct {
name string
connectionName string
queuesKey string // key to list of queues consumed by this connection
consumersKey string // key to set of consumers using this connection
readyKey string // key to list of ready deliveries
rejectedKey string // key to list of rejected deliveries
unackedKey string // key to list of currently consuming deliveries
pushKey string // key to list of pushed deliveries
redisClient *redis.Client
deliveryChan chan Delivery // nil for publish channels, not nil for consuming channels
prefetchLimit int // max number of prefetched deliveries number of unacked can go up to prefetchLimit + numConsumers
pollDuration time.Duration
consumingStopped bool
}
func newQueue(name, connectionName, queuesKey string, redisClient *redis.Client) *redisQueue {
consumersKey := strings.Replace(connectionQueueConsumersTemplate, phConnection, connectionName, 1)
consumersKey = strings.Replace(consumersKey, phQueue, name, 1)
readyKey := strings.Replace(queueReadyTemplate, phQueue, name, 1)
rejectedKey := strings.Replace(queueRejectedTemplate, phQueue, name, 1)
unackedKey := strings.Replace(connectionQueueUnackedTemplate, phConnection, connectionName, 1)
unackedKey = strings.Replace(unackedKey, phQueue, name, 1)
queue := &redisQueue{
name: name,
connectionName: connectionName,
queuesKey: queuesKey,
consumersKey: consumersKey,
readyKey: readyKey,
rejectedKey: rejectedKey,
unackedKey: unackedKey,
redisClient: redisClient,
}
return queue
}
func (queue *redisQueue) String() string {
return fmt.Sprintf("[%s conn:%s]", queue.name, queue.connectionName)
}
// Publish adds a delivery with the given payload to the queue
func (queue *redisQueue) Publish(payload string) bool {
// debug(fmt.Sprintf("publish %s %s", payload, queue)) // COMMENTOUT
return !redisErrIsNil(queue.redisClient.LPush(queue.readyKey, payload))
}
// PublishBytes just casts the bytes and calls Publish
func (queue *redisQueue) PublishBytes(payload []byte) bool {
return queue.Publish(string(payload))
}
// PurgeReady removes all ready deliveries from the queue and returns the number of purged deliveries
func (queue *redisQueue) PurgeReady() int {
return queue.deleteRedisList(queue.readyKey)
}
// PurgeRejected removes all rejected deliveries from the queue and returns the number of purged deliveries
func (queue *redisQueue) PurgeRejected() int {
return queue.deleteRedisList(queue.rejectedKey)
}
// Close purges and removes the queue from the list of queues
func (queue *redisQueue) Close() bool {
queue.PurgeRejected()
queue.PurgeReady()
result := queue.redisClient.SRem(queuesKey, queue.name)
if redisErrIsNil(result) {
return false
}
return result.Val() > 0
}
func (queue *redisQueue) ReadyCount() int {
result := queue.redisClient.LLen(queue.readyKey)
if redisErrIsNil(result) {
return 0
}
return int(result.Val())
}
func (queue *redisQueue) UnackedCount() int {
result := queue.redisClient.LLen(queue.unackedKey)
if redisErrIsNil(result) {
return 0
}
return int(result.Val())
}
func (queue *redisQueue) RejectedCount() int {
result := queue.redisClient.LLen(queue.rejectedKey)
if redisErrIsNil(result) {
return 0
}
return int(result.Val())
}
// ReturnAllUnacked moves all unacked deliveries back to the ready
// queue and deletes the unacked key afterwards, returns number of returned
// deliveries
func (queue *redisQueue) ReturnAllUnacked() int {
result := queue.redisClient.LLen(queue.unackedKey)
if redisErrIsNil(result) {
return 0
}
unackedCount := int(result.Val())
for i := 0; i < unackedCount; i++ {
if redisErrIsNil(queue.redisClient.RPopLPush(queue.unackedKey, queue.readyKey)) {
return i
}
// debug(fmt.Sprintf("rmq queue returned unacked delivery %s %s", result.Val(), queue.readyKey)) // COMMENTOUT
}
return unackedCount
}
// ReturnAllRejected moves all rejected deliveries back to the ready
// list and returns the number of returned deliveries
func (queue *redisQueue) ReturnAllRejected() int {
result := queue.redisClient.LLen(queue.rejectedKey)
if redisErrIsNil(result) {
return 0
}
rejectedCount := int(result.Val())
return queue.ReturnRejected(rejectedCount)
}
// ReturnRejected tries to return count rejected deliveries back to
// the ready list and returns the number of returned deliveries
func (queue *redisQueue) ReturnRejected(count int) int {
if count == 0 {
return 0
}
for i := 0; i < count; i++ {
result := queue.redisClient.RPopLPush(queue.rejectedKey, queue.readyKey)
if redisErrIsNil(result) {
return i
}
// debug(fmt.Sprintf("rmq queue returned rejected delivery %s %s", result.Val(), queue.readyKey)) // COMMENTOUT
}
return count
}
// CloseInConnection closes the queue in the associated connection by removing all related keys
func (queue *redisQueue) CloseInConnection() {
redisErrIsNil(queue.redisClient.Del(queue.unackedKey))
redisErrIsNil(queue.redisClient.Del(queue.consumersKey))
redisErrIsNil(queue.redisClient.SRem(queue.queuesKey, queue.name))
}
func (queue *redisQueue) SetPushQueue(pushQueue Queue) {
redisPushQueue, ok := pushQueue.(*redisQueue)
if !ok {
return
}
queue.pushKey = redisPushQueue.readyKey
}
// StartConsuming starts consuming into a channel of size prefetchLimit
// must be called before consumers can be added!
// pollDuration is the duration the queue sleeps before checking for new deliveries
func (queue *redisQueue) StartConsuming(prefetchLimit int, pollDuration time.Duration) bool {
if queue.deliveryChan != nil {
return false // already consuming
}
// add queue to list of queues consumed on this connection
if redisErrIsNil(queue.redisClient.SAdd(queue.queuesKey, queue.name)) {
log.Panicf("rmq queue failed to start consuming %s", queue)
}
queue.prefetchLimit = prefetchLimit
queue.pollDuration = pollDuration
queue.deliveryChan = make(chan Delivery, prefetchLimit)
// log.Printf("rmq queue started consuming %s %d %s", queue, prefetchLimit, pollDuration)
go queue.consume()
return true
}
func (queue *redisQueue) StopConsuming() bool {
if queue.deliveryChan == nil || queue.consumingStopped {
return false // not consuming or already stopped
}
queue.consumingStopped = true
return true
}
// AddConsumer adds a consumer to the queue and returns its internal name
// panics if StartConsuming wasn't called before!
func (queue *redisQueue) AddConsumer(tag string, consumer Consumer) string {
name := queue.addConsumer(tag)
go queue.consumerConsume(consumer)
return name
}
// AddBatchConsumer is similar to AddConsumer, but for batches of deliveries
func (queue *redisQueue) AddBatchConsumer(tag string, batchSize int, consumer BatchConsumer) string {
return queue.AddBatchConsumerWithTimeout(tag, batchSize, defaultBatchTimeout, consumer)
}
func (queue *redisQueue) AddBatchConsumerWithTimeout(tag string, batchSize int, timeout time.Duration, consumer BatchConsumer) string {
name := queue.addConsumer(tag)
go queue.consumerBatchConsume(batchSize, timeout, consumer)
return name
}
func (queue *redisQueue) GetConsumers() []string {
result := queue.redisClient.SMembers(queue.consumersKey)
if redisErrIsNil(result) {
return []string{}
}
return result.Val()
}
func (queue *redisQueue) RemoveConsumer(name string) bool {
result := queue.redisClient.SRem(queue.consumersKey, name)
if redisErrIsNil(result) {
return false
}
return result.Val() > 0
}
func (queue *redisQueue) addConsumer(tag string) string {
if queue.deliveryChan == nil {
log.Panicf("rmq queue failed to add consumer, call StartConsuming first! %s", queue)
}
name := fmt.Sprintf("%s-%s", tag, uniuri.NewLen(6))
// add consumer to list of consumers of this queue
if redisErrIsNil(queue.redisClient.SAdd(queue.consumersKey, name)) {
log.Panicf("rmq queue failed to add consumer %s %s", queue, tag)
}
// log.Printf("rmq queue added consumer %s %s", queue, name)
return name
}
func (queue *redisQueue) RemoveAllConsumers() int {
result := queue.redisClient.Del(queue.consumersKey)
if redisErrIsNil(result) {
return 0
}
return int(result.Val())
}
func (queue *redisQueue) consume() {
for {
batchSize := queue.batchSize()
wantMore := queue.consumeBatch(batchSize)
if !wantMore {
time.Sleep(queue.pollDuration)
}
if queue.consumingStopped {
// log.Printf("rmq queue stopped consuming %s", queue)
return
}
}
}
func (queue *redisQueue) batchSize() int {
prefetchCount := len(queue.deliveryChan)
prefetchLimit := queue.prefetchLimit - prefetchCount
// TODO: ignore ready count here and just return prefetchLimit?
if readyCount := queue.ReadyCount(); readyCount < prefetchLimit {
return readyCount
}
return prefetchLimit
}
// consumeBatch tries to read batchSize deliveries, returns true if any and all were consumed
func (queue *redisQueue) consumeBatch(batchSize int) bool {
if batchSize == 0 {
return false
}
for i := 0; i < batchSize; i++ {
result := queue.redisClient.RPopLPush(queue.readyKey, queue.unackedKey)
if redisErrIsNil(result) {
// debug(fmt.Sprintf("rmq queue consumed last batch %s %d", queue, i)) // COMMENTOUT
return false
}
// debug(fmt.Sprintf("consume %d/%d %s %s", i, batchSize, result.Val(), queue)) // COMMENTOUT
queue.deliveryChan <- newDelivery(result.Val(), queue.unackedKey, queue.rejectedKey, queue.pushKey, queue.redisClient)
}
// debug(fmt.Sprintf("rmq queue consumed batch %s %d", queue, batchSize)) // COMMENTOUT
return true
}
func (queue *redisQueue) consumerConsume(consumer Consumer) {
for delivery := range queue.deliveryChan {
// debug(fmt.Sprintf("consumer consume %s %s", delivery, consumer)) // COMMENTOUT
consumer.Consume(delivery)
}
}
func (queue *redisQueue) consumerBatchConsume(batchSize int, timeout time.Duration, consumer BatchConsumer) {
batch := []Delivery{}
timer := time.NewTimer(timeout)
stopTimer(timer) // timer not active yet
for {
select {
case <-timer.C:
// debug("batch timer fired") // COMMENTOUT
// consume batch below
case delivery, ok := <-queue.deliveryChan:
if !ok {
// debug("batch channel closed") // COMMENTOUT
return
}
batch = append(batch, delivery)
// debug(fmt.Sprintf("batch consume added delivery %d", len(batch))) // COMMENTOUT
if len(batch) == 1 { // added first delivery
timer.Reset(timeout) // set timer to fire
}
if len(batch) < batchSize {
// debug(fmt.Sprintf("batch consume wait %d < %d", len(batch), batchSize)) // COMMENTOUT
continue
}
// consume batch below
}
// debug(fmt.Sprintf("batch consume consume %d", len(batch))) // COMMENTOUT
consumer.Consume(batch)
batch = batch[:0] // reset batch
stopTimer(timer) // stop and drain the timer if it fired in between
}
}
func stopTimer(timer *time.Timer) {
if timer.Stop() {
return
}
select {
case <-timer.C:
default:
}
}
// return number of deleted list items
// https://www.redisgreen.net/blog/deleting-large-lists
func (queue *redisQueue) deleteRedisList(key string) int {
llenResult := queue.redisClient.LLen(key)
total := int(llenResult.Val())
if total == 0 {
return 0 // nothing to do
}
// delete elements without blocking
for todo := total; todo > 0; todo -= purgeBatchSize {
// minimum of purgeBatchSize and todo
batchSize := purgeBatchSize
if batchSize > todo {
batchSize = todo
}
// remove one batch
queue.redisClient.LTrim(key, 0, int64(-1-batchSize))
}
return total
}
// redisErrIsNil returns false if there is no error, true if the result error is nil and panics if there's another error
func redisErrIsNil(result redis.Cmder) bool {
switch result.Err() {
case nil:
return false
case redis.Nil:
return true
default:
log.Panicf("rmq redis error is not nil %#v", result.Err())
return false
}
}
func debug(message string) {
// log.Printf("rmq debug: %s", message) // COMMENTOUT
}