// RateLimitingInterface is an interface that rate limits items being added to the queue.
//RateLimiting interface 結合了 delaying work queue ,透過一些方法計算 物件的延遲速率
// 再交由 delaying work queue 丟入Heap 最後放入 common queue中。
typeRateLimitingInterfaceinterface{DelayingInterface// delaying work queue 前一篇有介紹過
// AddRateLimited adds an item to the workqueue after the rate limiter says it's ok
AddRateLimited(iteminterface{})// 對某個物件加入延遲
// 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(iteminterface{})// 表示某個物件已經做了
// NumRequeues returns back how many times the item was requeued
NumRequeues(iteminterface{})int//計算某個物件調用queueAddRateLimited的次數
}
typeRateLimiterinterface{// When gets an item and gets to decide how long that item should wait
//當一個物件放入的時候,需要回傳延遲多久(可自定義規則,等等會看到)
When(iteminterface{})time.Duration// Forget indicates that an item is finished being retried. Doesn't matter whether its for perm failing
// or for success, we'll stop tracking it
//當一個物件完成的時候可以,要忘記曾經延遲過(重新計算)
Forget(iteminterface{})// NumRequeues returns back how many failures the item has had
// 回傳物件已經放入幾次(重試了幾次,白話一點呼叫whem幾次)
NumRequeues(iteminterface{})int}
看到這裡想必大家一定亂了,我幫大家整理一下個類別之間的關係。
看圖說故事的時間到了
Interface 繼承關係
1. Interface 為 common work queue 的介面(先用介面表示抽象方法,這裡就不用Interface會搞混....)
2. DelayingInterface 為 delaying work queue 的介面,這個介面繼承了Interface
3. RateLimitingInterface 繼承了 delaying work queue 的介面
// NewRateLimitingQueue constructs a new workqueue with rateLimited queuing ability
// Remember to call Forget! If you don't, you may end up tracking failures forever.
//使用者在使用RateLimiting的時候可以傳入自己實作的RateLimiter,此時使用預設的delay work queue。
funcNewRateLimitingQueue(rateLimiterRateLimiter)RateLimitingInterface{return&rateLimitingType{DelayingInterface:NewDelayingQueue(),//前一小節有提到過delating work queue的newfunction
rateLimiter:rateLimiter,//自行實作的rateLimiter
}}//使用者在使用RateLimiting的時候可以傳入自己實作的RateLimiter,此時使用預設的delaying work queue並且可以設定delaying work queue的metric name。
funcNewNamedRateLimitingQueue(rateLimiterRateLimiter,namestring)RateLimitingInterface{return&rateLimitingType{DelayingInterface:NewNamedDelayingQueue(name),//前一小節有提到delating work queue的newfunction(可以設定metric name)
rateLimiter:rateLimiter,//自行實作的rateLimiter
}}