-
Notifications
You must be signed in to change notification settings - Fork 4
/
queue_consumer.go
227 lines (196 loc) · 5.99 KB
/
queue_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
package sqsconsumer
import (
"errors"
"sync"
"time"
"github.com/Wattpad/sqsconsumer/sqsmessage"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"golang.org/x/net/context"
)
var (
ErrShutdownChannelClosed = errors.New("shutDown channel is already closed")
)
// NewConsumer creates a Consumer that uses the given SQSService to connect and invokes the handler for each message received.
func NewConsumer(s *SQSService, handler MessageHandlerFunc) *Consumer {
return &Consumer{
s: s,
handler: handler,
delayAfterReceiveError: defaultDelayAfterReceiveError,
WaitSeconds: defaultReceiveMessageWaitSeconds,
ReceiveVisibilityTimoutSeconds: defaultReceiveVisibilityTimeoutSeconds,
Logger: NoopLogger,
ExtendVisibilityTimeoutBySeconds: defaultExtendVisibilityBySeconds,
ExtendVisibilityTimeoutEvery: defaultExtendVisibilityEvery,
DeleteMessageAccumulatorTimeout: defaultDeleteAccumulatorTimeout,
DeleteMessageDrainTimeout: defaultDeleteMessageDrainTimeout,
}
}
// SetLogger sets the consumer and service loggers to a function similar to fmt.Printf
func (mf *Consumer) SetLogger(fn func(format string, args ...interface{})) {
mf.Logger = fn
mf.s.Logger = fn
}
func (mf *Consumer) startWorkers(ctx context.Context, jobs <-chan job, wg *sync.WaitGroup) {
for i := 0; i < awsBatchSizeLimit; i++ {
wg.Add(1)
go func() {
for j := range jobs {
msgCtx := sqsmessage.NewContext(ctx, j.msg)
err := mf.handler(msgCtx, aws.StringValue(j.msg.Body))
if err != nil {
mf.Logger("[%s] handler error: %s", aws.StringValue(j.msg.MessageId), err)
}
j.completed <- result{
msg: j.msg,
success: err == nil,
}
}
wg.Done()
}()
}
}
func (mf *Consumer) startBatchExtender(ctx context.Context, wg *sync.WaitGroup, del chan<- *sqs.Message, pending []*sqs.Message) chan<- result {
results := make(chan result, len(pending))
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(mf.ExtendVisibilityTimeoutEvery)
ext := NewBatchVisibilityExtender(ctx, mf.s, ticker.C, mf.ExtendVisibilityTimeoutBySeconds, pending)
left := len(pending)
for left > 0 {
select {
case r := <-results:
if r.success {
del <- r.msg
}
ext <- r.msg
left--
case <-ctx.Done():
ticker.Stop()
return
}
}
}()
return results
}
func (mf *Consumer) receiveMessages(ctx context.Context, wg *sync.WaitGroup, done <-chan struct{}, ch chan<- job, dq chan<- *sqs.Message) {
defer close(ch)
rcvParams := &sqs.ReceiveMessageInput{
QueueUrl: mf.s.URL,
MaxNumberOfMessages: aws.Int64(awsBatchSizeLimit),
WaitTimeSeconds: aws.Int64(mf.WaitSeconds),
VisibilityTimeout: aws.Int64(mf.ReceiveVisibilityTimoutSeconds),
AttributeNames: []*string{aws.String("SentTimestamp"), aws.String("ApproximateReceiveCount")},
}
for {
select {
case <-ctx.Done():
return
case <-done:
return
default:
}
resp, err := mf.s.Svc.ReceiveMessage(rcvParams)
if err != nil {
mf.Logger("Error receiving messages: %v", err)
mf.Logger("Waiting before trying again")
time.Sleep(mf.delayAfterReceiveError)
continue
}
if len(resp.Messages) == 0 {
continue
}
completed := mf.startBatchExtender(ctx, wg, dq, resp.Messages)
for _, msg := range resp.Messages {
ch <- job{
msg: msg,
completed: completed,
}
}
}
}
// Run starts the Consumer, stopping it when the given context is cancelled.
// To shut down without canceling the Context, and allow in-flight messages to drain,
// use the WithShutdownChan RunOption.
//
// If the context is canceled, the returned error is the context's error.
// If the optional shutDown channel is closed before Run is called, the returned error is ErrShutdownChannelClosed.
// If in-flight messages drain to completion after shutdown, the returned error is nil.
func (mf *Consumer) Run(ctx context.Context, opts ...RunOption) error {
ro := resolveRunOptions(opts)
select {
case <-ro.shutDown:
return ErrShutdownChannelClosed
default:
}
wg := &sync.WaitGroup{}
jobs := make(chan job)
mf.startWorkers(ctx, jobs, wg)
cleanupWG := &sync.WaitGroup{}
cleanupCtx, cleanupCancel := context.WithCancel(context.Background())
del := NewBatchDeleter(cleanupCtx, cleanupWG, mf.s, mf.DeleteMessageAccumulatorTimeout, mf.DeleteMessageDrainTimeout)
messages := make(chan job)
go mf.receiveMessages(cleanupCtx, cleanupWG, ro.shutDown, messages, del)
defer func() {
close(jobs)
wg.Wait()
cleanupCancel()
cleanupWG.Wait()
}()
for {
// Stop if the context was cancelled
select {
case <-ctx.Done():
return ctx.Err()
case msg, ok := <-messages:
if !ok {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case jobs <- msg:
}
}
}
}
func resolveRunOptions(opts []RunOption) *runOpts {
opt := &runOpts{}
for _, fn := range opts {
fn(opt)
}
return opt
}
type job struct {
msg *sqs.Message
completed chan<- result
}
type result struct {
msg *sqs.Message
success bool
}
const (
defaultDelayAfterReceiveError = 5 * time.Second
defaultReceiveVisibilityTimeoutSeconds = 30
defaultExtendVisibilityBySeconds = 60
defaultExtendVisibilityEvery = 30 * time.Second
defaultDeleteAccumulatorTimeout = 250 * time.Millisecond
defaultDeleteMessageDrainTimeout = time.Second
// AWS maximums
awsBatchSizeLimit = 10
defaultReceiveMessageWaitSeconds = 20
)
// Consumer is an SQS queue consumer
type Consumer struct {
s *SQSService
handler MessageHandlerFunc
delayAfterReceiveError time.Duration
Logger func(string, ...interface{})
WaitSeconds int64
ReceiveVisibilityTimoutSeconds int64
ExtendVisibilityTimeoutBySeconds int64
ExtendVisibilityTimeoutEvery time.Duration
DeleteMessageAccumulatorTimeout time.Duration
DeleteMessageDrainTimeout time.Duration
}