forked from thoas/bokchoy
/
broker_redis.go
377 lines (311 loc) · 9.41 KB
/
broker_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
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
package bokchoy
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/thoas/bokchoy/logging"
"github.com/go-redis/redis"
"github.com/pkg/errors"
)
type redisClient interface {
redis.UniversalClient
}
type redisBroker struct {
clientType string
cfg RedisConfig
clt redisClient
prefix string
logger logging.Logger
scripts map[string]string
}
const (
// Redis type
redisTypeSentinel = "sentinel"
redisTypeCluster = "cluster"
redisTypeClient = "client"
)
var redisScripts = map[string]string{
"HMSETEXPIRE": `local key = KEYS[1]
local data = ARGV
local ttl = table.remove(data, 1)
local res = redis.call('HMSET', key, unpack(data))
redis.call('EXPIRE', key, ttl)
return res`,
"ZPOPBYSCORE": `local key = ARGV[1]
local min = ARGV[2]
local max = ARGV[3]
local results = redis.call('ZRANGEBYSCORE', key, min, max)
local length = #results
if length > 0 then
redis.call('ZREMRANGEBYSCORE', key, min, max)
return results
else
return nil
end`,
"MULTIHGETALL": `local collate = function (key)
local raw_data = redis.call('HGETALL', key)
local data = {}
for idx = 1, #raw_data, 2 do
data[raw_data[idx]] = raw_data[idx + 1]
end
return data;
end
local data = {}
for _, key in ipairs(KEYS) do
data[key] = collate(key)
end
return cjson.encode(data)`,
}
// newRedisBroker initializes a new redis client.
func newRedisBroker(ctx context.Context, cfg RedisConfig, logger logging.Logger) *redisBroker {
var clt redisClient
switch cfg.Type {
case redisTypeSentinel:
clt = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: cfg.Sentinel.MasterName,
SentinelAddrs: cfg.Sentinel.SentinelAddrs,
Password: cfg.Sentinel.Password,
MaxRetries: cfg.Sentinel.MaxRetries,
DialTimeout: cfg.Sentinel.DialTimeout,
ReadTimeout: cfg.Sentinel.ReadTimeout,
WriteTimeout: cfg.Sentinel.WriteTimeout,
PoolSize: cfg.Sentinel.PoolSize,
PoolTimeout: cfg.Sentinel.PoolTimeout,
IdleTimeout: cfg.Sentinel.IdleTimeout,
MinIdleConns: cfg.Sentinel.MinIdleConns,
MaxConnAge: cfg.Sentinel.MaxConnAge,
IdleCheckFrequency: cfg.Sentinel.IdleCheckFrequency,
})
case redisTypeCluster:
clt = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: cfg.Cluster.Addrs,
Password: cfg.Cluster.Password,
MaxRetries: cfg.Cluster.MaxRetries,
DialTimeout: cfg.Cluster.DialTimeout,
ReadTimeout: cfg.Cluster.ReadTimeout,
WriteTimeout: cfg.Cluster.WriteTimeout,
PoolSize: cfg.Cluster.PoolSize,
PoolTimeout: cfg.Cluster.PoolTimeout,
IdleTimeout: cfg.Cluster.IdleTimeout,
MinIdleConns: cfg.Cluster.MinIdleConns,
MaxConnAge: cfg.Cluster.MaxConnAge,
IdleCheckFrequency: cfg.Cluster.IdleCheckFrequency,
})
default:
clt = redis.NewClient(&redis.Options{
Addr: cfg.Client.Addr,
Password: cfg.Client.Password,
DB: cfg.Client.DB,
MaxRetries: cfg.Client.MaxRetries,
DialTimeout: cfg.Client.DialTimeout,
ReadTimeout: cfg.Client.ReadTimeout,
WriteTimeout: cfg.Client.WriteTimeout,
PoolSize: cfg.Client.PoolSize,
PoolTimeout: cfg.Client.PoolTimeout,
IdleTimeout: cfg.Client.IdleTimeout,
MinIdleConns: cfg.Client.MinIdleConns,
MaxConnAge: cfg.Client.MaxConnAge,
IdleCheckFrequency: cfg.Client.IdleCheckFrequency,
})
}
return &redisBroker{
clientType: cfg.Type,
clt: clt,
prefix: cfg.Prefix,
logger: logger,
}
}
func (p redisBroker) String() string {
return fmt.Sprintf("redis (%s)", p.clientType)
}
// Initialize initializes the redis broker.
func (p *redisBroker) Initialize(ctx context.Context) error {
err := p.clt.Ping().Err()
if err != nil {
return err
}
p.scripts = make(map[string]string)
for key := range redisScripts {
sha, err := p.clt.ScriptLoad(redisScripts[key]).Result()
if err != nil {
return errors.Wrapf(err, "Unable to load script %s", key)
}
p.scripts[key] = sha
}
p.logger.Debug(ctx, fmt.Sprintf("connected to redis %s", p.cfg.Type))
return nil
}
// Ping pings the redis broker to ensure it's well connected.
func (p redisBroker) Ping() error {
_, err := p.clt.Ping().Result()
if err != nil {
return errors.Wrapf(err, "unable to ping redis %s", p.cfg.Type)
}
return nil
}
func (p redisBroker) prefixed(keys ...interface{}) string {
parts := []interface{}{p.prefix}
parts = append(parts, keys...)
return fmt.Sprint(parts...)
}
// Consume returns an array of raw data.
func (p *redisBroker) Consume(name string, taskPrefix string, eta time.Time) ([]map[string]interface{}, error) {
var (
err error
result []string
queueKey = p.prefixed(name)
)
if eta.IsZero() {
result, err = p.clt.BRPop(1*time.Second, queueKey).Result()
if err != nil && err != redis.Nil {
return nil, errors.Wrapf(err, "unable to BRPOP %s", queueKey)
}
} else {
max := fmt.Sprintf("%d", eta.UTC().Unix())
vals, err := p.clt.EvalSha(p.scripts["ZPOPBYSCORE"], nil, queueKey, "0", max).Result()
if err != nil && err != redis.Nil {
return nil, errors.Wrapf(err, "unable to ZPOPBYSCORE %s", queueKey)
}
if vals != nil {
raw, ok := vals.([]interface{})
if !ok {
return nil, errors.Wrapf(err, "unable to cast %v from ZPOPBYSCORE %s", vals, queueKey)
}
result = make([]string, len(raw))
for i := range raw {
result[i], ok = raw[i].(string)
if !ok {
return nil, errors.Wrapf(err, "unable to cast %v from ZPOPBYSCORE %s", raw[i], queueKey)
}
}
}
}
if len(result) == 0 {
return nil, nil
}
taskKeys := make([]string, 0, len(result))
for i := range result {
if result[i] == name {
continue
}
taskKeys = append(taskKeys, p.prefixed(taskPrefix, result[i]))
}
vals, err := p.clt.EvalSha(p.scripts["MULTIHGETALL"], taskKeys).Result()
if err != nil {
return nil, errors.Wrapf(err, "unable to HGETALL %s", strings.Join(taskKeys, ", "))
}
var values map[string]map[string]interface{}
err = json.Unmarshal([]byte(vals.(string)), &values)
if err != nil {
return nil, errors.Wrapf(err, "unable to unmarshal %s", strings.Join(taskKeys, ", "))
}
results := make([]map[string]interface{}, 0, len(result))
for _, data := range values {
if len(data) == 0 {
continue
}
results = append(results, data)
}
return results, nil
}
// Get returns stored raw data from task key.
func (p *redisBroker) Get(taskKey string) (map[string]interface{}, error) {
taskKey = p.prefixed(taskKey)
res, err := p.clt.HGetAll(taskKey).Result()
if err != nil {
return nil, errors.Wrapf(err, "unable to HGETALL %s", taskKey)
}
results := make(map[string]interface{})
for k, v := range res {
results[k] = v
}
return results, nil
}
// Count returns number of items from a queue name.
func (p *redisBroker) Count(queueName string) (int, error) {
queueName = p.prefixed(queueName)
var res *redis.IntCmd
value, err := p.clt.Type(queueName).Result()
if err != nil {
return 0, errors.Wrapf(err, "unable to TYPE %s", queueName)
}
switch value {
case "zset":
res = p.clt.ZCount(queueName, "-inf", "+inf")
case "list":
res = p.clt.LLen(queueName)
}
if res == nil {
return 0, nil
}
if res != nil && res.Err() != nil {
return 0, errors.Wrapf(res.Err(), "unable to LEN %s", queueName)
}
return int(res.Val()), nil
}
// Save synchronizes the stored item in redis.
func (p *redisBroker) Set(taskKey string, data map[string]interface{}, expiration time.Duration) error {
prefixedTaskKey := p.prefixed(taskKey)
if int(expiration.Seconds()) == 0 {
_, err := p.clt.HMSet(prefixedTaskKey, data).Result()
if err != nil {
return errors.Wrapf(err, "unable to HMSET %s", prefixedTaskKey)
}
return nil
}
values := []interface{}{int(expiration.Seconds())}
values = append(values, unpack(data)...)
_, err := p.clt.EvalSha(p.scripts["HMSETEXPIRE"], []string{prefixedTaskKey}, values...).Result()
if err != nil {
return errors.Wrapf(err, "unable to HMSETEXPIRE %s", prefixedTaskKey)
}
return nil
}
// Publish publishes raw data.
// it uses a hash to store the task itself
// pushes the task id to the list or a zset if the task is delayed.
func (p *redisBroker) Publish(queueName string, taskPrefix string,
taskID string, data map[string]interface{}, eta time.Time) error {
prefixedTaskKey := p.prefixed(taskPrefix, taskID)
_, err := p.clt.Pipelined(func(pipe redis.Pipeliner) error {
pipe.HMSet(prefixedTaskKey, data)
if eta.IsZero() {
pipe.RPush(p.prefixed(queueName), taskID)
} else {
// if eta is before now, then we should push this
// taskID in priority
if eta.Before(time.Now().UTC()) {
pipe.LPush(p.prefixed(queueName), taskID)
} else {
pipe.ZAdd(p.prefixed(queueName), redis.Z{
Score: float64(eta.UTC().Unix()),
Member: taskID,
})
}
}
return nil
})
if err != nil {
return errors.Wrapf(err, "unable to HMSET %s", prefixedTaskKey)
}
return nil
}
// Empty removes the redis key for a queue.
func (p *redisBroker) Empty(name string) error {
err := p.clt.Del(p.prefixed(name)).Err()
if err != nil && err != redis.Nil {
return errors.Wrapf(err, "unable to DEL %s", p.prefixed(name))
}
return nil
}
// Flush flushes the entire redis database.
func (p *redisBroker) Flush() error {
err := p.clt.FlushDB().Err()
if err != nil {
return errors.Wrap(err, "unable to FLUSHDB")
}
return nil
}
var _ Broker = (*redisBroker)(nil)