-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscribe.go
257 lines (230 loc) · 7.34 KB
/
subscribe.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
package redispubsub
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/go-redis/redis/v9"
"gocloud.dev/gcerrors"
"gocloud.dev/pubsub"
"gocloud.dev/pubsub/batcher"
"gocloud.dev/pubsub/driver"
)
var recvBatcherOpts = &batcher.Options{
// Concurrency doesn't make sense here.
MaxBatchSize: 1,
MaxHandlers: 1,
}
type subscription struct {
broker *redis.Client
group string
topic string
opts SubscriptionOptions
args *redis.XReadGroupArgs
args0 *redis.XReadGroupArgs
autoclaim *redis.XAutoClaimArgs
}
// SubscriptionOptions contains configuration for subscriptions.
type SubscriptionOptions struct {
From string // starting id ($ after tail of stream), 0 by default (from head of stream)
Consumer string // unique consumer name
NoAck bool
AutoClaimIdleTime time.Duration
}
// OpenSubscription creates a pubsub.Subscription that joins group, receiving
// messages from topics.
func OpenSubscription(broker *redis.Client, group, topic string, opts *SubscriptionOptions) (*pubsub.Subscription, error) {
ds, err := openSubscription(broker, group, topic, opts)
if err != nil {
return nil, err
}
return pubsub.NewSubscription(ds, recvBatcherOpts, nil), nil
}
// openSubscription returns the driver for OpenSubscription. This function
// exists so the test harness can get the driver interface implementation if it
// needs to.
func openSubscription(broker *redis.Client, group, topic string, opts *SubscriptionOptions) (driver.Subscription, error) {
if opts == nil {
opts = &SubscriptionOptions{}
}
if opts.From == "" {
opts.From = "0"
}
if opts.AutoClaimIdleTime == 0 {
opts.AutoClaimIdleTime = 30 * time.Minute
}
// Create a consumer group eater on the stream, and start consuming from
// the latest message (represented by $) or From id
_, err := broker.XGroupCreateMkStream(context.Background(), topic, group, opts.From).Result()
if err != nil && !strings.HasPrefix(err.Error(), "BUSYGROUP") {
return nil, err
}
// Read messages in the consumer group eater that have not been read by other consumers>
// Will block after running, name the input on the redis client XADD "example:stream" * foodId 1003 foodName Coca-Cola will get the result
xReadGroupArgs := &redis.XReadGroupArgs{
Group: group, // consumer group
Consumer: opts.Consumer, // Consumer, created on-the-fly
Streams: []string{topic, ">"}, // stream
Block: 0, // infinite waiting
NoAck: opts.NoAck, // Confirmation required
Count: 1,
}
xReadGroupArgs0 := &redis.XReadGroupArgs{
Group: group, // consumer group
Consumer: opts.Consumer, // Consumer, created on-the-fly
Streams: []string{topic, "0"}, // stream
Block: 0, // infinite waiting
NoAck: opts.NoAck, // Confirmation required
Count: 1,
}
xAutoClaimArgs := &redis.XAutoClaimArgs{
Start: "0-0",
Stream: topic,
Group: group,
MinIdle: opts.AutoClaimIdleTime,
Count: 1,
Consumer: opts.Consumer,
}
ds := &subscription{
broker: broker,
opts: *opts,
args: xReadGroupArgs,
args0: xReadGroupArgs0,
autoclaim: xAutoClaimArgs,
group: group,
topic: topic,
}
return ds, nil
}
// ReceiveBatch implements driver.Subscription.ReceiveBatch.
func (s *subscription) ReceiveBatch(ctx context.Context, maxMessages int) ([]*driver.Message, error) {
// if maxMessages > 0 {
// args.Count = int64(maxMessages)
// }
// XAUTOCLAIM identifies idle pending messages, captured by dead consumers,
// and transfers ownership of them to a consumer.
if dm, err := s.receiveAutoClaimMessage(ctx, s.autoclaim); dm != nil && err == nil {
return []*driver.Message{dm}, nil
}
// What will happen if we crash in the middle of processing messages,
// is that our messages will remain in the pending entries list,
// so we can access our history by giving XREADGROUP initially an ID of 0,
// and performing the same loop. Once providing an ID of 0 the reply
// is an empty set of messages, we know that we processed and acknowledged
// all the pending messages.
if dm, err := s.receiveNextMessage(ctx, s.args0); dm != nil && err == nil {
return []*driver.Message{dm}, nil
}
// We can start to use > as ID, in order to get the new messages
// and rejoin the consumers that are processing new things.
dm, err := s.receiveNextMessage(ctx, s.args)
if err != nil {
return nil, err
}
return []*driver.Message{dm}, nil
}
func (s *subscription) receiveAutoClaimMessage(ctx context.Context, args *redis.XAutoClaimArgs) (*driver.Message, error) {
msgs, _, err := s.broker.XAutoClaim(ctx, args).Result()
if err != nil || ctx.Err() != nil {
if err == nil {
err = ctx.Err()
}
return nil, err
}
if len(msgs) == 0 {
return nil, nil
}
msg := msgs[0]
return driverMsgFromRedisMsg(msg)
}
func (s *subscription) receiveNextMessage(ctx context.Context, args *redis.XReadGroupArgs) (*driver.Message, error) {
xStreamSlice, err := s.broker.XReadGroup(ctx, args).Result()
if err != nil || ctx.Err() != nil {
if err == nil {
err = ctx.Err()
}
return nil, err
}
if len(xStreamSlice) == 0 || len(xStreamSlice[0].Messages) == 0 {
return nil, nil
}
msg := xStreamSlice[0].Messages[0]
return driverMsgFromRedisMsg(msg)
}
func driverMsgFromRedisMsg(msg redis.XMessage) (*driver.Message, error) {
bd := []byte(msg.Values["body"].(string))
var bm map[string]string
if err := json.Unmarshal([]byte(msg.Values["headers"].(string)), &bm); err != nil {
return nil, err
}
return &driver.Message{
LoggableID: fmt.Sprintf("msg %s", msg.ID),
Body: bd,
Metadata: bm,
AckID: msg.ID,
AsFunc: func(i interface{}) bool {
if p, ok := i.(*redis.XMessage); ok {
*p = msg
return true
}
return false
},
}, nil
}
// SendAcks implements driver.Subscription.SendAcks.
func (s *subscription) SendAcks(ctx context.Context, ids []driver.AckID) error {
// Mark them all acked.
for _, id := range ids {
_, err := s.broker.XAck(ctx, s.topic, s.group, fmt.Sprint(id)).Result()
if err != nil || ctx.Err() != nil {
if err == nil {
err = ctx.Err()
}
return fmt.Errorf("ack id %s error: %w", id, err)
}
}
return nil
}
// CanNack implements driver.CanNack.
func (s *subscription) CanNack() bool {
// Nacking a single message doesn't make sense with the way Kafka maintains
// offsets.
return false
}
// SendNacks implements driver.Subscription.SendNacks.
func (s *subscription) SendNacks(ctx context.Context, ids []driver.AckID) error {
panic("unreachable")
}
// Close implements io.Closer.
func (s *subscription) Close() error {
return nil
}
// IsRetryable implements driver.Subscription.IsRetryable.
func (*subscription) IsRetryable(error) bool {
return false
}
// As implements driver.Subscription.As.
func (s *subscription) As(i interface{}) bool {
if p, ok := i.(*redis.XReadGroupArgs); ok {
*p = *s.args
return true
}
return false
}
// ErrorAs implements driver.Subscription.ErrorAs.
func (s *subscription) ErrorAs(err error, i interface{}) bool {
return errorAs(err, i)
}
// ErrorCode implements driver.Subscription.ErrorCode.
func (*subscription) ErrorCode(err error) gcerrors.ErrorCode {
switch err {
case nil:
return gcerrors.OK
case context.Canceled:
return gcerrors.Canceled
case errNotInitialized:
return gcerrors.NotFound
}
return gcerrors.Unknown
}