-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqs.go
460 lines (370 loc) · 11.4 KB
/
sqs.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//
// gosqs - Go packages to interact with the Amazon SQS Web Services.
//
// depends on https://wiki.ubuntu.com/goamz
//
//
// Written by Prudhvi Krishna Surapaneni <[email protected]>
// Extended by Fabrizio Milo <[email protected]>
//
package sqs
import (
"net/http"
"net/http/httputil"
"encoding/xml"
"net/url"
"time"
"fmt"
"log"
"strconv"
"launchpad.net/goamz/aws"
"errors"
)
const debug = false
// The SQS type encapsulates operation with an SQS region.
type SQS struct {
aws.Auth
aws.Region
private byte // Reserve the right of using private data.
}
// NewFrom Create A new SQS Client given an access and secret Key
// region must be one of "us.east, us.west, eu.west"
func NewFrom(accessKey, secretKey, region string) (*SQS, error) {
auth := aws.Auth{AccessKey:accessKey, SecretKey:secretKey}
aws_region := aws.USEast
switch region {
case "us.east":
aws_region = aws.USEast
case "us.west":
aws_region = aws.USWest
case "eu.west":
aws_region = aws.EUWest
default:
return nil, errors.New(fmt.Sprintf("Unknow/Unsupported region %s", region))
}
aws_sqs := New(auth, aws_region)
return aws_sqs, nil
}
// NewFrom Create A new SQS Client from an exisisting aws.Auth
func New(auth aws.Auth, region aws.Region) *SQS {
return &SQS{auth, region, 0}
}
// Queue Reference to a Queue
type Queue struct {
SQS *SQS
Url string
}
type CreateQueueResponse struct {
QueueUrl string `xml:"CreateQueueResult>QueueUrl"`
ResponseMetadata ResponseMetadata
}
type GetQueueUrlResponse struct {
QueueUrl string `xml:"GetQueueUrlResult>QueueUrl"`
ResponseMetadata ResponseMetadata
}
type ListQueuesResponse struct {
QueueUrl []string `xml:"ListQueuesResult>QueueUrl"`
ResponseMetadata ResponseMetadata
}
type DeleteMessageResponse struct {
ResponseMetadata ResponseMetadata
}
type DeleteQueueResponse struct {
ResponseMetadata ResponseMetadata
}
type SendMessageResponse struct {
MD5 string `xml:"SendMessageResult>MD5OfMessageBody"`
Id string `xml:"SendMessageResult>MessageId"`
ResponseMetadata ResponseMetadata
}
type ReceiveMessageResponse struct {
Messages []Message `xml:"ReceiveMessageResult>Message"`
ResponseMetadata ResponseMetadata
}
type Message struct {
MessageId string `xml:"MessageId"`
Body string `xml:"Body"`
MD5OfBody string `xml:"MD5OfBody"`
ReceiptHandle string `xml:"ReceiptHandle"`
Attribute []Attribute `xml:"Attribute"`
}
type Attribute struct {
Name string `xml:"ReceiveMessageResult>Message>Attribute>Name"`
Value string `xml:"ReceiveMessageResult>Message>Attribute>Value"`
}
type ChangeMessageVisibilityResponse struct {
ResponseMetadata ResponseMetadata
}
type QueueAttribute struct {
Name string `xml:"Name"`
Value string `xml:"Value"`
}
type GetQueueAttributesResponse struct {
Attributes []QueueAttribute `xml:"GetQueueAttributesResult>Attribute"`
ResponseMetadata ResponseMetadata
}
type ResponseMetadata struct {
RequestId string
BoxUsage float64
}
type Error struct {
StatusCode int
Code string
Message string
RequestId string
}
func (err *Error) Error() string {
if err.Code == "" {
return err.Message
}
return fmt.Sprintf("%s (%s)", err.Message, err.Code)
}
func (err *Error) String() string {
return err.Message
}
type xmlErrors struct {
RequestId string
Errors []Error `xml:"Errors>Error"`
Error Error
}
// CreateQueue create a queue with a specific name
func (s *SQS) CreateQueue(queueName string) (*Queue, error) {
return s.CreateQueueWithTimeout(queueName, 30)
}
// CreateQueue create a queue with a specific name and a timeout
func (s *SQS) CreateQueueWithTimeout(queueName string, timeout int) (q *Queue, err error) {
resp, err := s.newQueue(queueName, timeout)
if err != nil {
return nil, err
}
q = &Queue{SQS:s, Url:resp.QueueUrl}
return
}
// GetQueue get a reference to the given quename
func (s *SQS) GetQueue(queueName string) (*Queue, error) {
var q *Queue
resp, err := s.getQueueUrl(queueName)
if err != nil {
return q, err
}
q = &Queue{SQS:s, Url:resp.QueueUrl}
return q, nil
}
func (s *SQS) QueueFromArn(queueUrl string) (q *Queue) {
q = &Queue{SQS:s, Url:queueUrl}
return
}
func (s *SQS) getQueueUrl(queueName string) (resp *GetQueueUrlResponse, err error) {
resp = &GetQueueUrlResponse{}
params := makeParams("GetQueueUrl")
params["QueueName"] = queueName
err = s.query("", params, resp)
return resp, err
}
func (s *SQS) newQueue(queueName string, timeout int) (resp *CreateQueueResponse, err error) {
resp = &CreateQueueResponse{}
params := makeParams("CreateQueue")
params["QueueName"] = queueName
params["DefaultVisibilityTimeout"] = strconv.Itoa(timeout)
err = s.query("", params, resp)
return
}
func (s *SQS) ListQueues(QueueNamePrefix string) (resp *ListQueuesResponse, err error) {
resp = &ListQueuesResponse{}
params := makeParams("ListQueues")
if QueueNamePrefix != "" {
params["QueueNamePrefix"] = QueueNamePrefix
}
err = s.query("", params, resp)
return
}
func (q *Queue) Delete() (resp *DeleteQueueResponse, err error) {
resp = &DeleteQueueResponse{}
params := makeParams("DeleteQueue")
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) SendMessage(MessageBody string) (resp *SendMessageResponse, err error) {
resp = &SendMessageResponse{}
params := makeParams("SendMessage")
params["MessageBody"] = MessageBody
err = q.SQS.query(q.Url, params, resp)
return
}
// ReceiveMessageWithVisibilityTimeout
func (q *Queue) ReceiveMessageWithVisibilityTimeout(MaxNumberOfMessages, VisibilityTimeoutSec int) (resp *ReceiveMessageResponse, err error) {
resp = &ReceiveMessageResponse{}
params := makeParams("ReceiveMessage")
params["AttributeName"] = "All"
params["MaxNumberOfMessages"] = strconv.Itoa(MaxNumberOfMessages)
params["VisibilityTimeout"] = strconv.Itoa(VisibilityTimeoutSec)
err = q.SQS.query(q.Url, params, resp)
return
}
// ReceiveMessage
func (q *Queue) ReceiveMessage(MaxNumberOfMessages int) (resp *ReceiveMessageResponse, err error) {
resp = &ReceiveMessageResponse{}
params := makeParams("ReceiveMessage")
params["AttributeName"] = "All"
params["MaxNumberOfMessages"] = strconv.Itoa(MaxNumberOfMessages)
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) ChangeMessageVisibility(M *Message, VisibilityTimeout int) (resp *ChangeMessageVisibilityResponse, err error) {
resp = &ChangeMessageVisibilityResponse{}
params := makeParams("ChangeMessageVisibility")
params["VisibilityTimeout"] = strconv.Itoa(VisibilityTimeout)
params["ReceiptHandle"] = M.ReceiptHandle
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) GetQueueAttributes(A string) (resp *GetQueueAttributesResponse, err error) {
resp = &GetQueueAttributesResponse{}
params := makeParams("GetQueueAttributes")
params["AttributeName"] = A
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) DeleteMessage(M *Message) (resp *DeleteMessageResponse, err error) {
resp = &DeleteMessageResponse{}
params := makeParams("DeleteMessage")
params["ReceiptHandle"] = M.ReceiptHandle
err = q.SQS.query(q.Url, params, resp)
return
}
type SendMessageBatchResultEntry struct {
Id string `xml:"Id"`
MessageId string `xml:"MessageId"`
MD5OfMessageBody string `xml:"MD5OfMessageBody"`
}
type SendMessageBatchResponse struct {
SendMessageBatchResult []SendMessageBatchResultEntry `xml:"SendMessageBatchResult>SendMessageBatchResultEntry"`
ResponseMetadata ResponseMetadata
}
/* SendMessageBatch
*/
func (q *Queue) SendMessageBatch(msgList []Message) (resp *SendMessageBatchResponse, err error) {
resp = &SendMessageBatchResponse{}
params := makeParams("SendMessageBatch")
for idx, msg := range msgList {
count := idx + 1
params[fmt.Sprintf("SendMessageBatchRequestEntry.%d.Id", count)] = fmt.Sprintf("msg-%d", count)
params[fmt.Sprintf("SendMessageBatchRequestEntry.%d.MessageBody", count)] = msg.Body
}
err = q.SQS.query(q.Url, params, resp)
return
}
/* SendMessageBatchString
*/
func (q *Queue) SendMessageBatchString(msgList []string) (resp *SendMessageBatchResponse, err error) {
resp = &SendMessageBatchResponse{}
params := makeParams("SendMessageBatch")
for idx, msg := range msgList {
count := idx + 1
params[fmt.Sprintf("SendMessageBatchRequestEntry.%d.Id", count)] = fmt.Sprintf("msg-%d", count)
params[fmt.Sprintf("SendMessageBatchRequestEntry.%d.MessageBody", count)] = msg
}
err = q.SQS.query(q.Url, params, resp)
return
}
type DeleteMessageBatchResponse struct {
DeleteMessageBatchResult []struct {
Id string
SenderFault bool
Code string
Message string
} `xml:"DeleteMessageBatchResult>DeleteMessageBatchResultEntry"`
ResponseMetadata ResponseMetadata
}
/* DeleteMessageBatch */
func (q *Queue) DeleteMessageBatch(msgList []Message) (resp *DeleteMessageBatchResponse, err error) {
resp = &DeleteMessageBatchResponse{}
params := makeParams("DeleteMessageBatch")
lutMsg := make(map[string]Message)
for idx := range msgList {
params[fmt.Sprintf("DeleteMessageBatchRequestEntry.%d.Id", idx+1)] = msgList[idx].MessageId
params[fmt.Sprintf("DeleteMessageBatchRequestEntry.%d.ReceiptHandle", idx+1)] = msgList[idx].ReceiptHandle
lutMsg[string(msgList[idx].MessageId)] = msgList[idx]
}
err = q.SQS.query(q.Url, params, resp)
messageWithErrors := make([]Message, 0, len(msgList))
for idx := range resp.DeleteMessageBatchResult {
if resp.DeleteMessageBatchResult[idx].SenderFault {
msg, ok := lutMsg[resp.DeleteMessageBatchResult[idx].Id]
if ok {
messageWithErrors = append(messageWithErrors, msg)
}
}
}
if len(messageWithErrors) > 0 {
log.Printf("%d Message have not been sent", len(messageWithErrors))
}
return
}
func (s *SQS) query(queueUrl string, params map[string]string, resp interface{}) (err error) {
params["Version"] = "2011-10-01"
params["Timestamp"] = time.Now().In(time.UTC).Format(time.RFC3339)
var url_ *url.URL
var path string
if queueUrl != "" {
url_, err = url.Parse(queueUrl)
path = queueUrl[len(s.Region.SQSEndpoint):]
} else {
url_, err = url.Parse(s.Region.SQSEndpoint)
path = "/"
}
if err != nil {
return err
}
//url_, err := url.Parse(s.Region.SQSEndpoint)
//if err != nil {
// return err
//}
sign(s.Auth, "GET", path, params, url_.Host)
url_.RawQuery = multimap(params).Encode()
if debug {
log.Printf("GET ", url_.String())
}
r, err := http.Get(url_.String())
if err != nil {
return err
}
defer r.Body.Close()
if debug {
dump, _ := httputil.DumpResponse(r, true)
log.Printf("DUMP:\n", string(dump))
}
if r.StatusCode != 200 {
return buildError(r)
}
err = xml.NewDecoder(r.Body).Decode(resp)
return err
}
func buildError(r *http.Response) error {
errors := xmlErrors{}
xml.NewDecoder(r.Body).Decode(&errors)
var err Error
if len(errors.Errors) > 0 {
err = errors.Errors[0]
} else {
err = errors.Error
}
err.RequestId = errors.RequestId
err.StatusCode = r.StatusCode
if err.Message == "" {
err.Message = r.Status
}
return &err
}
func makeParams(action string) map[string]string {
params := make(map[string]string)
params["Action"] = action
return params
}
func multimap(p map[string]string) url.Values {
q := make(url.Values, len(p))
for k, v := range p {
q[k] = []string{v}
}
return q
}