/
batch_streams.go
601 lines (542 loc) · 13.6 KB
/
batch_streams.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package red
import (
"time"
"github.com/alxarch/red/resp"
)
// XAck removes one or multiple messages from the pending entries list (PEL) of a stream consumer group.
// XACK key group ID [ID ...]
func (b *batchAPI) XAck(key, group string, id string, ids ...string) *ReplyInteger {
b.args.Key(key)
b.args.String(group)
b.args.Unique(id, ids...)
return b.doInteger("XACK")
}
// XAdd appends the specified stream entry to the stream at the specified key.
//
// XADD key ID field value [field value ...]
//
// If the key does not exist, as a side effect of running this command,
// the key is created with a stream value.
func (b *batchAPI) XAdd(key string, maxLen int64, id string, fields ...HArg) *ReplyBulkString {
b.args.Key(key)
if maxLen > 0 {
b.args.String("MAXLEN")
b.args.String("~")
b.args.Int(maxLen)
}
if id == "" {
id = "*"
}
b.args.String(id)
for i := range fields {
field := &fields[i]
b.args.String(field.Field)
b.args.Arg(field.Value)
}
return b.doBulkString("XADD")
}
// XClaim claims a pending message in a consumer group.
//
// XCLAIM key group consumer min-idle-time ID [ID ...] [IDLE ms] [TIME ms-unix-time] [RETRYCOUNT count] [FORCE] [JUSTID]
//
// Available since 5.0.0.
//
// Time complexity: O(log N) with N being the number of messages in the PEL of the consumer group.
//
// In the context of a stream consumer group, this command changes the ownership of a pending message,
// so that the new owner is the consumer specified as the command argument. Normally this is what happens:
//
// 1. There is a stream with an associated consumer group.
// 2. Some consumer A reads a message via XREADGROUP from a stream, in the context of that consumer group.
// 3. As a side effect a pending message entry is created in the pending entries list (PEL)
// of the consumer group: it means the message was delivered to a given consumer,
// but it was not yet acknowledged via XACK.
// 4. Then suddenly that consumer fails forever.
// 5. Other consumers may inspect the list of pending messages, that are stale for quite some time,
// using the XPENDING command. In order to continue processing such messages, they use XCLAIM
// to acquire the ownership of the message and continue.
//
// This dynamic is clearly explained in the [Stream intro documentation](https://redis.io/topics/streams-intro).
type XClaim struct {
Key string
Group string
Consumer string
MinIdleTime time.Duration
IDs []string
Idle time.Duration
RetryCount int
Force bool
Time time.Time
JustID bool
}
// BuildCommand implements CommandBuilder interface
func (cmd *XClaim) BuildCommand(args *ArgBuilder) string {
// XCLAIM key group consumer min-idle-time ID [ID ...] [IDLE ms] [TIME ms-unix-time] [RETRYCOUNT count] [FORCE] [JUSTID]
args.Key(cmd.Key)
args.String(cmd.Group)
args.String(cmd.Consumer)
args.Milliseconds(cmd.MinIdleTime)
args.String("ID")
args.Strings(cmd.IDs...)
if idle := cmd.Idle.Truncate(time.Millisecond); idle > 0 {
args.String("IDLE")
args.Milliseconds(cmd.Idle)
} else if !cmd.Time.IsZero() {
args.String("TIME")
args.Arg(UnixMilliseconds(cmd.Time))
}
if cmd.RetryCount > 0 {
args.String("RETRYCOUNT")
args.Int(int64(cmd.RetryCount))
}
args.Flag("FORCE", cmd.Force)
args.Flag("JUSTID", cmd.JustID)
return "XCLAIM"
}
// XDel deletes entries from a stream
func (b *batchAPI) XDel(key string, ids ...string) *ReplyInteger {
b.args.Key(key)
b.args.Strings(ids...)
return b.doInteger("XDEL")
}
// XGroupCreate creates a new consumer group associated with a stream.
// XGROUP [CREATE key groupname id-or-$]
func (b *batchAPI) XGroupCreate(key, group, id string, makeStream bool) *ReplyOK {
b.args.String("CREATE")
b.args.Key(key)
b.args.String(group)
if id == "" {
id = "$"
}
b.args.String(id)
b.args.Flag("MKSTREAM", makeStream)
return b.doSimpleStringOK("XGROUP", 0)
}
// XGroupSetID sets the consumer group last delivered ID to something else.
// XGROUP [SETID key groupname id-or-$]
func (b *batchAPI) XGroupSetID(key, group, id string) *ReplyOK {
b.args.String("SETID")
b.args.Key(key)
b.args.String(group)
if id == "" {
id = "$"
}
b.args.String(id)
return b.doSimpleStringOK("XGROUP", 0)
}
// XGroupDestroy destroys a consumer group
func (b *batchAPI) XGroupDestroy(key, group string) *ReplyInteger {
b.args.String("DESTROY")
b.args.Key(key)
b.args.String(group)
return b.doInteger("XGROUP")
}
// XGroupDelConsumer removes a specific consumer from a consumer group
func (b *batchAPI) XGroupDelConsumer(key, group, consumer string) *ReplyInteger {
b.args.String("DELCONSUMER")
b.args.Key(key)
b.args.String(group)
b.args.String(consumer)
return b.doInteger("XGROUP")
}
type XInfoStream struct {
Length int64
RadixTreeKeys int64
RadixTreeNodes int64
Groups int64
LastGeneratedID resp.BulkString
FirstEntry XInfoEntry
LastEntry XInfoEntry
}
type ReplyXInfoStream struct {
stream XInfoStream
batchReply
}
func (r *ReplyXInfoStream) Reply() (XInfoStream, error) {
return r.stream, r.err
}
func (info *XInfoStream) UnmarshalRESP(v resp.Value) error {
var values resp.SimpleStringRecord
if err := values.UnmarshalRESP(v); err != nil {
return err
}
s := XInfoStream{}
if n, ok := values["length"].(resp.Integer); ok {
s.Length = int64(n)
}
if n, ok := values["radix-tree-keys"].(resp.Integer); ok {
s.RadixTreeKeys = int64(n)
}
if n, ok := values["radix-tree-nodes"].(resp.Integer); ok {
s.RadixTreeNodes = int64(n)
}
if n, ok := values["groups"].(resp.Integer); ok {
s.Groups = int64(n)
}
if id, ok := values["last-generated-id"].(*resp.BulkString); ok {
s.LastGeneratedID = *id
}
if entry, ok := values["last-entry"].(resp.Array); ok {
if err := entry.Decode([]interface{}{
&s.LastEntry.ID,
&s.LastEntry.Values,
}); err != nil {
return err
}
}
if entry, ok := values["first-entry"].(resp.Array); ok {
if err := entry.Decode([]interface{}{
&s.FirstEntry.ID,
&s.FirstEntry.Values,
}); err != nil {
return err
}
}
return nil
}
type XInfoEntry struct {
ID string
Values map[string]string
}
func (info *XInfoEntry) UnmarshalRESP(v resp.Value) error {
return v.Decode([]interface{}{
&info.ID,
&info.Values,
})
}
type XInfoGroup struct {
Name string
Consumers int64
Pending int64
}
func (info *XInfoGroup) UnmarshalRESP(v resp.Value) error {
var values resp.SimpleStringRecord
if err := values.UnmarshalRESP(v); err != nil {
return err
}
var c XInfoGroup
if name, ok := values["name"].(*resp.BulkString); ok && name.Valid {
c.Name = name.String
}
if pending, ok := values["pending"].(resp.Integer); ok {
c.Pending = int64(pending)
}
if consumers, ok := values["consumers"].(resp.Integer); ok {
c.Consumers = int64(consumers)
}
*info = c
return nil
}
type XInfoConsumer struct {
Name string
Pending int64
Idle time.Duration
}
func (info *XInfoConsumer) UnmarshalRESP(v resp.Value) error {
var values resp.SimpleStringRecord
if err := values.UnmarshalRESP(v); err != nil {
return err
}
var c XInfoConsumer
if name, ok := values["name"].(*resp.BulkString); ok && name.Valid {
c.Name = name.String
}
if pending, ok := values["pending"].(resp.Integer); ok {
c.Pending = int64(pending)
}
if idle, ok := values["idle"].(resp.Integer); ok {
c.Idle = time.Duration(idle) * time.Millisecond
}
*info = c
return nil
}
// XInfoConsumers returns the list of every consumer in a specific consumer group
func (b *batchAPI) XInfoConsumers(key, group string) *ReplyXInfoConsumers {
b.args.String("CONSUMERS")
b.args.Key(key)
b.args.String(group)
reply := ReplyXInfoConsumers{}
reply.Bind(&reply.consumers)
b.do("XINFO", &reply.batchReply)
return &reply
}
// XInfoGroups returns all the consumer groups associated with the stream.
func (b *batchAPI) XInfoGroups(key string) *ReplyXInfoGroups {
b.args.String("GROUPS")
b.args.Key(key)
reply := ReplyXInfoGroups{}
reply.Bind(&reply.groups)
b.do("XINFO", &reply.batchReply)
return &reply
}
type ReplyXInfoGroups struct {
groups []XInfoGroup
batchReply
}
func (r *ReplyXInfoGroups) Reply() ([]XInfoGroup, error) {
return r.groups, r.err
}
type ReplyXInfoConsumers struct {
consumers []XInfoConsumer
batchReply
}
func (r *ReplyXInfoConsumers) Reply() ([]XInfoConsumer, error) {
return r.consumers, r.err
}
// XInfoStream returns general information about the stream stored at the specified key.
func (b *batchAPI) XInfoStream(key string) *ReplyXInfoStream {
b.args.String("STREAM")
b.args.Key(key)
reply := ReplyXInfoStream{}
reply.Bind(&reply.stream)
b.do("XINFO", &reply.batchReply)
return &reply
}
// XInfoHelp gets help for XINFO command
func (b *batchAPI) XInfoHelp() *ReplyBulkStringArray {
b.args.String("HELP")
return b.doBulkStringArray("XINFO")
}
// XLen returns the number of entries inside a stream.
func (b *batchAPI) XLen(key string) *ReplyInteger {
b.args.Key(key)
return b.doInteger("XLEN")
}
type StreamRecord struct {
ID string
Record []HArg
}
// UnmarshalRESP implements resp.Unmarshaler interface
func (s *StreamRecord) UnmarshalRESP(v resp.Value) error {
return v.Decode([]interface{}{
&s.ID,
(*HSet)(&s.Record),
})
}
type ReplyXRange struct {
records []StreamRecord
batchReply
}
func (r *ReplyXRange) Reply() ([]StreamRecord, error) {
return r.records, r.Err()
}
// Command implements Commander interface
func (b *batchAPI) XRange(key, group, start, end string, count int64) *ReplyXRange {
if start == "" {
start = "-"
}
if end == "" {
end = "+"
}
b.args.Key(key)
b.args.String(start)
b.args.String(end)
if count > 0 {
b.args.String("COUNT")
b.args.Int(count)
}
reply := ReplyXRange{}
reply.Bind(&reply.records)
b.do("XRANGE", &reply.batchReply)
return &reply
}
func (b *batchAPI) XRevRange(key, group, end, start string, count int64) *ReplyXRange {
if start == "" {
start = "-"
}
if end == "" {
end = "+"
}
b.args.Key(key)
b.args.String(start)
b.args.String(end)
if count > 0 {
b.args.String("COUNT")
b.args.Int(count)
}
reply := ReplyXRange{}
reply.Bind(&reply.records)
b.do("XREVRANGE", &reply.batchReply)
return &reply
}
func (b *batchAPI) XPending(key, group string) *ReplyXPendingSummary {
b.args.Key(key)
b.args.String(group)
reply := ReplyXPendingSummary{}
reply.Bind(&reply.summary)
b.do("XPENDING", &reply.batchReply)
return &reply
}
type XRange struct {
Start string
End string
Count int64
}
func (b *batchAPI) XPendingGroup(args XPending) *ReplyXPending {
b.args.Key(args.Key)
b.args.String(args.Group)
start := args.Start
if start == "" {
start = "-"
}
b.args.String(start)
end := args.End
if end == "" {
start = "+"
}
b.args.String(end)
reply := ReplyXPending{}
reply.Bind(&reply.entries)
b.do("XPENDING", &reply.batchReply)
return &reply
}
func (b *batchAPI) XPendingConsumer(args XPending) *ReplyXPending {
b.args.Key(args.Key)
b.args.String(args.Group)
start := args.Start
if start == "" {
start = "-"
}
b.args.String(start)
end := args.End
if end == "" {
start = "+"
}
b.args.String(end)
b.args.String(args.Consumer)
reply := ReplyXPending{}
reply.Bind(&reply.entries)
b.do("XPENDING", &reply.batchReply)
return &reply
}
type XPending struct {
Key string
Group string
Consumer string
Start, End string
Count int64
}
type XPendingSummary struct {
Pending int64
MinID string
MaxID string
Consumers []XPendingConsumer
}
func (x *XPendingSummary) UnmarshalRESP(v resp.Value) error {
return v.Decode([]interface{}{
&x.Pending,
&x.MinID,
&x.MaxID,
&x.Consumers,
})
}
type XPendingConsumer struct {
Name string
Pending int64
}
func (x *XPendingConsumer) UnmarshalRESP(v resp.Value) error {
return v.Decode([]interface{}{
&x.Name,
&x.Pending,
})
}
type ReplyXPendingSummary struct {
summary XPendingSummary
batchReply
}
func (r *ReplyXPendingSummary) Reply() (XPendingSummary, error) {
return r.summary, r.Err()
}
type ReplyXPending struct {
entries []XPendingEntry
batchReply
}
func (r *ReplyXPending) Reply() ([]XPendingEntry, error) {
return r.entries, r.Err()
}
type XPendingEntry struct {
ID string
Consumer string
age int64
Retries int64
}
func (x *XPendingEntry) Age() time.Duration {
return time.Duration(x.age) * time.Millisecond
}
func (x *XPendingEntry) UnmarshalRESP(v resp.Value) error {
return v.Decode([]interface{}{
&x.ID,
&x.Consumer,
&x.age,
&x.Retries,
})
}
// BuildCommand implements CommandBuilder interface
func (cmd *XPending) BuildCommand(args *ArgBuilder) string {
// XPENDING key group [start end count] [consumer]
args.Key(cmd.Key)
args.String(cmd.Group)
if cmd.Count > 0 {
start := cmd.Start
if start == "" {
start = "-"
}
end := cmd.End
if end == "" {
end = "+"
}
args.String(start)
args.String(end)
args.Int(cmd.Count)
}
if cmd.Consumer != "" {
args.String(cmd.Consumer)
}
return "XPENDING"
}
type XReadStream struct {
Key string
LastID string
}
func Stream(key, id string) XReadStream {
return XReadStream{Key: key, LastID: id}
}
type XRead struct {
Count int64
Group string
Consumer string
NoACK bool
Block time.Duration
Streams []XReadStream
}
// BuildCommand implements CommandBuilder interface
func (cmd *XRead) BuildCommand(args *ArgBuilder) string {
if cmd.Group != "" {
args.String("GROUP")
args.String(cmd.Group)
args.String(cmd.Consumer)
}
if cmd.Count > 0 {
args.String("COUNT")
args.Int(cmd.Count)
}
if cmd.Block > time.Millisecond {
args.String("BLOCK")
args.Milliseconds(cmd.Block)
}
args.Flag("NOACK", cmd.Group != "" && cmd.NoACK)
args.String("STREAMS")
for i := range cmd.Streams {
s := &cmd.Streams[i]
id := s.LastID
if id == "" {
id = "$"
}
args.Key(s.Key)
args.String(id)
}
if cmd.Group == "" {
return "XREAD"
}
return "XREADGROUP"
}