-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq.go
136 lines (114 loc) · 2.77 KB
/
q.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
package q
// Tiny abstraction library for consuming/polling AWS SQS messages
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
)
const defaultMaxMessages = 10
const defaultVisibilityTimeout = 1
const defaultWaitTimeSeconds = 20
var ErrNotFound = errors.New("No messages found")
type Queue struct {
endpoint string
params QueueParams
sqs *sqs.SQS
stopCh chan int
}
type QueueParams struct {
MaxMessages int64
VisibilityTimeout int64
WaitTimeSeconds int64
receiveMessageInput *sqs.ReceiveMessageInput
}
type Message struct {
ID *string
ReceiptHandle *string
Body string
}
func (qq *Queue) Receive() (msgs []*Message, err error) {
ms, err := qq.sqs.ReceiveMessage(qq.params.receiveMessageInput)
for _, m := range ms.Messages {
msgs = append(msgs, &Message{
ID: m.MessageID,
ReceiptHandle: m.ReceiptHandle,
Body: *m.Body,
})
}
if len(msgs) == 0 && err == nil {
return msgs, ErrNotFound
}
return msgs, err
}
func (qq *Queue) Delete(msgs []*Message) error {
input := &sqs.DeleteMessageBatchInput{
QueueURL: aws.String(qq.endpoint),
Entries: make([]*sqs.DeleteMessageBatchRequestEntry, 0),
}
for _, msg := range msgs {
input.Entries = append(input.Entries, &sqs.DeleteMessageBatchRequestEntry{
ID: msg.ID,
ReceiptHandle: msg.ReceiptHandle,
})
}
_, err := qq.sqs.DeleteMessageBatch(input)
return err
}
func (qq *Queue) Shift() ([]*Message, error) {
msgs, err := qq.Receive()
if err != nil {
return msgs, err
}
delErr := qq.Delete(msgs)
return msgs, delErr
}
func (qq *Queue) Poll(msgCh chan<- string, errCh chan<- error) {
for {
select {
case <-qq.stopCh:
break
default:
msgs, err := qq.Shift()
if len(msgs) > 0 {
for _, msg := range msgs {
msgCh <- msg.Body
}
}
if err != nil && err != ErrNotFound {
errCh <- err
}
}
}
}
func (qq *Queue) StopPoll() {
qq.stopCh <- 1
}
func (qp *QueueParams) defaults(endpoint string) {
if qp.MaxMessages == 0 {
qp.MaxMessages = defaultMaxMessages
}
if qp.VisibilityTimeout == 0 {
qp.VisibilityTimeout = defaultVisibilityTimeout
}
if qp.WaitTimeSeconds == 0 {
qp.WaitTimeSeconds = defaultWaitTimeSeconds
}
qp.receiveMessageInput = &sqs.ReceiveMessageInput{
QueueURL: aws.String(endpoint),
MaxNumberOfMessages: aws.Long(qp.MaxMessages),
VisibilityTimeout: aws.Long(qp.VisibilityTimeout),
WaitTimeSeconds: aws.Long(qp.WaitTimeSeconds),
}
}
func New(endpoint string, region string, params QueueParams) *Queue {
params.defaults(endpoint)
return &Queue{
sqs: sqs.New(&aws.Config{
Credentials: aws.DefaultChainCredentials,
Region: region,
}),
params: params,
endpoint: endpoint,
stopCh: make(chan int),
}
}