forked from adjust/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
400 lines (338 loc) · 13 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
package rmq
import (
"fmt"
"log"
"strings"
"time"
"github.com/adjust/uniuri"
)
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 RedisClient
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 RedisClient) *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 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()
count, _ := queue.redisClient.SRem(queuesKey, queue.name)
return count > 0
}
func (queue *redisQueue) ReadyCount() int {
count, _ := queue.redisClient.LLen(queue.readyKey)
return count
}
func (queue *redisQueue) UnackedCount() int {
count, _ := queue.redisClient.LLen(queue.unackedKey)
return count
}
func (queue *redisQueue) RejectedCount() int {
count, _ := queue.redisClient.LLen(queue.rejectedKey)
return count
}
// 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 {
count, ok := queue.redisClient.LLen(queue.unackedKey)
if !ok {
return 0
}
unackedCount := count
for i := 0; i < unackedCount; i++ {
if _, ok := queue.redisClient.RPopLPush(queue.unackedKey, queue.readyKey); !ok {
return i
}
// debug(fmt.Sprintf("rmq queue returned unacked delivery %s %s", count, 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 {
rejectedCount, _ := queue.redisClient.LLen(queue.rejectedKey)
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++ {
_, ok := queue.redisClient.RPopLPush(queue.rejectedKey, queue.readyKey)
if !ok {
return i
}
// debug(fmt.Sprintf("rmq queue returned rejected delivery %s %s", value, queue.readyKey)) // COMMENTOUT
}
return count
}
// CloseInConnection closes the queue in the associated connection by removing all related keys
func (queue *redisQueue) CloseInConnection() {
queue.redisClient.Del(queue.unackedKey)
queue.redisClient.Del(queue.consumersKey)
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 ok := queue.redisClient.SAdd(queue.queuesKey, queue.name); !ok {
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)
}
// Timeout limits the amount of time waiting to fill an entire batch
// The timer is only started when the first message in a batch is received
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 {
return queue.redisClient.SMembers(queue.consumersKey)
}
func (queue *redisQueue) RemoveConsumer(name string) bool {
count, _ := queue.redisClient.SRem(queue.consumersKey, name)
return count > 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 ok := queue.redisClient.SAdd(queue.consumersKey, name); !ok {
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 {
count, _ := queue.redisClient.Del(queue.consumersKey)
return count
}
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++ {
value, ok := queue.redisClient.RPopLPush(queue.readyKey, queue.unackedKey)
if !ok {
// 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, value, queue)) // COMMENTOUT
queue.deliveryChan <- newDelivery(value, 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{}
for {
// Wait for first delivery
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
batch, ok = queue.batchTimeout(batchSize, batch, timeout)
consumer.Consume(batch)
if !ok {
// debug("batch channel closed") // COMMENTOUT
return
}
batch = batch[:0] // reset batch
}
}
func (queue *redisQueue) batchTimeout(batchSize int, batch []Delivery, timeout time.Duration) (fullBatch []Delivery, ok bool) {
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
select {
case <-timer.C:
// debug("batch timer fired") // COMMENTOUT
// debug(fmt.Sprintf("batch consume consume %d", len(batch))) // COMMENTOUT
return batch, true
case delivery, ok := <-queue.deliveryChan:
if !ok {
// debug("batch channel closed") // COMMENTOUT
return batch, false
}
batch = append(batch, delivery)
// debug(fmt.Sprintf("batch consume added delivery %d", len(batch))) // COMMENTOUT
if len(batch) >= batchSize {
// debug(fmt.Sprintf("batch consume wait %d < %d", len(batch), batchSize)) // COMMENTOUT
return batch, true
}
}
}
}
// return number of deleted list items
// https://www.redisgreen.net/blog/deleting-large-lists
func (queue *redisQueue) deleteRedisList(key string) int {
total, _ := queue.redisClient.LLen(key)
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, -1-batchSize)
}
return total
}
func debug(message string) {
// log.Printf("rmq debug: %s", message) // COMMENTOUT
}