4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "sync"
7
8
8
9
"github.com/aws/aws-sdk-go-v2/service/sns"
9
10
"github.com/aws/aws-sdk-go-v2/service/sqs"
@@ -17,24 +18,27 @@ var ErrTopicOrQueueNotFound = errors.New("could not find neither topic ARN nor q
17
18
type Publisher struct {
18
19
sns * sns.Client
19
20
sqs * sqs.Client
20
- resources map [ string ] string
21
+ resources sync. Map
21
22
}
22
23
23
24
// NewPublisher creates a new SNS+SQS publisher.
24
25
func NewPublisher (sns * sns.Client , sqs * sqs.Client , resources map [string ]string ) * Publisher {
25
- return & Publisher {
26
- sns : sns ,
27
- sqs : sqs ,
28
- resources : resources ,
26
+ p := & Publisher {
27
+ sns : sns ,
28
+ sqs : sqs ,
29
29
}
30
+ for k , v := range resources {
31
+ p .resources .Store (k , v )
32
+ }
33
+ return p
30
34
}
31
35
32
36
// Publish a message trough SNS.
33
37
func (p * Publisher ) Publish (ctx context.Context , resourceID string , envelopes ... * pubsub.Envelope ) error {
34
38
// If the resource exists we get it, otherwise we use the identifier.
35
- resource , _ := p . resources [ resourceID ]
36
- if resource == "" {
37
- resource = resourceID
39
+ resource := resourceID
40
+ if r , ok := p . resources . Load ( resourceID ); ok {
41
+ resource = r .( string )
38
42
}
39
43
40
44
// Note: topic ARN "are" technically URLs, so this check need to go first.
@@ -49,6 +53,10 @@ func (p *Publisher) Publish(ctx context.Context, resourceID string, envelopes ..
49
53
return fmt .Errorf ("%w: %s" , ErrTopicOrQueueNotFound , resource )
50
54
}
51
55
56
+ func (p * Publisher ) AddResource (resourceID , resource string ) {
57
+ p .resources .Store (resourceID , resource )
58
+ }
59
+
52
60
func publishSNSMessage (ctx context.Context , c * sns.Client , topicARN string , envelopes ... * pubsub.Envelope ) error {
53
61
for _ , env := range envelopes {
54
62
// every FIFO queue message needs to have a message group in SNS
0 commit comments