-
Notifications
You must be signed in to change notification settings - Fork 74
/
consumer.go
242 lines (214 loc) · 6.13 KB
/
consumer.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
package redismq
import (
"context"
"fmt"
"time"
"gopkg.in/redis.v3"
)
// Consumer are used for reading from queues
type Consumer struct {
Name string
Queue *Queue
cancel context.CancelFunc
contextCleared <-chan struct{}
}
// Get returns a single package from the queue (blocking)
func (consumer *Consumer) Get() (*Package, error) {
if consumer.HasUnacked() {
return nil, fmt.Errorf("unacked Packages found")
}
return consumer.unsafeGet()
}
// NoWaitGet returns a single package from the queue (returns nil, nil if no package in queue)
func (consumer *Consumer) NoWaitGet() (*Package, error) {
if consumer.HasUnacked() {
return nil, fmt.Errorf("unacked Packages found")
}
answer := consumer.Queue.redisClient.RPopLPush(
queueInputKey(consumer.Queue.Name),
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
)
if answer.Val() == "" {
return nil, nil
}
consumer.Queue.incrRate(
consumerWorkingRateKey(consumer.Queue.Name, consumer.Name),
1,
)
return consumer.parseRedisAnswer(answer)
}
// MultiGet returns an array of packages from the queue
func (consumer *Consumer) MultiGet(length int) ([]*Package, error) {
var collection []*Package
if consumer.HasUnacked() {
return nil, fmt.Errorf("unacked Packages found")
}
// TODO maybe use transactions for rollback in case of errors?
reqs, err := consumer.Queue.redisClient.Pipelined(func(c *redis.Pipeline) error {
c.BRPopLPush(
queueInputKey(consumer.Queue.Name),
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
0,
)
for i := 1; i < length; i++ {
c.RPopLPush(
queueInputKey(consumer.Queue.Name),
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
)
}
return nil
})
if err != nil && err != redis.Nil {
return nil, err
}
for _, answer := range reqs {
switch answer := answer.(type) {
case *redis.StringCmd:
if answer.Val() == "" {
continue
}
p, err := consumer.parseRedisAnswer(answer)
if err != nil {
return nil, err
}
p.Collection = &collection
collection = append(collection, p)
default:
return nil, err
}
}
consumer.Queue.incrRate(
consumerWorkingRateKey(consumer.Queue.Name, consumer.Name),
int64(length),
)
return collection, nil
}
// GetUnacked returns a single packages from the working queue of this consumer
func (consumer *Consumer) GetUnacked() (*Package, error) {
if !consumer.HasUnacked() {
return nil, fmt.Errorf("no unacked Packages found")
}
answer := consumer.Queue.redisClient.LIndex(
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
-1,
)
return consumer.parseRedisAnswer(answer)
}
// HasUnacked returns true if the consumers has unacked packages
func (consumer *Consumer) HasUnacked() bool {
if consumer.GetUnackedLength() != 0 {
return true
}
return false
}
// GetUnackedLength returns the number of packages in the unacked queue
func (consumer *Consumer) GetUnackedLength() int64 {
return consumer.Queue.redisClient.LLen(consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name)).Val()
}
// GetFailed returns a single packages from the failed queue of this consumer
func (consumer *Consumer) GetFailed() (*Package, error) {
answer := consumer.Queue.redisClient.RPopLPush(
queueFailedKey(consumer.Queue.Name),
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
)
consumer.Queue.incrRate(
consumerWorkingRateKey(consumer.Queue.Name, consumer.Name),
1,
)
return consumer.parseRedisAnswer(answer)
}
// ResetWorking deletes! all messages in the working queue of this consumer
func (consumer *Consumer) ResetWorking() error {
return consumer.Queue.redisClient.Del(consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name)).Err()
}
// RequeueWorking requeues all packages from working to input
func (consumer *Consumer) RequeueWorking() error {
for consumer.HasUnacked() {
p, err := consumer.GetUnacked()
if err != nil {
return err
}
p.Requeue()
}
return nil
}
func (consumer *Consumer) ackPackage(p *Package) error {
return consumer.Queue.redisClient.RPop(consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name)).Err()
}
func (consumer *Consumer) requeuePackage(p *Package) error {
answer := consumer.Queue.redisClient.RPopLPush(
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
queueInputKey(consumer.Queue.Name),
)
consumer.Queue.incrRate(queueInputRateKey(consumer.Queue.Name), 1)
return answer.Err()
}
func (consumer *Consumer) failPackage(p *Package) error {
return consumer.Queue.redisClient.RPopLPush(
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
queueFailedKey(consumer.Queue.Name),
).Err()
}
func (consumer *Consumer) startHeartbeat() {
firstWrite := make(chan struct{}, 1)
ctx, cancel := context.WithCancel(context.Background())
consumer.cancel = cancel
waitForClear := make(chan struct{}, 1)
consumer.contextCleared = waitForClear
go func() {
firstRun := true
for {
consumer.Queue.redisClient.Set(
consumerHeartbeatKey(consumer.Queue.Name, consumer.Name),
"ping",
time.Second,
)
if firstRun {
// use close instead
close(firstWrite)
firstRun = false
}
select {
case <-time.After(500 * time.Millisecond):
case <-ctx.Done():
// remove heart beat immediately
consumer.Queue.redisClient.Del(consumerHeartbeatKey(consumer.Queue.Name, consumer.Name))
close(waitForClear)
return
}
}
}()
<-firstWrite
return
}
func (consumer *Consumer) Quit() {
if consumer.cancel == nil {
return
}
consumer.cancel()
// wait until heart beat mark is removed
<-consumer.contextCleared
consumer.cancel = nil
}
func (consumer *Consumer) parseRedisAnswer(answer *redis.StringCmd) (*Package, error) {
if answer.Err() != nil {
return nil, answer.Err()
}
p, err := unmarshalPackage(answer.Val(), consumer.Queue, consumer)
if err != nil {
return nil, err
}
return p, nil
}
func (consumer *Consumer) unsafeGet() (*Package, error) {
answer := consumer.Queue.redisClient.BRPopLPush(
queueInputKey(consumer.Queue.Name),
consumerWorkingQueueKey(consumer.Queue.Name, consumer.Name),
0,
)
consumer.Queue.incrRate(
consumerWorkingRateKey(consumer.Queue.Name, consumer.Name),
1,
)
return consumer.parseRedisAnswer(answer)
}