-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsqs.go
More file actions
187 lines (167 loc) · 6.33 KB
/
sqs.go
File metadata and controls
187 lines (167 loc) · 6.33 KB
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
// Copyright © 2016 Tom Maiaroto <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package framework
import (
"bytes"
"context"
"errors"
"strings"
"github.com/aws/aws-lambda-go/lambda"
)
// SQSRouter struct provides an interface to handle SQS events (routers can be per Domain much like S3ObjectRouter can be per Bucket)
type SQSRouter struct {
handlers map[string]SQSHandler
Queue string
Tracer TraceStrategy
}
// SQSHandler handles incoming SQS events. Matching on attribute name and its string value (ie. 1 is "1" and binary values are base64 strings)
type SQSHandler struct {
Handler func(context.Context, *HandlerDependencies, *SQSEvent) error
AttributeName string
AttributeStrValue string
}
// LambdaHandler handles SQS events.
func (r *SQSRouter) LambdaHandler(ctx context.Context, d *HandlerDependencies, evt SQSEvent) error {
// If this Router had a Tracer set for it, replace the default which came from the Aegis interface.
if r.Tracer != nil {
d.Tracer = r.Tracer
}
// If an incoming event can be matched to this router, but the router has no registered handlers
// or if one hasn't been added to aegis.Handlers{}.
if r == nil {
return errors.New("no handlers registered for SQSRouter")
}
var err error
handled := false
// There can be multiple
for _, record := range evt.Records {
// If there are any handlers registered
if r.handlers != nil {
// First, look for queue name match
queueMatch := false
if record.EventSourceARN == r.Queue || GetQueueNameFromARN(record.EventSourceARN) == r.Queue {
queueMatch = true
}
// and if the domain matches or if no domain was defined for the SQSRouter (all domains)
if r.Queue == "" || queueMatch {
// Each key on handlers map is a string that can be used to glob match
// If one of them matches, use that
matchedAttributeName := ""
matchedAttributeStrValue := ""
for _, handler := range r.handlers {
attributeMatch := false
for attrName, attrVal := range record.MessageAttributes {
if attrVal.StringValue != nil {
strVal := *attrVal.StringValue
if handler.AttributeName == attrName && handler.AttributeStrValue == strVal {
attributeMatch = true
}
}
}
if attributeMatch {
handled = true
// Trace (default is to use XRay)
d.Tracer.Record("annotation",
map[string]interface{}{
"EventSourceARN": record.EventSourceARN,
"EventSource": record.EventSource,
"AWSRegion": record.AWSRegion,
"AttributeName": matchedAttributeName,
"AttributeStrValue": matchedAttributeStrValue,
},
)
err = d.Tracer.Capture(ctx, "SQSHandler", func(ctx1 context.Context) error {
return handler.Handler(ctx1, d, &evt)
})
}
}
// Otherwise, use the catch all (router "fallthrough" equivalent) handler.
// The application can inspect the map and make a decision on what to do, if anything.
// This is optional.
if !handled {
// It's possible that the SQSRouter wasn't created with NewSQSRouter, so check for this still.
if handler, ok := r.handlers["_"]; ok {
// Capture the handler (in XRay by default) automatically
d.Tracer.Record("annotation",
map[string]interface{}{
"EventSourceARN": record.EventSourceARN,
"EventSource": record.EventSource,
"AWSRegion": record.AWSRegion,
"AttributeName": matchedAttributeName,
"AttributeStrValue": matchedAttributeStrValue,
"FallthroughHandler": true,
},
)
err = d.Tracer.Capture(ctx, "SQSHandler", func(ctx1 context.Context) error {
return handler.Handler(ctx1, d, &evt)
})
}
}
}
}
}
return err
}
// Listen will start an SES event listener that handles incoming object based events (put, delete, etc.)
func (r *SQSRouter) Listen() {
lambda.Start(r.LambdaHandler)
}
// NewSQSRouter simply returns a new SQSRouter struct and behaves a bit like Router, it even takes an optional rootHandler or "fall through" catch all
func NewSQSRouter(rootHandler ...func(context.Context, *HandlerDependencies, *SQSEvent) error) *SQSRouter {
// The catch all is optional, if not provided, an empty handler is still called and it returns nothing.
handler := SQSHandler{
Handler: func(context.Context, *HandlerDependencies, *SQSEvent) error {
return nil
},
}
if len(rootHandler) > 0 {
handler = SQSHandler{
Handler: rootHandler[0],
}
}
return &SQSRouter{
handlers: map[string]SQSHandler{
"_": handler,
},
}
}
// NewSQSRouterForQueue returns a new SQSRouter for a specific SQS queue, it will only route events coming from the given queue
func NewSQSRouterForQueue(queue string, rootHandler ...func(context.Context, *HandlerDependencies, *SQSEvent) error) *SQSRouter {
router := NewSQSRouter(rootHandler...)
router.Queue = queue
return router
}
// Handle will register a handler for a given attribute name and its string value match (1 would be "1" and binary values would be base64 strings).
// Note that routers can be per queue so this match could match two different queues and therefore be used in two different routers.
func (r *SQSRouter) Handle(attrName string, attrStrValue string, handler func(context.Context, *HandlerDependencies, *SQSEvent) error) {
if r.handlers == nil {
r.handlers = make(map[string]SQSHandler)
}
var buffer bytes.Buffer
buffer.WriteString(attrName)
buffer.WriteString(attrStrValue)
k := buffer.String()
buffer.Reset()
r.handlers[k] = SQSHandler{
Handler: handler,
AttributeName: attrName,
AttributeStrValue: attrStrValue,
}
}
// GetQueueNameFromARN will get the SQS queue name given the ARN string
func GetQueueNameFromARN(arn string) string {
p := strings.Split(arn, ":")
return p[len(p)-1]
}
// TODO: Helper perhaps to send messages to a queue?