-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqs_reciever.go
137 lines (116 loc) · 3.02 KB
/
sqs_reciever.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"go.uber.org/zap"
)
const ChanelBufferSize = 10000
const MaxNumberOfMessages = int64(10)
const WaitTimeSeconds = int64(10)
type SQSReceiver struct {
DeleteCh chan *sqs.ReceiveMessageOutput // SQS delete channel
RecvCh chan *sqs.ReceiveMessageOutput // send channel
connectionNum int
svcs []*sqs.SQS
qURL *string
exitch chan bool
}
func newSQSReceiver(exitch chan bool, config Config) *SQSReceiver {
awsConfig := config.AWSConfig
num := config.QueueConnectionNum
logger.Info(fmt.Sprintf("recv queue: %s", config.SourceQueue),
zap.Int("queue_connection_num", num),
)
svcs := make([]*sqs.SQS, num)
for i := 0; i < num; i++ {
s := session.Must(session.NewSessionWithOptions(session.Options{
Config: awsConfig,
}))
svcs[i] = sqs.New(s)
}
// assume qURL is same
qURL, err := svcs[0].GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: aws.String(config.SourceQueue),
})
if err != nil {
logger.Fatal("NewSQSReceiver failed", zap.NamedError("error", err))
}
return &SQSReceiver{
svcs: svcs,
qURL: qURL.QueueUrl,
connectionNum: num,
DeleteCh: make(chan *sqs.ReceiveMessageOutput, ChanelBufferSize),
RecvCh: make(chan *sqs.ReceiveMessageOutput, ChanelBufferSize),
exitch: exitch,
}
}
func (s *SQSReceiver) startRecvQueue(svc *sqs.SQS) {
param := &sqs.ReceiveMessageInput{
QueueUrl: s.qURL,
MaxNumberOfMessages: aws.Int64(MaxNumberOfMessages),
WaitTimeSeconds: aws.Int64(WaitTimeSeconds),
AttributeNames: aws.StringSlice([]string{
"SentTimestamp",
}),
MessageAttributeNames: aws.StringSlice([]string{
"All",
}),
}
for {
ret, err := svc.ReceiveMessage(param)
if err != nil {
logger.Warn("sqs recv error", zap.NamedError("error", err))
continue
}
if len(ret.Messages) > 0 {
s.RecvCh <- ret
}
}
}
func (s *SQSReceiver) startDeleteQueue(svc *sqs.SQS) {
for {
select {
case msg, ok := <-s.DeleteCh:
if !ok {
logger.Warn("msgCh closed")
return
}
if err := s.Delete(msg, svc); err != nil {
logger.Warn("sqs delete error", zap.NamedError("error", err))
}
}
}
}
func (s *SQSReceiver) Delete(msgs *sqs.ReceiveMessageOutput, svc *sqs.SQS) error {
param := &sqs.DeleteMessageBatchInput{
QueueUrl: s.qURL,
}
entries := make([]*sqs.DeleteMessageBatchRequestEntry, len(msgs.Messages))
for i, msg := range msgs.Messages {
entries[i] = &sqs.DeleteMessageBatchRequestEntry{
Id: msg.MessageId,
ReceiptHandle: msg.ReceiptHandle,
}
}
param.SetEntries(entries)
_, err := svc.DeleteMessageBatch(param)
return err
}
func (s *SQSReceiver) run() {
for i := 0; i < s.connectionNum; i++ {
logger.Debug(fmt.Sprintf("recv goroutine started: %d", i))
go s.startRecvQueue(s.svcs[i])
go s.startDeleteQueue(s.svcs[i])
}
for {
select {
case <-s.exitch:
logger.Warn("exit")
close(s.RecvCh)
close(s.DeleteCh)
return
}
}
}