/
send_aws_sqs.go
178 lines (146 loc) · 4.42 KB
/
send_aws_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
package transform
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/brexhq/substation/config"
"github.com/brexhq/substation/internal/aggregate"
"github.com/brexhq/substation/internal/aws"
"github.com/brexhq/substation/internal/aws/sqs"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/message"
)
// Records greater than 256 KB in size cannot be
// put into an SQS queue.
const sendSQSMessageSizeLimit = 1024 * 1024 * 256
// errSendSQSMessageSizeLimit is returned when data exceeds the SQS msg
// size limit. If this error occurs, then conditions or transforms
// should be applied to either drop or reduce the size of the data.
var errSendSQSMessageSizeLimit = fmt.Errorf("data exceeded size limit")
type sendAWSSQSConfig struct {
// ARN is the AWS SNS topic ARN that messages are sent to.
ARN string `json:"arn"`
// AuxTransforms are applied to batched data before it is sent.
AuxTransforms []config.Config `json:"auxiliary_transforms"`
Object iconfig.Object `json:"object"`
Batch iconfig.Batch `json:"batch"`
AWS iconfig.AWS `json:"aws"`
Retry iconfig.Retry `json:"retry"`
}
func (c *sendAWSSQSConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *sendAWSSQSConfig) Validate() error {
if c.ARN == "" {
return fmt.Errorf("arn: %v", errors.ErrMissingRequiredOption)
}
return nil
}
func newSendAWSSQS(_ context.Context, cfg config.Config) (*sendAWSSQS, error) {
conf := sendAWSSQSConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: send_aws_sqs: %v", err)
}
if err := conf.Validate(); err != nil {
return nil, fmt.Errorf("transform: send_aws_sqs: %v", err)
}
// arn:aws:sqs:region:account_id:queue_name
arn := strings.Split(conf.ARN, ":")
tf := sendAWSSQS{
conf: conf,
queueURL: fmt.Sprintf(
"https://sqs.%s.amazonaws.com/%s/%s",
arn[3],
arn[4],
arn[5],
),
}
// Setup the AWS client.
tf.client.Setup(aws.Config{
Region: conf.AWS.Region,
RoleARN: conf.AWS.RoleARN,
MaxRetries: conf.Retry.Count,
RetryableErrors: conf.Retry.ErrorMessages,
})
agg, err := aggregate.New(aggregate.Config{
// SQS limits batch operations to 10 messages.
Count: 10,
// SQS limits batch operations to 256 KB.
Size: sendSQSMessageSizeLimit,
Duration: conf.Batch.Duration,
})
if err != nil {
return nil, err
}
tf.agg = agg
if len(conf.AuxTransforms) > 0 {
tf.tforms = make([]Transformer, len(conf.AuxTransforms))
for i, c := range conf.AuxTransforms {
t, err := New(context.Background(), c)
if err != nil {
return nil, fmt.Errorf("transform: send_aws_sqs: %v", err)
}
tf.tforms[i] = t
}
}
return &tf, nil
}
type sendAWSSQS struct {
conf sendAWSSQSConfig
queueURL string
// client is safe for concurrent use.
client sqs.API
mu sync.Mutex
agg *aggregate.Aggregate
tforms []Transformer
}
func (tf *sendAWSSQS) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
tf.mu.Lock()
defer tf.mu.Unlock()
if msg.IsControl() {
for key := range tf.agg.GetAll() {
if tf.agg.Count(key) == 0 {
continue
}
if err := tf.send(ctx, key); err != nil {
return nil, fmt.Errorf("transform: send_aws_sqs: %v", err)
}
}
tf.agg.ResetAll()
return []*message.Message{msg}, nil
}
if len(msg.Data()) > sendSQSMessageSizeLimit {
return nil, fmt.Errorf("transform: send_aws_sqs: %v", errSendSQSMessageSizeLimit)
}
// If this value does not exist, then all data is batched together.
key := msg.GetValue(tf.conf.Object.BatchKey).String()
if ok := tf.agg.Add(key, msg.Data()); ok {
return []*message.Message{msg}, nil
}
if err := tf.send(ctx, key); err != nil {
return nil, fmt.Errorf("transform: send_aws_sqs: %v", err)
}
// If data cannot be added after reset, then the batch is misconfgured.
tf.agg.Reset(key)
if ok := tf.agg.Add(key, msg.Data()); !ok {
return nil, fmt.Errorf("transform: send_aws_sqs: %v", errSendBatchMisconfigured)
}
return []*message.Message{msg}, nil
}
func (tf *sendAWSSQS) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}
func (tf *sendAWSSQS) send(ctx context.Context, key string) error {
data, err := withTransforms(ctx, tf.tforms, tf.agg.Get(key))
if err != nil {
return err
}
if _, err := tf.client.SendMessageBatch(ctx, tf.queueURL, data); err != nil {
return err
}
return nil
}