1
- package controllerworkqueue
1
+ package priorityqueue
2
2
3
3
import (
4
4
"sync"
@@ -56,7 +56,7 @@ func New[T comparable](name string, o ...Opt[T]) PriorityQueue[T] {
56
56
opts .MetricProvider = metrics.WorkqueueMetricsProvider {}
57
57
}
58
58
59
- cwq := & controllerworkqueue [T ]{
59
+ cwq := & priorityqueue [T ]{
60
60
items : map [T ]* item [T ]{},
61
61
queue : btree .NewG (32 , less [T ]),
62
62
// itemOrWaiterAdded indicates that an item or
@@ -77,7 +77,7 @@ func New[T comparable](name string, o ...Opt[T]) PriorityQueue[T] {
77
77
return wrapWithMetrics (cwq , name , opts .MetricProvider )
78
78
}
79
79
80
- type controllerworkqueue [T comparable ] struct {
80
+ type priorityqueue [T comparable ] struct {
81
81
// lock has to be acquired for any access to either items or queue
82
82
lock sync.Mutex
83
83
items map [T ]* item [T ]
@@ -110,7 +110,7 @@ type controllerworkqueue[T comparable] struct {
110
110
tick func (time.Duration ) <- chan time.Time
111
111
}
112
112
113
- func (w * controllerworkqueue [T ]) AddWithOpts (o AddOpts , items ... T ) {
113
+ func (w * priorityqueue [T ]) AddWithOpts (o AddOpts , items ... T ) {
114
114
w .lock .Lock ()
115
115
defer w .lock .Unlock ()
116
116
@@ -155,14 +155,14 @@ func (w *controllerworkqueue[T]) AddWithOpts(o AddOpts, items ...T) {
155
155
}
156
156
}
157
157
158
- func (w * controllerworkqueue [T ]) notifyItemOrWaiterAdded () {
158
+ func (w * priorityqueue [T ]) notifyItemOrWaiterAdded () {
159
159
select {
160
160
case w .itemOrWaiterAdded <- struct {}{}:
161
161
default :
162
162
}
163
163
}
164
164
165
- func (w * controllerworkqueue [T ]) spin () {
165
+ func (w * priorityqueue [T ]) spin () {
166
166
blockForever := make (chan time.Time )
167
167
var nextReady <- chan time.Time
168
168
nextReady = blockForever
@@ -216,19 +216,19 @@ func (w *controllerworkqueue[T]) spin() {
216
216
}
217
217
}
218
218
219
- func (w * controllerworkqueue [T ]) Add (item T ) {
219
+ func (w * priorityqueue [T ]) Add (item T ) {
220
220
w .AddWithOpts (AddOpts {}, item )
221
221
}
222
222
223
- func (w * controllerworkqueue [T ]) AddAfter (item T , after time.Duration ) {
223
+ func (w * priorityqueue [T ]) AddAfter (item T , after time.Duration ) {
224
224
w .AddWithOpts (AddOpts {After : after }, item )
225
225
}
226
226
227
- func (w * controllerworkqueue [T ]) AddRateLimited (item T ) {
227
+ func (w * priorityqueue [T ]) AddRateLimited (item T ) {
228
228
w .AddWithOpts (AddOpts {RateLimited : true }, item )
229
229
}
230
230
231
- func (w * controllerworkqueue [T ]) GetWithPriority () (_ T , priority int , shutdown bool ) {
231
+ func (w * priorityqueue [T ]) GetWithPriority () (_ T , priority int , shutdown bool ) {
232
232
w .waiters .Add (1 )
233
233
234
234
w .notifyItemOrWaiterAdded ()
@@ -237,40 +237,40 @@ func (w *controllerworkqueue[T]) GetWithPriority() (_ T, priority int, shutdown
237
237
return item .key , item .priority , w .shutdown .Load ()
238
238
}
239
239
240
- func (w * controllerworkqueue [T ]) Get () (item T , shutdown bool ) {
240
+ func (w * priorityqueue [T ]) Get () (item T , shutdown bool ) {
241
241
key , _ , shutdown := w .GetWithPriority ()
242
242
return key , shutdown
243
243
}
244
244
245
- func (w * controllerworkqueue [T ]) Forget (item T ) {
245
+ func (w * priorityqueue [T ]) Forget (item T ) {
246
246
w .rateLimiter .Forget (item )
247
247
}
248
248
249
- func (w * controllerworkqueue [T ]) NumRequeues (item T ) int {
249
+ func (w * priorityqueue [T ]) NumRequeues (item T ) int {
250
250
return w .rateLimiter .NumRequeues (item )
251
251
}
252
252
253
- func (w * controllerworkqueue [T ]) ShuttingDown () bool {
253
+ func (w * priorityqueue [T ]) ShuttingDown () bool {
254
254
return w .shutdown .Load ()
255
255
}
256
256
257
- func (w * controllerworkqueue [T ]) Done (item T ) {
257
+ func (w * priorityqueue [T ]) Done (item T ) {
258
258
w .lockedLock .Lock ()
259
259
defer w .lockedLock .Unlock ()
260
260
w .locked .Delete (item )
261
261
w .notifyItemOrWaiterAdded ()
262
262
}
263
263
264
- func (w * controllerworkqueue [T ]) ShutDown () {
264
+ func (w * priorityqueue [T ]) ShutDown () {
265
265
w .shutdown .Store (true )
266
266
close (w .done )
267
267
}
268
268
269
- func (w * controllerworkqueue [T ]) ShutDownWithDrain () {
269
+ func (w * priorityqueue [T ]) ShutDownWithDrain () {
270
270
w .ShutDown ()
271
271
}
272
272
273
- func (w * controllerworkqueue [T ]) Len () int {
273
+ func (w * priorityqueue [T ]) Len () int {
274
274
w .lock .Lock ()
275
275
defer w .lock .Unlock ()
276
276
@@ -306,10 +306,10 @@ type item[T comparable] struct {
306
306
readyAt * time.Time
307
307
}
308
308
309
- func wrapWithMetrics [T comparable ](q * controllerworkqueue [T ], name string , provider workqueue.MetricsProvider ) PriorityQueue [T ] {
309
+ func wrapWithMetrics [T comparable ](q * priorityqueue [T ], name string , provider workqueue.MetricsProvider ) PriorityQueue [T ] {
310
310
mwq := & metricWrappedQueue [T ]{
311
- controllerworkqueue : q ,
312
- metrics : newQueueMetrics [T ](provider , name , clock.RealClock {}),
311
+ priorityqueue : q ,
312
+ metrics : newQueueMetrics [T ](provider , name , clock.RealClock {}),
313
313
}
314
314
315
315
go mwq .updateUnfinishedWorkLoop ()
@@ -318,19 +318,19 @@ func wrapWithMetrics[T comparable](q *controllerworkqueue[T], name string, provi
318
318
}
319
319
320
320
type metricWrappedQueue [T comparable ] struct {
321
- * controllerworkqueue [T ]
321
+ * priorityqueue [T ]
322
322
metrics queueMetrics [T ]
323
323
}
324
324
325
325
func (m * metricWrappedQueue [T ]) AddWithOpts (o AddOpts , items ... T ) {
326
326
for _ , item := range items {
327
327
m .metrics .add (item )
328
328
}
329
- m .controllerworkqueue .AddWithOpts (o , items ... )
329
+ m .priorityqueue .AddWithOpts (o , items ... )
330
330
}
331
331
332
332
func (m * metricWrappedQueue [T ]) GetWithPriority () (T , int , bool ) {
333
- item , priority , shutdown := m .controllerworkqueue .GetWithPriority ()
333
+ item , priority , shutdown := m .priorityqueue .GetWithPriority ()
334
334
m .metrics .get (item )
335
335
return item , priority , shutdown
336
336
}
@@ -342,14 +342,14 @@ func (m *metricWrappedQueue[T]) Get() (T, bool) {
342
342
343
343
func (m * metricWrappedQueue [T ]) Done (item T ) {
344
344
m .metrics .done (item )
345
- m .controllerworkqueue .Done (item )
345
+ m .priorityqueue .Done (item )
346
346
}
347
347
348
348
func (m * metricWrappedQueue [T ]) updateUnfinishedWorkLoop () {
349
349
t := time .NewTicker (time .Millisecond )
350
350
defer t .Stop ()
351
351
for range t .C {
352
- if m .controllerworkqueue .ShuttingDown () {
352
+ if m .priorityqueue .ShuttingDown () {
353
353
return
354
354
}
355
355
m .metrics .updateUnfinishedWork ()
0 commit comments