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
// 回傳物件已經放入幾次(重試了幾次,白話一點呼叫NumRequeues幾次)
NumRequeues(iteminterface{})int}
看完了抽象的定義之後,必須要回過來看 Item Fast Slow Rate Limiter queue 實際物件定義了哪些屬性
// ItemFastSlowRateLimiter does a quick retry for a certain number of attempts, then a slow retry after that
typeItemFastSlowRateLimiterstruct{failuresLocksync.Mutex//鎖,防止資源競爭
failuresmap[interface{}]int//計算某個物件呼叫了延遲的次數
maxFastAttemptsint//最大短嘗試次數
fastDelaytime.Duration//短延遲時間
slowDelaytime.Duration//長延遲時間
}
看完了資料結構我們接著來看 Item Fast Slow Rate Limiter 實作的方法,與初始化方法。
funcNewRateLimitingQueue(rateLimiterRateLimiter)RateLimitingInterface{return&rateLimitingType{DelayingInterface:NewDelayingQueue(),//前一小節有提到過delating work queue的newfunction
rateLimiter:rateLimiter,//自行實作的rateLimiter
}}