-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
81 lines (68 loc) · 2.12 KB
/
publisher.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
package aws
import (
"context"
"errors"
"fmt"
"sync"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/hmoragrega/pubsub"
)
var ErrTopicOrQueueNotFound = errors.New("could not find neither topic ARN nor queue URL")
// Publisher SNS+SQS publisher.
type Publisher struct {
sns *sns.Client
sqs *sqs.Client
resources sync.Map
}
// NewPublisher creates a new SNS+SQS publisher.
func NewPublisher(sns *sns.Client, sqs *sqs.Client, resources map[string]string) *Publisher {
p := &Publisher{
sns: sns,
sqs: sqs,
}
for k, v := range resources {
p.resources.Store(k, v)
}
return p
}
// Publish a message trough SNS.
func (p *Publisher) Publish(ctx context.Context, resourceID string, envelopes ...*pubsub.Envelope) error {
// If the resource exists we get it, otherwise we use the identifier.
resource := resourceID
if r, ok := p.resources.Load(resourceID); ok {
resource = r.(string)
}
// Note: topic ARN "are" technically URLs, so this check need to go first.
if topicARN := resource; isSNSTopicARN(topicARN) {
return publishSNSMessage(ctx, p.sns, topicARN, envelopes...)
}
if queueURL := resource; isURL(queueURL) {
return publishSQSMessage(ctx, p.sqs, queueURL, 0, envelopes...)
}
return fmt.Errorf("%w: %s", ErrTopicOrQueueNotFound, resource)
}
func (p *Publisher) AddResource(resourceID, resource string) {
p.resources.Store(resourceID, resource)
}
func publishSNSMessage(ctx context.Context, c *sns.Client, topicARN string, envelopes ...*pubsub.Envelope) error {
for _, env := range envelopes {
// every FIFO queue message needs to have a message group in SNS
// @TODO only for FIFO
// key := env.Key
// if key == "" {
// key = "void"
// }
_, err := c.Publish(ctx, &sns.PublishInput{
TopicArn: &topicARN,
Message: stringPtr(env.Body),
MessageAttributes: encodeSNSAttributes(env),
//MessageDeduplicationId: &base64ID, // @TODO FIFO only
//MessageGroupId: &key, // @TODO FIFO only
})
if err != nil {
return fmt.Errorf("cannot publish message %s: %w", env.ID, wrapError(err))
}
}
return nil
}