forked from beatlabs/patron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
294 lines (255 loc) · 7.55 KB
/
main.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
package main
import (
"context"
"errors"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sns/snsiface"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
"github.com/beatlabs/patron"
patronsns "github.com/beatlabs/patron/client/sns/v2"
patronsqs "github.com/beatlabs/patron/client/sqs/v2"
patronamqp "github.com/beatlabs/patron/component/amqp"
"github.com/beatlabs/patron/encoding/json"
"github.com/beatlabs/patron/encoding/protobuf"
"github.com/beatlabs/patron/examples"
"github.com/beatlabs/patron/log"
"github.com/streadway/amqp"
)
const (
amqpURL = "amqp://guest:guest@localhost:5672/"
amqpQueue = "patron"
amqpExchangeName = "patron"
amqpExchangeType = amqp.ExchangeFanout
// Shared AWS config
awsRegion = "eu-west-1"
awsID = "test"
awsSecret = "test"
awsToken = "token"
// SQS config
awsSQSEndpoint = "http://localhost:4566"
awsSQSQueue = "patron"
// SNS config
awsSNSEndpoint = "http://localhost:4566"
awsSNSTopic = "patron-topic"
)
func init() {
err := os.Setenv("PATRON_LOG_LEVEL", "debug")
if err != nil {
log.Fatalf("failed to set log level env var: %v", err)
}
err = os.Setenv("PATRON_JAEGER_SAMPLER_PARAM", "1.0")
if err != nil {
log.Fatalf("failed to set sampler env vars: %v", err)
}
err = os.Setenv("PATRON_HTTP_DEFAULT_PORT", "50003")
if err != nil {
log.Fatalf("failed to set default patron port env vars: %v", err)
}
// Setup queue and exchange if not already done.
err = setupQueueAndExchange()
if err != nil {
log.Fatalf("failed to set up queue and exchange: %v", err)
}
}
func setupQueueAndExchange() error {
conn, err := amqp.Dial(amqpURL)
if err != nil {
return err
}
channel, err := conn.Channel()
if err != nil {
return err
}
err = channel.ExchangeDeclare(amqpExchangeName, amqpExchangeType, true, false, false, false, nil)
if err != nil {
return err
}
q, err := channel.QueueDeclare(amqpQueue, true, false, false, false, nil)
if err != nil {
return err
}
err = channel.QueueBind(q.Name, "", amqpExchangeName, false, nil)
if err != nil {
return err
}
return nil
}
func main() {
name := "amqp"
version := "1.0.0"
service, err := patron.New(name, version, patron.TextLogger())
if err != nil {
log.Fatalf("failed to set up service: %v", err)
}
// Programmatically create an empty SQS queue for the sake of the example
sqsAPI := sqs.New(getAWSSession(awsSQSEndpoint))
sqsQueueURL, err := createSQSQueue(sqsAPI)
if err != nil {
log.Fatalf("failed to create sqs queue: %v", err)
}
// Programmatically create an SNS topic for the sake of the example
snsAPI := sns.New(getAWSSession(awsSNSEndpoint))
snsTopicArn, err := createSNSTopic(snsAPI)
if err != nil {
log.Fatalf("failed to create sns topic: %v", err)
}
// Route the SNS topic to the SQS queue, so that any message received on the SNS topic
// will be automatically sent to the SQS queue.
err = routeSNSTopicToSQSQueue(snsAPI, sqsQueueURL, snsTopicArn)
if err != nil {
log.Fatalf("failed to route sns to sqs: %v", err)
}
// Create an SNS publisher
snsPub, err := patronsns.New(snsAPI)
if err != nil {
log.Fatalf("failed to create sns publisher: %v", err)
}
// Create an SQS publisher
sqsPub, err := patronsqs.New(sqsAPI)
if err != nil {
log.Fatalf("failed to create sqs publisher: %v", err)
}
// Initialise the AMQP component
amqpCmp, err := newAmqpComponent(amqpURL, amqpQueue, snsTopicArn, snsPub, sqsPub, sqsQueueURL)
if err != nil {
log.Fatalf("failed to create processor %v", err)
}
ctx := context.Background()
err = service.WithComponents(amqpCmp.cmp).Run(ctx)
if err != nil {
log.Fatalf("failed to create and run service %v", err)
}
}
func getAWSSession(endpoint string) *session.Session {
// 15 attempts 1 seconds separated to retrieve valid session
var s *session.Session = nil
var err error = nil
for i := 0; i < 15; i++ {
s, err = session.NewSession(
&aws.Config{
Region: aws.String(awsRegion),
Credentials: credentials.NewStaticCredentials(awsID, awsSecret, awsToken),
},
&aws.Config{Endpoint: aws.String(endpoint)},
)
if err == nil {
return s
}
time.Sleep(1 * time.Second)
}
// this will panic if error is not null
return session.Must(s, err)
}
func createSQSQueue(api sqsiface.SQSAPI) (string, error) {
out, err := api.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(awsSQSQueue),
})
if err != nil {
return "", err
}
if out.QueueUrl == nil {
return "", errors.New("could not create the queue")
}
return *out.QueueUrl, err
}
func createSNSTopic(snsAPI snsiface.SNSAPI) (string, error) {
out, err := snsAPI.CreateTopic(&sns.CreateTopicInput{
Name: aws.String(awsSNSTopic),
})
if err != nil {
return "", err
}
return *out.TopicArn, nil
}
func routeSNSTopicToSQSQueue(snsAPI snsiface.SNSAPI, sqsQueueArn, topicArn string) error {
_, err := snsAPI.Subscribe(&sns.SubscribeInput{
Protocol: aws.String("sqs"),
TopicArn: aws.String(topicArn),
Endpoint: aws.String(sqsQueueArn),
Attributes: map[string]*string{
// Set the RawMessageDelivery to "true" in order to be able to pass the MessageAttributes from SNS
// to SQS, and therefore to propagate the trace.
// See https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html for more information.
"RawMessageDelivery": aws.String("true"),
},
})
return err
}
type amqpComponent struct {
cmp patron.Component
snsTopicArn string
snsPub patronsns.Publisher
sqsPub patronsqs.Publisher
sqsQueueURL string
}
func newAmqpComponent(url, queue, snsTopicArn string, snsPub patronsns.Publisher, sqsPub patronsqs.Publisher,
sqsQueueURL string) (*amqpComponent, error) {
amqpCmp := amqpComponent{
snsTopicArn: snsTopicArn,
snsPub: snsPub,
sqsPub: sqsPub,
sqsQueueURL: sqsQueueURL,
}
cmp, err := patronamqp.New(url, queue, amqpCmp.Process, patronamqp.Retry(10, 1*time.Second))
if err != nil {
return nil, err
}
amqpCmp.cmp = cmp
return &amqpCmp, nil
}
func (ac *amqpComponent) Process(ctx context.Context, batch patronamqp.Batch) {
for _, msg := range batch.Messages() {
if ctx.Err() != nil {
log.FromContext(ctx).Info("context cancelled, exiting process function")
}
logger := log.FromContext(msg.Context())
var u examples.User
err := protobuf.DecodeRaw(msg.Body(), &u)
if err != nil {
logger.Errorf("failed to decode message: %v", err)
err = msg.NACK()
if err != nil {
logger.Errorf("failed to NACK message: %v", err)
}
}
payload, err := json.Encode(&u)
if err != nil {
logger.Errorf("failed to encode message: %v", err)
err = msg.NACK()
if err != nil {
logger.Errorf("failed to NACK message: %v", err)
}
}
input := &sns.PublishInput{
Message: aws.String(string(payload)),
TargetArn: aws.String(ac.snsTopicArn),
}
_, err = ac.snsPub.Publish(msg.Context(), input)
if err != nil {
logger.Errorf("failed to publish message to SNS: %v", err)
err = msg.NACK()
if err != nil {
logger.Errorf("failed to NACK message: %v", err)
}
}
sqsMsg := &sqs.SendMessageInput{
MessageBody: aws.String(string(payload)),
QueueUrl: aws.String(ac.sqsQueueURL),
}
_, err = ac.sqsPub.Publish(msg.Context(), sqsMsg)
if err != nil {
logger.Errorf("failed to publish message to SQS: %v", err)
err = msg.NACK()
if err != nil {
logger.Errorf("failed to NACK message: %v", err)
}
}
logger.Infof("request processed: %s %s", u.GetFirstname(), u.GetLastname())
}
}