-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
334 lines (279 loc) · 9.28 KB
/
client.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package asyncsqs
import (
"context"
"fmt"
"net/url"
"sync"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
)
const (
defaultBufferSize = 1000
maxBatchSize = 10
maxPayloadBytes = 262144
)
type sqsOp int
const (
opSend sqsOp = iota
opDelete sqsOp = iota
)
// SQSClient wraps *sqs.Client from aws-sdk-go-v2
type SQSClient interface {
SendMessageBatch(context.Context, *sqs.SendMessageBatchInput, ...func(*sqs.Options)) (*sqs.SendMessageBatchOutput, error)
DeleteMessageBatch(context.Context, *sqs.DeleteMessageBatchInput, ...func(*sqs.Options)) (*sqs.DeleteMessageBatchOutput, error)
}
// genericEntry for the lack of generics in Go.
type genericEntry struct {
sendReq types.SendMessageBatchRequestEntry
delReq types.DeleteMessageBatchRequestEntry
}
// Config is used to configure BufferedClient.
type Config struct {
// SQSClient abstracts *sqs.Client from aws-sdk-go-v2. You can bring your
// own fully initialised SQS client (with required credentials, options
// etc). This is a required field.
SQSClient SQSClient
// QueueURL specifies AWS SQS Queue URL for a queue.
// This is a required field.
QueueURL string
// Following fields are optional.
// SendWaitTime specifies a time limit for how long the client will
// wait before it will dispatch accumulated send message requests
// even if the batch isn't full. If not specified, send message
// requests will be dispatched only when a batch is full.
SendWaitTime time.Duration
// SendBufferSize specifies a limit on the number of send message
// requests that can be held in memory. If not specified, defaults
// to 1000.
SendBufferSize int
// SendConcurrency limits the number of concurrent send message SQS
// requests in progress. If not specified, defaults to SendBufferSize/10.
SendConcurrency int
// OnSendMessageBatch will be called with results returned by SQSClient
// for a send message batch operation. If set, this callback function
// needs to be goroutine safe.
OnSendMessageBatch func(*sqs.SendMessageBatchOutput, error)
// DeleteWaitTime specifies a time limit for how long the client will
// wait before it will dispatch accumulated delete message requests
// even if the batch isn't full. If not specified, delete message
// requests will be dispatched only when a batch is full.
DeleteWaitTime time.Duration
// DeleteBufferSize specifies a limit on the number of delete message
// requests that can be held in memory. If not specified, defaults
// to 1000.
DeleteBufferSize int
// DeleteConcurrency limits the number of concurrent delete message SQS
// requests in progress. If not specified, defaults to DeleteBufferSize/10.
DeleteConcurrency int
// OnDeleteMessageBatch will be called with results returned by SQSClient
// for a delete message batch operation. If set, this callback function
// needs to be goroutine safe.
OnDeleteMessageBatch func(*sqs.DeleteMessageBatchOutput, error)
}
// Stats contains client statistics.
type Stats struct {
MessagesSent uint64
MessagesDeleted uint64
SendMessageBatchCalls uint64
DeleteMessageBatchCalls uint64
}
// BufferedClient wraps aws-sdk-go-v2's sqs.Client to provide a async buffered client.
type BufferedClient struct {
Config
sendQueue chan genericEntry
deleteQueue chan genericEntry
batchers sync.WaitGroup
stopped bool
stats Stats
}
// NewBufferedClient creates and returns a new instance of BufferedClient. You
// will need one BufferedClient client per SQS queue. Stop() must be eventually
// called to free resources created by NewBufferedClient.
func NewBufferedClient(config Config) (*BufferedClient, error) {
if config.SQSClient == nil {
return nil, fmt.Errorf("config.Client cannot be nil")
}
if _, err := url.ParseRequestURI(config.QueueURL); err != nil {
return nil, fmt.Errorf("invalid config.QueueURL=%s error=%w", config.QueueURL, err)
}
c := &BufferedClient{
Config: config,
}
if c.SendBufferSize <= 0 {
c.SendBufferSize = defaultBufferSize
}
c.sendQueue = make(chan genericEntry, c.SendBufferSize)
if c.DeleteBufferSize <= 0 {
c.DeleteBufferSize = defaultBufferSize
}
c.deleteQueue = make(chan genericEntry, c.DeleteBufferSize)
if c.SendConcurrency < 1 {
c.SendConcurrency = c.SendBufferSize / maxBatchSize
}
c.batchers.Add(1)
go c.batcher(c.sendQueue, c.SendWaitTime, c.SendConcurrency, opSend, &c.batchers)
if c.DeleteConcurrency < 1 {
c.DeleteConcurrency = c.DeleteBufferSize / maxBatchSize
}
c.batchers.Add(1)
go c.batcher(c.deleteQueue, c.DeleteWaitTime, c.DeleteConcurrency, opDelete, &c.batchers)
return c, nil
}
// Stop stops all the batcher and dispatcher goroutines. It blocks until all
// pending requests in buffer are gracefully drained. Stop should be called
// only after calls to SendMessageAsync() and DeleteMessageAsync() have stopped.
func (c *BufferedClient) Stop() {
if c.stopped {
return
}
c.stopped = true
close(c.sendQueue)
close(c.deleteQueue)
c.batchers.Wait()
}
// Stats returns client statistics.
func (c *BufferedClient) Stats() Stats {
s := Stats{
MessagesSent: atomic.LoadUint64(&c.stats.MessagesSent),
MessagesDeleted: atomic.LoadUint64(&c.stats.MessagesDeleted),
SendMessageBatchCalls: atomic.LoadUint64(&c.stats.SendMessageBatchCalls),
DeleteMessageBatchCalls: atomic.LoadUint64(&c.stats.DeleteMessageBatchCalls),
}
return s
}
// SendMessageAsync schedules message(s) to be sent. It blocks if the send
// buffer is full.
func (c *BufferedClient) SendMessageAsync(entries ...types.SendMessageBatchRequestEntry) error {
if c.stopped {
return fmt.Errorf("client stopped")
}
for _, entry := range entries {
if len(*entry.MessageBody) > maxPayloadBytes/maxBatchSize {
return fmt.Errorf("individual message size cannot exceed %d bytes", maxPayloadBytes/maxBatchSize)
}
}
for _, entry := range entries {
c.sendQueue <- genericEntry{
sendReq: entry,
}
}
return nil
}
// DeleteMessageAsync schedules message(s) to be deleted. It blocks if the delete
// buffer is full.
func (c *BufferedClient) DeleteMessageAsync(entries ...types.DeleteMessageBatchRequestEntry) error {
if c.stopped {
return fmt.Errorf("client stopped")
}
for _, entry := range entries {
c.deleteQueue <- genericEntry{
delReq: entry,
}
}
return nil
}
// batcher batches multiple send and delete requests to be dispatched in batches.
func (c *BufferedClient) batcher(queue chan genericEntry, waitTime time.Duration, concurrency int, op sqsOp, wg *sync.WaitGroup) {
defer wg.Done()
var ticker *time.Ticker
if waitTime <= 0 {
ticker = disabledTicker()
} else {
ticker = time.NewTicker(waitTime)
defer ticker.Stop()
}
jobs := make(chan []genericEntry)
var dispatchers sync.WaitGroup
for i := 0; i < concurrency; i++ {
dispatchers.Add(1)
go c.dispatcher(jobs, op, &dispatchers)
}
defer func() {
close(jobs) // signal dispatchers to exit
dispatchers.Wait() // wait for dispatchers to end
}()
for {
var arr [maxBatchSize]genericEntry
var batch = arr[:0]
for {
select {
case entry, ok := <-queue:
if !ok {
// channel closed as Stop() was called
// drain the accumulated partial/full batch
if len(batch) > 0 {
jobs <- batch
}
return
}
batch = append(batch, entry)
if len(batch) != maxBatchSize {
// batch hasn't filled up yet, continue to wait
continue
}
case <-ticker.C:
if len(batch) < 1 {
// time's up but nothing to dispatch, continue to wait
continue
}
}
jobs <- batch
break // break inner loop, create a new batch
}
}
}
func (c *BufferedClient) dispatcher(batches chan []genericEntry, op sqsOp, wg *sync.WaitGroup) {
defer wg.Done()
for batch := range batches {
c.dispatchBatch(batch, op)
}
}
func (c *BufferedClient) dispatchBatch(batch []genericEntry, op sqsOp) {
switch op {
case opSend:
var arr [maxBatchSize]types.SendMessageBatchRequestEntry
var entries = arr[:0]
for _, ge := range batch {
entries = append(entries, ge.sendReq)
}
c.sendMessageBatch(entries)
case opDelete:
var arr [maxBatchSize]types.DeleteMessageBatchRequestEntry
var entries = arr[:0]
for _, ge := range batch {
entries = append(entries, ge.delReq)
}
c.deleteMessageBatch(entries)
}
}
func (c *BufferedClient) sendMessageBatch(entries []types.SendMessageBatchRequestEntry) {
resp, err := c.SQSClient.SendMessageBatch(context.TODO(), &sqs.SendMessageBatchInput{
Entries: entries,
QueueUrl: aws.String(c.QueueURL),
})
atomic.AddUint64(&c.stats.SendMessageBatchCalls, 1)
atomic.AddUint64(&c.stats.MessagesSent, uint64(len(entries)))
if c.OnSendMessageBatch != nil {
c.OnSendMessageBatch(resp, err)
}
}
func (c *BufferedClient) deleteMessageBatch(entries []types.DeleteMessageBatchRequestEntry) {
resp, err := c.SQSClient.DeleteMessageBatch(context.TODO(), &sqs.DeleteMessageBatchInput{
Entries: entries,
QueueUrl: aws.String(c.QueueURL),
})
atomic.AddUint64(&c.stats.DeleteMessageBatchCalls, 1)
atomic.AddUint64(&c.stats.MessagesDeleted, uint64(len(entries)))
if c.OnDeleteMessageBatch != nil {
c.OnDeleteMessageBatch(resp, err)
}
}
// disabledTicker returns a ticker that is stopped and shall never tick.
func disabledTicker() *time.Ticker {
ticker := time.NewTicker(1 * time.Hour)
ticker.Stop()
return ticker
}