-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
186 lines (157 loc) · 3.65 KB
/
queue.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
package redis_queue
import (
"context"
"errors"
"github.com/redis/go-redis/v9"
)
type redisClient interface {
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
ScriptLoad(ctx context.Context, script string) *redis.StringCmd
Del(ctx context.Context, keys ...string) *redis.IntCmd
EvalRO(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalShaRO(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
}
type Queue interface {
Pop() (Message, error)
Push(payload string) error
}
func NewCommonQueue(cfg *Config) *CommonQueue {
return &CommonQueue{
Name: cfg.Queue.Name,
ctx: context.Background(),
rdb: cfg.Redis,
retry: cfg.Queue.Retry,
}
}
// CommonQueue is a simple queue
type CommonQueue struct {
Name string
ctx context.Context
rdb redisClient
retry int
}
func (q *CommonQueue) pop() (Message, error) {
result, err := pop.Run(q.ctx, q.rdb, []string{q.Name}).Result()
if err != nil {
return nil, err
}
v, ok := result.(string)
if !ok {
return nil, ErrUnexpectedType
}
return NewCommonMessage(v), nil
}
func (q *CommonQueue) Pop() (msg Message, err error) {
msg, err = q.pop()
switch {
case errors.Is(err, redis.Nil):
return nil, nil
case err != nil:
return nil, err
}
return msg, nil
}
func (q *CommonQueue) push(payload string) error {
_, err := push.Run(q.ctx, q.rdb, []string{q.Name}, payload).Result()
if err != nil {
return err
}
return nil
}
func (q *CommonQueue) Push(payload string) (err error) {
err = q.push(payload)
switch {
case errors.Is(err, redis.Nil):
return ErrPushFailed
case err != nil:
return err
}
return nil
}
func NewSafeQueue(cfg *Config) *SafeQueue {
return &SafeQueue{
Name: cfg.Queue.Name,
AckName: cfg.Safe.AckZSetName,
ttl: cfg.Safe.TTL,
ctx: context.Background(),
rdb: cfg.Redis,
retry: cfg.Queue.Retry,
}
}
type SafeQueue struct {
Name string
AckName string
ttl int
ctx context.Context
rdb redisClient
retry int
}
func (q *SafeQueue) safePop() (*SafeMessage, error) {
result, err := ackPop.Run(q.ctx, q.rdb, []string{q.Name, q.AckName}, q.ttl).Result()
if err != nil {
return nil, err
}
v, ok := result.(string)
if !ok {
return nil, ErrUnexpectedType
}
return NewSafeMessage(v, q), nil
}
func (q *SafeQueue) SafePop() (msg *SafeMessage, err error) {
for i := 0; i < q.retry; i++ {
msg, err = q.safePop()
switch {
case errors.Is(err, redis.Nil):
return nil, nil
case errors.Is(err, ErrTimestampUpdateFailed):
continue
case errors.Is(err, ErrAckPopFailed):
continue
case err != nil:
return nil, err
}
// No error, return
break
}
return msg, nil
}
func (q *SafeQueue) pop() (Message, error) {
result, err := pop.Run(q.ctx, q.rdb, []string{q.Name}).Result()
if err != nil {
return nil, err
}
v, ok := result.(string)
if !ok {
return nil, ErrUnexpectedType
}
return NewCommonMessage(v), nil
}
func (q *SafeQueue) Pop() (msg Message, err error) {
msg, err = q.pop()
switch {
case errors.Is(err, redis.Nil):
return nil, nil
case err != nil:
return nil, err
}
return msg, nil
}
func (q *SafeQueue) push(payload string) error {
_, err := push.Run(q.ctx, q.rdb, []string{q.Name}, payload).Result()
if err != nil {
return err
}
return nil
}
func (q *SafeQueue) Push(payload string) (err error) {
err = q.push(payload)
switch {
case errors.Is(err, redis.Nil):
return ErrPushFailed
case err != nil:
return err
}
return nil
}