@@ -3,13 +3,15 @@ package sns
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "errors"
6
7
"strings"
7
8
8
9
"github.com/aws/aws-sdk-go/aws"
9
10
"github.com/aws/aws-sdk-go/aws/request"
10
11
"github.com/aws/aws-sdk-go/aws/session"
11
12
"github.com/aws/aws-sdk-go/service/sns"
12
- "github.com/google/uuid"
13
+ "github.com/creatorstack/htsqs/constants"
14
+ "github.com/creatorstack/htsqs/publisher/models"
13
15
)
14
16
15
17
// sender is the interface to sns.SNS. Its sole purpose is to make
@@ -67,18 +69,22 @@ func (p *Publisher) Publish(ctx context.Context, msg interface{}) error {
67
69
// kept under 100 messages so that all messages can be published in 10 tries. In case
68
70
// of failure when parsing or publishing any of the messages, this function will stop
69
71
// further publishing and return an error
70
- func (p * Publisher ) PublishBatch (ctx context.Context , msgs []interface {}) error {
72
+ func (p * Publisher ) PublishBatch (ctx context.Context , msgs []models. Message ) ( map [ string ] error , int64 , int64 , error ) {
71
73
var (
72
74
defaultMessageGroupID = "default"
75
+ publishResult = make (map [string ]error )
73
76
err error
77
+
78
+ errorCount int64
79
+ successCount int64
74
80
)
75
81
76
82
isFifo := strings .Contains (strings .ToLower (p .cfg .TopicArn ), "fifo" )
77
83
78
84
var (
79
85
numPublishedMessages = 0
80
86
start = 0
81
- end = 10 // 10 is the maximum batch size for SNS.PublishBatch
87
+ end = constants . MaxBatchSize
82
88
)
83
89
if end > len (msgs ) {
84
90
end = len (msgs )
@@ -90,14 +96,13 @@ func (p *Publisher) PublishBatch(ctx context.Context, msgs []interface{}) error
90
96
for idx := start ; idx < end ; idx ++ {
91
97
msg := msgs [idx ]
92
98
93
- b , err := json .Marshal (msg )
99
+ b , err := json .Marshal (msg . Data )
94
100
if err != nil {
95
- return err
101
+ return publishResult , successCount , errorCount , err
96
102
}
97
103
98
- entryId := uuid .New ().String ()
99
104
requestEntry := & sns.PublishBatchRequestEntry {
100
- Id : aws .String (entryId ),
105
+ Id : aws .String (msg . ID ),
101
106
Message : aws .String (string (b )),
102
107
}
103
108
@@ -112,20 +117,38 @@ func (p *Publisher) PublishBatch(ctx context.Context, msgs []interface{}) error
112
117
PublishBatchRequestEntries : requestEntries ,
113
118
TopicArn : & p .cfg .TopicArn ,
114
119
}
115
- _ , err = p .sns .PublishBatchWithContext (ctx , input )
120
+ response , err : = p .sns .PublishBatchWithContext (ctx , input )
116
121
if err != nil {
117
- return err
122
+ return publishResult , successCount , errorCount , err
123
+ }
124
+
125
+ for _ , errEntry := range response .Failed {
126
+ if errEntry != nil && errEntry .Id != nil {
127
+ errMsg := "publish error"
128
+ if errEntry .Message != nil {
129
+ errMsg = * errEntry .Message
130
+ }
131
+ publishResult [* errEntry .Id ] = errors .New (errMsg )
132
+ errorCount ++
133
+ }
134
+ }
135
+
136
+ for _ , successEntry := range response .Successful {
137
+ if successEntry != nil && successEntry .Id != nil {
138
+ publishResult [* successEntry .Id ] = nil
139
+ successCount ++
140
+ }
118
141
}
119
142
120
143
numPublishedMessages += len (requestEntries )
121
144
start = end
122
- end += 10
145
+ end += constants . MaxBatchSize
123
146
if end > len (msgs ) {
124
147
end = len (msgs )
125
148
}
126
149
}
127
150
128
- return err
151
+ return publishResult , successCount , errorCount , err
129
152
}
130
153
131
154
func defaultPublisherConfig (cfg * Config ) {
0 commit comments