-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqsworker.go
281 lines (243 loc) · 6 KB
/
sqsworker.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
package sqsworker
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sns/snsiface"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
"go.uber.org/zap"
"os"
"runtime"
"sync"
)
// DefaultWorkers Number of worker goroutines to spawn, each runs the handler function
const DefaultWorkers = 1
// DefaultMaxNumberOfMessages amount of messages received by each SQS request
const DefaultMaxNumberOfMessages = 10
// DefaultVisibilityTimeout SQS visibility Timeout
const DefaultVisibilityTimeout = 60
// DefaultWaitTimeSeconds Long-polling interval for SQS
const DefaultWaitTimeSeconds = 20
// Handler interface for SQS consumers
type Processor interface {
Process(context.Context, *sqs.Message, *sns.PublishInput) error
}
// Callback which is passed result from handler on success
type Callback func(*string, error)
// Worker encapsulates the SQS consumer
type Worker struct {
QueueURL string
TopicArn string
Queue sqsiface.SQSAPI
Topic snsiface.SNSAPI
Session *session.Session
Consumers int
Logger *zap.Logger
Processor Processor
Callback Callback
Name string
done chan error
}
// WorkerConfig settings for Worker to be passed in NewWorker Contstuctor
type WorkerConfig struct {
QueueURL string
TopicArn string
// If the number of workers is 0, the number of workers defaults to runtime.NumCPU()
Workers int
Processor Processor
Callback Callback
Name string
Logger *zap.Logger
}
func (w *Worker) logError(msg string, err error) {
if w.Logger != nil {
w.Logger.Error(err.Error(),
zap.String("app", w.Name),
zap.String("msg", msg),
zap.Error(err),
)
}
}
func (w *Worker) logInfo(msg string) {
if w.Logger != nil {
w.Logger.Info(msg,
zap.String("app", w.Name),
)
}
}
func (w *Worker) deleteMessage(m *sqs.DeleteMessageInput) error {
_, err := w.Queue.DeleteMessage(m)
if err != nil {
return err
}
return nil
}
func (w *Worker) sendMessage(msg *sns.PublishInput) error {
if w.TopicArn == "" {
return nil
}
if msg.Message == nil {
return nil
}
_, err := w.Topic.Publish(msg)
return err
}
func (w *Worker) consumer(ctx context.Context, in chan *sqs.Message) {
var msgString string
deleteInput := &sqs.DeleteMessageInput{QueueUrl: &w.QueueURL}
var sendInput *sns.PublishInput
var err error
for {
select {
case <-ctx.Done():
return
case msg := <-in:
if w.Callback != nil || w.TopicArn != "" {
sendInput = &sns.PublishInput{TopicArn: &w.TopicArn, Message: &msgString}
}
err = w.Processor.Process(ctx, msg, sendInput)
if err == nil {
err = w.sendMessage(sendInput)
if err != nil {
w.logError("send message failed!", err)
}
deleteInput.ReceiptHandle = msg.ReceiptHandle
err = w.deleteMessage(deleteInput)
if err != nil {
w.logError("delete message failed!", err)
}
} else {
w.logError("handler failed!", err)
}
if w.Callback != nil {
w.Callback(sendInput.Message, err)
}
}
}
}
func (w *Worker) producer(ctx context.Context, out chan *sqs.Message) {
params := &sqs.ReceiveMessageInput{
QueueUrl: aws.String(w.QueueURL),
MaxNumberOfMessages: aws.Int64(DefaultMaxNumberOfMessages),
VisibilityTimeout: aws.Int64(DefaultVisibilityTimeout),
WaitTimeSeconds: aws.Int64(DefaultWaitTimeSeconds),
}
for {
select {
case <-ctx.Done():
return
default:
req, resp := w.Queue.ReceiveMessageRequest(params)
err := req.Send()
if err != nil {
w.logError("receive messages failed!", err)
} else {
messages := resp.Messages
if len(messages) > 0 {
for _, message := range messages {
out <- message
}
}
}
}
}
}
// Close function will send a signal to all workers to exit
func (w *Worker) Close() {
close(w.done)
}
// Run does the main consumer/producer loop
func (w *Worker) Run() {
ctx, cancel := context.WithCancel(context.Background())
messages := make(chan *sqs.Message, w.Consumers)
w.logInfo(fmt.Sprint("Staring producer"))
go func() {
w.producer(ctx, messages)
close(messages)
}()
go func() {
<-w.done
cancel()
}()
w.logInfo(fmt.Sprint("Staring consumer with ", w.Consumers, " consumers"))
// Consume messages
var wg sync.WaitGroup
for x := 0; x < w.Consumers; x++ {
wg.Add(1)
go func() {
defer wg.Done()
w.consumer(ctx, messages)
}()
}
wg.Wait()
}
// CreateQueue Create queue by name.
func CreateQueue(name string, sqsc sqsiface.SQSAPI) (string, error) {
result, err := sqsc.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(name),
})
if err != nil {
return "", err
}
return *result.QueueUrl, nil
}
// GetOrCreateQueue an SQS Queue by name.
func GetOrCreateQueue(name string, sqsc sqsiface.SQSAPI) (string, error) {
queueOut, err := sqsc.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: aws.String(name),
})
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case sqs.ErrCodeQueueDoesNotExist:
return CreateQueue(name, sqsc)
}
}
return *queueOut.QueueUrl, err
}
// GetOrCreateTopic Create SNS topic by name.
func GetOrCreateTopic(name string, snsc snsiface.SNSAPI) (string, error) {
if name == "" {
return "", nil
}
snsOut, err := snsc.CreateTopic(&sns.CreateTopicInput{
Name: aws.String(name),
})
return *snsOut.TopicArn, err
}
// NewWorker constructor for SQS Worker
func NewWorker(sess *session.Session, wc WorkerConfig) *Worker {
var logger *zap.Logger
workers := runtime.NumCPU()
var queueURL, topicARN = wc.QueueURL, wc.TopicArn
if wc.Workers != 0 {
workers = wc.Workers
}
if wc.Logger == nil {
logger, _ = zap.NewProduction()
} else {
logger = wc.Logger
}
if queueURL == "" {
queueURL = os.Getenv("QUEUE_URL")
}
if topicARN == "" {
topicARN = os.Getenv("TOPIC_ARN")
}
return &Worker{
queueURL,
topicARN,
sqs.New(sess),
sns.New(sess),
sess,
workers,
logger,
wc.Processor,
wc.Callback,
wc.Name,
make(chan error),
}
}