-
Notifications
You must be signed in to change notification settings - Fork 21
/
broker.go
198 lines (173 loc) · 4.5 KB
/
broker.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
package redis
import (
"context"
"errors"
"fmt"
"log/slog"
"strconv"
"time"
"github.com/go-redis/redis/v8"
)
const (
DefaultPollPeriod = time.Second
sortedSetKey = "tasqueue:ss:%s"
)
type Options struct {
Addrs []string
Password string
DB int
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MinIdleConns int
PollPeriod time.Duration
// OPTIONAL
// If non-zero, enqueue redis commands will be piped instead of being directly sent each time.
// The pipe will be executed every `PipePeriod` duration.
PipePeriod time.Duration
}
type Broker struct {
lo *slog.Logger
opts Options
conn redis.UniversalClient
pipe redis.Pipeliner
}
func New(o Options, lo *slog.Logger) *Broker {
if o.PollPeriod == 0 {
o.PollPeriod = DefaultPollPeriod
}
b := &Broker{
opts: o,
lo: lo,
conn: redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: o.Addrs,
DB: o.DB,
Password: o.Password,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
MinIdleConns: o.MinIdleConns,
IdleTimeout: o.IdleTimeout,
}),
}
if o.PipePeriod != 0 {
b.pipe = b.conn.Pipeline()
go b.execPipe(context.TODO())
}
return b
}
func (r *Broker) execPipe(ctx context.Context) {
tk := time.NewTicker(r.opts.PipePeriod)
for {
select {
case <-ctx.Done():
r.lo.Debug("context closed, draining redis pipe", "length", r.pipe.Len())
if _, err := r.pipe.Exec(ctx); err != nil {
r.lo.Error("could not execute redis pipe", "error", err)
}
return
case <-tk.C:
plen := r.pipe.Len()
if plen == 0 {
continue
}
r.lo.Debug("submitting redis pipe", "length", plen)
if _, err := r.pipe.Exec(ctx); err != nil {
r.lo.Error("could not execute redis pipe", "error", err)
}
}
}
}
func (r *Broker) GetPending(ctx context.Context, queue string) ([]string, error) {
rs, err := r.conn.LRange(ctx, queue, 0, -1).Result()
if err == redis.Nil {
return []string{}, nil
} else if err != nil {
return []string{}, err
}
return rs, nil
}
func (b *Broker) Enqueue(ctx context.Context, msg []byte, queue string) error {
if b.opts.PipePeriod != 0 {
return b.pipe.LPush(ctx, queue, msg).Err()
}
return b.conn.LPush(ctx, queue, msg).Err()
}
func (b *Broker) EnqueueScheduled(ctx context.Context, msg []byte, queue string, ts time.Time) error {
if b.opts.PipePeriod != 0 {
return b.pipe.ZAdd(ctx, fmt.Sprintf(sortedSetKey, queue), &redis.Z{
Score: float64(ts.UnixNano()),
Member: msg,
}).Err()
}
return b.conn.ZAdd(ctx, fmt.Sprintf(sortedSetKey, queue), &redis.Z{
Score: float64(ts.UnixNano()),
Member: msg,
}).Err()
}
func (b *Broker) Consume(ctx context.Context, work chan []byte, queue string) {
go b.consumeScheduled(ctx, queue)
for {
select {
case <-ctx.Done():
b.lo.Debug("shutting down consumer..")
return
default:
b.lo.Debug("receiving from consumer..")
res, err := b.conn.BLPop(ctx, b.opts.PollPeriod, queue).Result()
if err != nil && err.Error() != "redis: nil" {
b.lo.Error("error consuming from redis queue", "error", err)
} else if errors.Is(err, redis.Nil) {
b.lo.Debug("no tasks to consume..", "queue", queue)
} else {
msg, err := blpopResult(res)
if err != nil {
b.lo.Error("error parsing response from redis", "error", err)
return
}
work <- []byte(msg)
}
}
}
}
func (b *Broker) consumeScheduled(ctx context.Context, queue string) {
poll := time.NewTicker(b.opts.PollPeriod)
for {
select {
case <-ctx.Done():
b.lo.Debug("shutting down scheduled consumer..")
return
case <-poll.C:
b.conn.Watch(ctx, func(tx *redis.Tx) error {
// Fetch the tasks with score less than current time. These tasks have been scheduled
// to be queued.
tasks, err := tx.ZRevRangeByScore(ctx, fmt.Sprintf(sortedSetKey, queue), &redis.ZRangeBy{
Min: "0",
Max: strconv.FormatInt(time.Now().UnixNano(), 10),
Offset: 0,
Count: 1,
}).Result()
if err != nil {
return err
}
for _, task := range tasks {
if err := b.Enqueue(ctx, []byte(task), queue); err != nil {
return err
}
}
// Remove the tasks
if err := tx.ZRem(ctx, fmt.Sprintf(sortedSetKey, queue), tasks).Err(); err != nil {
return err
}
return nil
})
}
}
}
func blpopResult(rs []string) (string, error) {
if len(rs) != 2 {
return "", fmt.Errorf("BLPop result should have exactly 2 strings. Got : %v", rs)
}
return rs[1], nil
}