in lib/throttle.go [38:70]
func (c *Throttle) OK(key interface{}) (bool, error) {
if c.maxRatePerItem <= 0 {
return true, nil
}
c.mu.Lock()
defer c.mu.Unlock()
// If the limiter is not in the cache for the given key
// check for the cache limiter. If it is below the maximum,
// then create a limiter, add it to the cache and allocate a bucket.
value, ok := c.lru.Get(key)
if !ok {
if c.cacheLimiter.Allow() {
limiter := rate.NewLimiter(rate.Limit(c.maxRatePerItem), c.maxRatePerItem)
c.lru.Add(key, limiter)
return limiter.Allow(), nil
}
err := fmt.Errorf("Cache invalidation is too fast (max: %d item/sec) - throttling", c.cacheRate)
return false, err
}
// So the limiter object is in the cache. Try to allocate a bucket.
limiter := value.(*rate.Limiter)
if !limiter.Allow() {
err := fmt.Errorf("Request rate is too high for %v (max: %d req/sec) - throttling", key, c.maxRatePerItem)
return false, err
}
return true, nil
}