This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
/
redis.go
341 lines (303 loc) · 7.73 KB
/
redis.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
package broker
import (
"context"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/pkg/errors"
"m3o.dev/platform/service/broker"
"m3o.dev/platform/service/events"
"m3o.dev/platform/service/logger"
)
var (
errHandler = fmt.Errorf("error from handler")
readGroupTimeout = 10 * time.Second // how long to block on call to redis
)
const (
errMsgPoolTimeout = "redis: connection pool timeout"
)
type redisBroker struct {
redisClient *redis.Client
opts broker.Options
ropts Options
}
type subscriber struct {
opts broker.SubscribeOptions
broker *redisBroker
topic string
queue string
}
func (s *subscriber) Options() broker.SubscribeOptions {
return s.opts
}
func (s *subscriber) Topic() string {
return s.topic
}
func (s subscriber) Unsubscribe() error {
return s.broker.redisClient.XGroupDestroy(context.Background(), fmt.Sprintf("broker-%s", s.topic), s.queue).Err()
}
func (r *redisBroker) Init(opts ...broker.Option) error {
r.setOption(opts...)
return nil
}
func (r *redisBroker) Options() broker.Options {
return r.opts
}
func (r *redisBroker) Address() string {
return r.redisClient.Options().Addr
}
func (r *redisBroker) Connect() error {
return nil
}
func (r *redisBroker) Disconnect() error {
return r.redisClient.Close()
}
func (r *redisBroker) Publish(topic string, m *broker.Message, opts ...broker.PublishOption) error {
// validate the topic
if len(topic) == 0 {
return errors.New("missing topic")
}
topic = fmt.Sprintf("broker-%s", topic)
payload, err := r.opts.Codec.Marshal(m)
if err != nil {
return err
}
return r.redisClient.XAdd(context.Background(), &redis.XAddArgs{
Stream: topic,
Values: []string{"event", string(payload)},
MaxLen: 1,
Approx: true,
}).Err()
}
func (r *redisBroker) Subscribe(topic string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
if len(topic) == 0 {
return nil, events.ErrMissingTopic
}
opt := broker.SubscribeOptions{
Context: context.Background(),
}
for _, o := range opts {
o(&opt)
}
group := opt.Queue
if len(group) == 0 {
group = uuid.New().String()
}
err := r.consumeWithGroup(topic, group, h, opt.ErrorHandler)
if err != nil {
return nil, err
}
s := subscriber{
opts: opt,
broker: r,
topic: topic,
queue: group,
}
return &s, nil
}
func (r *redisBroker) String() string {
return "redis"
}
func (r *redisBroker) consumeWithGroup(topic, group string, h broker.Handler, eh broker.ErrorHandler) error {
topic = fmt.Sprintf("broker-%s", topic)
lastRead := "$"
if err := callWithRetry(func() error {
return r.redisClient.XGroupCreateMkStream(context.Background(), topic, group, lastRead).Err()
}, 2); err != nil {
if !strings.HasPrefix(err.Error(), "BUSYGROUP") {
return err
}
}
consumerName := uuid.New().String()
go func() {
defer func() {
// try to clean up the consumer
if err := callWithRetry(func() error {
return r.redisClient.XGroupDelConsumer(context.Background(), topic, group, consumerName).Err()
}, 2); err != nil {
logger.Errorf("Error deleting consumer %s", err)
}
}()
// only stop processing if we get an error while from the handler, in all other cases continue
start := "-"
for {
// sweep up any old pending messages
var pendingCmd *redis.XPendingExtCmd
err := callWithRetry(func() error {
pendingCmd = r.redisClient.XPendingExt(context.Background(), &redis.XPendingExtArgs{
Stream: topic,
Group: group,
Start: start,
End: "+",
Count: 50,
})
return pendingCmd.Err()
}, 2)
if err != nil && err != redis.Nil {
logger.Errorf("Error finding pending messages %s", err)
return
}
pend := pendingCmd.Val()
if len(pend) == 0 {
break
}
pendingIDs := make([]string, len(pend))
for i, p := range pend {
pendingIDs[i] = p.ID
}
var claimCmd *redis.XMessageSliceCmd
err = callWithRetry(func() error {
claimCmd = r.redisClient.XClaim(context.Background(), &redis.XClaimArgs{
Stream: topic,
Group: group,
Consumer: consumerName,
MinIdle: 60 * time.Second,
Messages: pendingIDs,
})
return claimCmd.Err()
}, 2)
if err != nil {
logger.Errorf("Error claiming message %s", err)
continue
}
msgs := claimCmd.Val()
if err := r.processMessages(msgs, topic, group, h, eh); err == errHandler {
logger.Errorf("Error processing message %s", err)
return
}
if len(pendingIDs) < 50 {
break
}
start = incrementID(pendingIDs[49])
}
for {
res := r.redisClient.XReadGroup(context.Background(), &redis.XReadGroupArgs{
Group: group,
Consumer: consumerName,
Streams: []string{topic, ">"},
Block: readGroupTimeout,
})
sl, err := res.Result()
if err != nil {
logger.Errorf("Error reading from stream %s", err)
sleepWithJitter(2 * time.Second)
continue
}
if sl == nil || len(sl) == 0 || len(sl[0].Messages) == 0 {
logger.Errorf("No data received from stream")
continue
}
if err := r.processMessages(sl[0].Messages, topic, group, h, eh); err == errHandler {
return
}
}
}()
return nil
}
// callWithRetry tries the call and reattempts uf we see a connection pool timeout error
func callWithRetry(f func() error, retries int) error {
var err error
for i := 0; i < retries; i++ {
err = f()
if err == nil {
return nil
}
if !isTimeoutError(err) {
break
}
sleepWithJitter(2 * time.Second)
}
return err
}
func sleepWithJitter(max time.Duration) {
// jitter the duration
time.Sleep(max * time.Duration(rand.Int63n(200)) / 200)
}
func isTimeoutError(err error) bool {
return err != nil && strings.Contains(err.Error(), errMsgPoolTimeout)
}
func incrementID(id string) string {
// id is of form 12345-0
parts := strings.Split(id, "-")
if len(parts) != 2 {
// not sure what to do with this
return id
}
i, err := strconv.Atoi(parts[1])
if err != nil {
// not sure what to do with this
return id
}
i++
return fmt.Sprintf("%s-%d", parts[0], i)
}
func (r *redisBroker) processMessages(msgs []redis.XMessage, topic, group string, h broker.Handler, eh broker.ErrorHandler) error {
for _, v := range msgs {
evBytes := v.Values["event"]
bStr, ok := evBytes.(string)
if !ok {
logger.Warnf("Failed to convert to bytes, discarding %s", v.ID)
r.redisClient.XAck(context.Background(), topic, group, v.ID)
continue
}
var msg broker.Message
if err := r.opts.Codec.Unmarshal([]byte(bStr), &msg); err != nil {
logger.Warnf("Failed to unmarshal event, discarding %s %s", err, v.ID)
r.redisClient.XAck(context.Background(), topic, group, v.ID)
continue
}
if err := h(&msg); err != nil {
if eh != nil {
eh(&msg, err)
}
return errHandler
}
// TODO check for error
r.redisClient.XAck(context.Background(), topic, group, v.ID)
}
return nil
}
func NewBroker(opts ...broker.Option) broker.Broker {
boptions := broker.Options{
// Default codec
Codec: Marshaler{},
Context: context.Background(),
}
rs := &redisBroker{
opts: boptions,
}
rs.setOption(opts...)
rs.runJanitor()
return rs
}
func (r *redisBroker) setOption(opts ...broker.Option) {
for _, o := range opts {
o(&r.opts)
}
// if no specific redis options passed then parse the broker address
if ropts, ok := r.opts.Context.Value(optionsKey{}).(Options); ok {
r.ropts = ropts
} else {
url, err := redis.ParseURL(r.opts.Addrs[0])
if err != nil {
panic(err)
}
r.ropts = Options{
Address: url.Addr,
User: url.Username,
Password: url.Password,
TLSConfig: url.TLSConfig,
}
}
rc := redis.NewClient(&redis.Options{
Addr: r.ropts.Address,
Username: r.ropts.User,
Password: r.ropts.Password,
TLSConfig: r.ropts.TLSConfig,
})
r.redisClient = rc
}