-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqs_receiver.go
62 lines (54 loc) · 1.45 KB
/
sqs_receiver.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
package consumer
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"log"
"os"
"time"
)
//SqsReceiver defines the struct that polls messages from AWS SQS
type SqsReceiver struct {
queueURL string
messagesChannel chan []*sqs.Message
shutdown chan os.Signal
sess *session.Session
visibilityTimeout int64
maxNumberOfMessages int64
pollDelayInMilliseconds int
}
func (r *SqsReceiver) applyBackPressure() {
time.Sleep(time.Millisecond * time.Duration(r.pollDelayInMilliseconds))
}
func (r *SqsReceiver) receiveMessages() {
queue := sqs.New(r.sess)
for {
select {
case <-r.shutdown:
log.Println("Shutting down message receiver")
close(r.messagesChannel)
return
default:
result, err := queue.ReceiveMessage(&sqs.ReceiveMessageInput{
AttributeNames: []*string{
aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
},
MessageAttributeNames: []*string{
aws.String(sqs.QueueAttributeNameAll),
},
QueueUrl: aws.String(r.queueURL),
MaxNumberOfMessages: aws.Int64(r.maxNumberOfMessages),
VisibilityTimeout: aws.Int64(r.visibilityTimeout),
})
if err != nil {
log.Println("Could not read from queue", err)
return
}
if len(result.Messages) > 0 {
messages := result.Messages
r.messagesChannel <- messages
}
r.applyBackPressure()
}
}
}