-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup_delete.go
144 lines (126 loc) · 3.07 KB
/
cleanup_delete.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
package sqsconsumer
import (
"sync"
"time"
"github.com/aws/aws-sdk-go/service/sqs"
"golang.org/x/net/context"
)
type deleteQueue struct {
queue chan *sqs.Message
sync.Mutex
entries []*sqs.DeleteMessageBatchRequestEntry
svc *SQSService
drainTimeout time.Duration
accumulationTimeout time.Duration
}
// NewBatchDeleter starts a batch deleter routine that deletes messages after they are sent to the returned channel
func NewBatchDeleter(ctx context.Context, wg *sync.WaitGroup, s *SQSService, every, drainTimeout time.Duration) chan<- *sqs.Message {
dq := &deleteQueue{
svc: s,
accumulationTimeout: every,
drainTimeout: drainTimeout,
queue: make(chan *sqs.Message),
}
wg.Add(1)
go dq.start(ctx, wg)
return dq.queue
}
func (dq *deleteQueue) addToPendingDeletes(msg *sqs.Message) {
dq.Lock()
defer dq.Unlock()
for _, e := range dq.entries {
if *msg.MessageId == *e.Id {
return
}
}
dq.entries = append(dq.entries, &sqs.DeleteMessageBatchRequestEntry{
Id: msg.MessageId,
ReceiptHandle: msg.ReceiptHandle,
})
}
// deleteBatch deletes a batch of messages and returns the list of messages that failed to delete or an error for overall failure.
func (dq *deleteQueue) deleteBatch(msgs []*sqs.DeleteMessageBatchRequestEntry) ([]*sqs.DeleteMessageBatchRequestEntry, error) {
req := &sqs.DeleteMessageBatchInput{
QueueUrl: dq.svc.URL,
Entries: msgs,
}
resp, err := dq.svc.Svc.DeleteMessageBatch(req)
if err != nil {
dq.svc.Logger("Error deleting messages: %s", err)
return nil, err
}
var failed []*sqs.DeleteMessageBatchRequestEntry
for _, f := range resp.Failed {
for _, m := range msgs {
if *m.Id == *f.Id {
failed = append(failed, m)
break
}
}
}
return failed, nil
}
// deleteFromPending prepares a batch of messages and deletes them
func (dq *deleteQueue) deleteFromPending() {
dq.Lock()
defer dq.Unlock()
n := len(dq.entries)
if n > awsBatchSizeLimit {
n = awsBatchSizeLimit
}
fails, err := dq.deleteBatch(dq.entries[:n])
if err != nil {
dq.svc.Logger("Error deleting batch: %s", err)
return
}
dq.entries = dq.entries[n:]
if len(fails) > 0 {
dq.entries = append(dq.entries, fails...)
}
}
func (dq *deleteQueue) start(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
// read from the delete queue accumulating batches and running delete every 10 items or 250ms
for {
select {
case msg := <-dq.queue:
dq.addToPendingDeletes(msg)
dq.Lock()
n := len(dq.entries)
dq.Unlock()
if n >= awsBatchSizeLimit {
dq.deleteFromPending()
}
case <-time.After(dq.accumulationTimeout):
dq.Lock()
n := len(dq.entries)
dq.Unlock()
if n > 0 {
dq.deleteFromPending()
}
}
select {
case <-ctx.Done():
dq.drain()
// drain the delete queue and return
go func() {
for {
<-dq.queue
}
}()
return
default:
}
}
}
func (dq *deleteQueue) drain() {
for {
select {
case msg := <-dq.queue:
dq.addToPendingDeletes(msg)
dq.deleteFromPending()
case <-time.After(dq.drainTimeout):
return
}
}
}