client-go workqueue provides a non-interruptible queue that works in the following pattern
func Run(ctx context.Context, queue workqueue.RateLimitingInterface) {
for i := 0; i < 4; i++ {
go wait.Until(func() { worker(queue) }, time.Second, ctx.Done())
}
go fakeResourceInformer(ctx, queue)
<-ctx.Done()
queue.ShutDown()
}
func worker(queue workqueue.RateLimitingInterface) {
for {
item, shutdown := queue.Get()
if shutdown {
return
}
if item != nil {
defer queue.Done(item)
if err := processItem(item); err != nil {
queue.AddRateLimited(item)
}
}
}
}
However, if I want to shut down a worker goroutine without shutting down the queue Dynamic controller scaling · Issue #2576 · kubernetes-sigs/controller-runtime · GitHub, it may not be easy since the worker could be stuck at the blocking queue.Get
. My question is, what is the best practice to stop a running worker to reduce resource usage? Why doesn’t the official provide a nonblocking queue.Get(ctx)
or something interruptible?