go.mod
module use-k8s-queue
go 1.19
require k8s.io/client-go v0.28.2
require (
github.com/go-logr/logr v1.2.4 // indirect
golang.org/x/time v0.3.0 // indirect
k8s.io/apimachinery v0.28.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
)
// k8s.io/api => k8s.io/api v0.0.0-20230915221828-1cac0b1ef7e3
replace k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230915221524-64708d3e9048
main.go
package main
import (
"fmt"
"k8s.io/client-go/util/workqueue"
)
func main() {
queue1 := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
queue1.Add("test")
queue1.ShutDown()
_, shutdown1 := queue1.Get()
if shutdown1 {
fmt.Println("queue1 has shutdown by Get check")
}
if queue1.ShuttingDown() {
fmt.Println("queue1 has shutdown by ShuttingDown check")
}
queue2 := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
queue2.ShutDown()
_, shutdown2 := queue1.Get()
if shutdown2 {
fmt.Println("queue2 has shutdown by Get check")
}
if queue2.ShuttingDown() {
fmt.Println("queue2 has shutdown by ShuttingDown check")
}
}
queue1在Get时还有元素,认为没有关闭,需要ShuttingDown方法才能认为已关闭。
queue2在Get时没有元素,认为已关闭,ShuttingDown方法依然认为已关闭。
不管是否有元素,ShuttingDown方法是判断队列是否关闭的正确方式。