type Type struct{// queue defines the order in which we will work on items. Every// element of queue should be in the dirty set and not in the// processing set.
queue []t # 队列 被处理的队列
// dirty defines all of the items that need to be processed.
dirty set # 标记需要处理的item,可用于去重 确保同一个消息在同一时间只有一个处理
// Things that are currently being processed are in the processing set.// These things may be simultaneously in the dirty set. When we finish// processing something and remove it from this set, we'll check if// it's in the dirty set, and if so, add it to the queue.
processing set
cond *sync.Cond
shuttingDown bool
metrics queueMetrics
unfinishedWorkUpdatePeriod time.Duration
clock clock.Clock
}
type DelayingInterface interface{
Interface # 使用FIFO 队列上进行封装一个ADDafter
// AddAfter adds an item to the workqueue after the indicated duration has passedAddAfter(item interface{}, duration time.Duration)}
AddAfter
func(q *delayingType)AddAfter(item interface{}, duration time.Duration){// don't add if we're already shutting downif q.ShuttingDown(){return}
q.metrics.retry()// immediately add things with no delay
# duration 小于等于0 直接添加到queue里面
if duration <=0{
q.Add(item)return}select{case<-q.stopCh:// unblock if ShutDown() is calledcase q.waitingForAddCh <-&waitFor{data: item, readyAt: q.clock.Now().Add(duration)}:// 等待duration时间在添加到queue
}}
限速队列
封装
// RateLimitingInterface is an interface that rate limits items being added to the queue.type RateLimitingInterface interface{
DelayingInterface # 延迟队列上面进行封装
// AddRateLimited adds an item to the workqueue after the rate limiter says it's okAddRateLimited(item interface{})// Forget indicates that an item is finished being retried. Doesn't matter whether it's for perm failing// or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you// still have to call `Done` on the queue.Forget(item interface{})// NumRequeues returns back how many times the item was requeuedNumRequeues(item interface{})int}