-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer.go
164 lines (137 loc) · 4.01 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
package gosqs
import (
"context"
"fmt"
"sync"
"time"
"github.com/engelmi/go-sqs/internal"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Consumer interface {
StartListening(ctx context.Context, wg *sync.WaitGroup)
StopListening() error
}
type MessageHandler func(ctx context.Context, msg IncomingMessage) error
type consumer struct {
*internal.Client
logger logrus.Logger
handlerFunc MessageHandler
cancelFunc context.CancelFunc
maxNumberOfMessages int64
pollTimeout int64
ackTimeout time.Duration
}
func NewConsumer(config ConsumerConfig, handlerFunc MessageHandler) (Consumer, error) {
client, err := internal.NewClient(config.Region, config.Endpoint, config.Queue)
if err != nil {
return nil, err
}
return &consumer{
Client: client,
logger: config.Logger,
handlerFunc: handlerFunc,
pollTimeout: int64(config.PollTimeout.Seconds()),
ackTimeout: config.AckTimeout,
maxNumberOfMessages: config.MaxNumberOfMessages,
}, nil
}
func (c *consumer) StartListening(ctx context.Context, wg *sync.WaitGroup) {
cancelCtx, cancel := context.WithCancel(ctx)
c.cancelFunc = cancel
if wg == nil {
wg = &sync.WaitGroup{}
}
wg.Add(1)
for {
select {
case <-cancelCtx.Done():
wg.Done()
return
default:
c.listen(context.Background())
}
}
}
func (c *consumer) StopListening() error {
if c.cancelFunc == nil {
return errors.New("Consumer has not been started yet")
}
c.cancelFunc()
return nil
}
func (c *consumer) listen(ctx context.Context) {
msgs, err := c.pollMessages(ctx)
if err != nil {
c.logger.WithContext(ctx).WithError(err).Error("Error polling sqs messages")
return
}
for _, msg := range msgs {
c.processMessage(ctx, msg)
}
}
func (c *consumer) pollMessages(ctx context.Context) ([]*sqs.Message, error) {
res, err := c.Sqs.ReceiveMessageWithContext(ctx, &sqs.ReceiveMessageInput{
QueueUrl: aws.String(c.QueueUrl),
MaxNumberOfMessages: aws.Int64(c.maxNumberOfMessages),
WaitTimeSeconds: aws.Int64(c.pollTimeout),
MessageAttributeNames: aws.StringSlice([]string{"All"}),
})
if err != nil {
return nil, errors.WithStack(err)
}
return res.Messages, nil
}
func (c *consumer) processMessage(ctx context.Context, msg *sqs.Message) {
err := c.callHandlerFunc(ctx, msg)
if err != nil {
c.logger.WithContext(ctx).WithError(err).Error("Error handling sqs message")
return
}
err = c.acknowledgeMessage(ctx, msg)
if err != nil {
c.logger.WithContext(ctx).WithError(err).Error("Error acknowledging sqs message")
}
}
func (c *consumer) callHandlerFunc(ctx context.Context, msg *sqs.Message) (err error) {
if msg == nil {
c.logger.WithContext(ctx).Info("Skipping empty message")
return nil
}
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("SQS-Handler function paniced: %v", r))
}
}()
return c.handlerFunc(ctx, c.mapFromSqsMessage(msg))
}
func (c *consumer) acknowledgeMessage(ctx context.Context, msg *sqs.Message) error {
timeoutCtx, cancel := context.WithTimeout(ctx, c.ackTimeout)
defer cancel()
_, err := c.Sqs.DeleteMessageWithContext(timeoutCtx, &sqs.DeleteMessageInput{
QueueUrl: &c.QueueUrl,
ReceiptHandle: msg.ReceiptHandle,
})
return errors.WithStack(err)
}
func (c *consumer) mapFromSqsMessage(awsmsg *sqs.Message) IncomingMessage {
msgAttrValues := map[string]*IncomingMessageAttributeValue{}
for key, value := range awsmsg.MessageAttributes {
msgAttrValues[key] = &IncomingMessageAttributeValue{
DataType: value.DataType,
BinaryValue: value.BinaryValue,
StringValue: value.StringValue,
}
}
return IncomingMessage{
MessageId: awsmsg.MessageId,
ReceiptHandle: awsmsg.ReceiptHandle,
Body: awsmsg.Body,
MD5OfBody: awsmsg.MD5OfBody,
MessageAttributes: msgAttrValues,
MD5OfMessageAttributes: awsmsg.MD5OfMessageAttributes,
Attributes: awsmsg.Attributes,
}
}