-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbonded_queue.go
More file actions
301 lines (273 loc) · 8.3 KB
/
bonded_queue.go
File metadata and controls
301 lines (273 loc) · 8.3 KB
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
// originally copied from
// https://github.com/jaegertracing/jaeger/blob/v1.55.0/pkg/queue/bounded_queue.go
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package queue
import (
"sync"
"sync/atomic"
"time"
"unsafe"
)
// Consumer consumes data from a bounded queue
type Consumer interface {
Consume(item interface{}) error
}
// BoundedQueue implements a producer-consumer exchange similar to a ring buffer queue,
// where the queue is bounded and if it fills up due to slow consumers, the new items written by
// the producer force the earliest items to be dropped. The implementation is actually based on
// channels, with a special Reaper goroutine that wakes up when the queue is full and consumers
// the items from the top of the queue until its size drops back to maxSize
type BoundedQueue struct {
workers int
stopWG sync.WaitGroup
size atomic.Int32
capacity atomic.Uint32
stopped atomic.Uint32
items *chan interface{}
onDroppedItem func(item interface{})
factory func() Consumer
stopCh chan struct{}
retryConfig RetryConfig
}
// RetryConfig holds configuration for retrying failed items
type RetryConfig struct {
isEnabled bool
maxCount uint32
delay time.Duration
}
// NewBoundedQueue constructs the new queue of specified capacity, and with an optional
// callback for dropped items (e.g. useful to emit metrics).
func NewBoundedQueue(capacity int, onDroppedItem func(item interface{})) *BoundedQueue {
queue := make(chan interface{}, capacity)
bq := &BoundedQueue{
onDroppedItem: onDroppedItem,
items: &queue,
stopCh: make(chan struct{}),
}
bq.capacity.Store(uint32(capacity))
return bq
}
// NewBoundedQueue constructs the new queue of specified capacity, and with an optional
// callback for dropped items (e.g. useful to emit metrics).
func NewBoundedQueueWithRetry(capacity int, onDroppedItem func(item interface{}), retryMaxCount uint32, retryDelay time.Duration) *BoundedQueue {
queue := make(chan interface{}, capacity)
bq := &BoundedQueue{
onDroppedItem: onDroppedItem,
items: &queue,
stopCh: make(chan struct{}),
retryConfig: RetryConfig{
isEnabled: true,
maxCount: retryMaxCount,
delay: retryDelay,
},
}
bq.capacity.Store(uint32(capacity))
return bq
}
// StartConsumersWithFactory creates a given number of consumers consuming items
// from the queue in separate goroutines.
func (q *BoundedQueue) StartConsumersWithFactory(num int, factory func() Consumer) {
q.workers = num
q.factory = factory
var startWG sync.WaitGroup
for i := 0; i < q.workers; i++ {
q.stopWG.Add(1)
startWG.Add(1)
go func() {
startWG.Done()
defer q.stopWG.Done()
consumer := q.factory()
queue := *q.items
if q.retryConfig.isEnabled {
// Create a local queue for retrying items at the front
var retryItem interface{}
hasRetry := false
retryDelay := time.Millisecond * 100
retryAttemptNumber := uint32(0)
for {
// If we have a retry item, process it first
if hasRetry {
if q.retryConfig.maxCount > 0 && retryAttemptNumber >= q.retryConfig.maxCount {
// Max retries exceeded, drop the item
q.size.Add(-1)
if q.onDroppedItem != nil {
q.onDroppedItem(retryItem)
}
hasRetry = false
retryItem = nil
retryDelay = time.Millisecond * 100
retryAttemptNumber = 0
continue
}
select {
case <-time.After(retryDelay):
retryAttemptNumber++
err := consumer.Consume(retryItem)
if err != nil {
// Still failing, increase delay (exponential backoff)
retryDelay = retryDelay * 2
if retryDelay > q.retryConfig.delay {
retryDelay = q.retryConfig.delay
}
} else {
// Success! Clear retry state and decrement size
hasRetry = false
retryItem = nil
retryDelay = time.Millisecond * 100
retryAttemptNumber = 0
q.size.Add(-1)
}
case <-q.stopCh:
// Queue is closing
if hasRetry {
q.size.Add(-1)
}
return
}
} else {
// No retry item, get next from queue
select {
case item, ok := <-queue:
if ok {
err := consumer.Consume(item)
if err != nil {
// Failed, set as retry item
retryItem = item
hasRetry = true
} else {
// Success, decrement size
q.size.Add(-1)
}
} else {
// channel closed, finish worker
return
}
case <-q.stopCh:
// the whole queue is closing, finish worker
return
}
}
}
} else {
for {
select {
case item, ok := <-queue:
if ok {
_ = consumer.Consume(item)
q.size.Add(-1)
} else {
// channel closed, finish worker
return
}
case <-q.stopCh:
// the whole queue is closing, finish worker
return
}
}
}
}()
}
startWG.Wait()
}
// ConsumerFunc is an adapter to allow the use of
// a consume function callback as a Consumer.
type ConsumerFunc func(item interface{}) error
// Consume calls c(item)
func (c ConsumerFunc) Consume(item interface{}) error {
return c(item)
}
// StartConsumers starts a given number of goroutines consuming items from the queue
// and passing them into the consumer callback.
func (q *BoundedQueue) StartConsumers(num int, callback func(item interface{}) error) {
q.StartConsumersWithFactory(num, func() Consumer {
return ConsumerFunc(callback)
})
}
// StopConsumers stops all the consumers
// should be started again with StartConsumers func
func (q *BoundedQueue) StopConsumers() {
q.stopWG.Done()
}
// Produce is used by the producer to submit new item to the queue. Returns false in case of queue overflow.
func (q *BoundedQueue) Produce(item interface{}) bool {
if q.stopped.Load() != 0 {
if q.onDroppedItem != nil {
q.onDroppedItem(item)
}
return false
}
// we might have two concurrent backing queues at the moment
// their combined size is stored in q.size, and their combined capacity
// should match the capacity of the new queue
if q.Size() >= q.Capacity() {
// note that all items will be dropped if the capacity is 0
if q.onDroppedItem != nil {
q.onDroppedItem(item)
}
return false
}
q.size.Add(1)
select {
case *q.items <- item:
return true
default:
// should not happen, as overflows should have been captured earlier
q.size.Add(-1)
if q.onDroppedItem != nil {
q.onDroppedItem(item)
}
return false
}
}
// Stop stops all consumers, as well as the length reporter if started,
// and releases the items channel. It blocks until all consumers have stopped.
func (q *BoundedQueue) Stop() {
// Use atomic CAS to ensure Stop is only executed once
if q.stopped.CompareAndSwap(0, 1) {
close(q.stopCh)
q.stopWG.Wait()
close(*q.items)
}
}
// Size returns the current size of the queue
func (q *BoundedQueue) Size() int {
return int(q.size.Load())
}
// Capacity returns capacity of the queue
func (q *BoundedQueue) Capacity() int {
return int(q.capacity.Load())
}
// Resize changes the capacity of the queue, returning whether the action was successful
func (q *BoundedQueue) Resize(capacity int) bool {
if capacity == q.Capacity() {
// noop
return false
}
previous := *q.items
queue := make(chan interface{}, capacity)
// swap queues
// #nosec
swapped := atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&q.items)), unsafe.Pointer(q.items), unsafe.Pointer(&queue))
if swapped {
// start a new set of consumers, based on the information given previously
q.StartConsumersWithFactory(q.workers, q.factory)
// gracefully drain the existing queue
close(previous)
// update the capacity
q.capacity.Store(uint32(capacity))
}
return swapped
}