diff --git a/algorithms.go b/algorithms.go index 61fa1544..17af8f03 100644 --- a/algorithms.go +++ b/algorithms.go @@ -146,18 +146,18 @@ func tokenBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat rl.ResetTime = expire } - if s != nil && reqState.IsOwner { - defer func() { - s.OnChange(ctx, r, item) - }() - } - // Client is only interested in retrieving the current status or // updating the rate limit config. if r.Hits == 0 { return rl, nil } + if s != nil && reqState.IsOwner { + defer func() { + s.OnChange(ctx, r, item) + }() + } + // If we are already at the limit. if rl.Remaining == 0 && r.Hits > 0 { trace.SpanFromContext(ctx).AddEvent("Already over the limit") diff --git a/cache.go b/cache.go index 163627d2..0fd431a5 100644 --- a/cache.go +++ b/cache.go @@ -39,3 +39,19 @@ type CacheItem struct { // for the latest rate limit data. InvalidAt int64 } + +func (item *CacheItem) IsExpired() bool { + now := MillisecondNow() + + // If the entry is invalidated + if item.InvalidAt != 0 && item.InvalidAt < now { + return true + } + + // If the entry has expired, remove it from the cache + if item.ExpireAt < now { + return true + } + + return false +} diff --git a/gubernator.go b/gubernator.go index 55f3f9f2..f81742a2 100644 --- a/gubernator.go +++ b/gubernator.go @@ -318,7 +318,7 @@ func (s *V1Instance) asyncRequest(ctx context.Context, req *AsyncReq) { funcTimer := prometheus.NewTimer(metricFuncTimeDuration.WithLabelValues("V1Instance.asyncRequest")) defer funcTimer.ObserveDuration() - reqState := RateLimitReqState{IsOwner: false} + reqState := RateLimitReqState{IsOwner: req.Peer.Info().IsOwner} resp := AsyncResp{ Idx: req.Idx, } @@ -337,7 +337,7 @@ func (s *V1Instance) asyncRequest(ctx context.Context, req *AsyncReq) { // If we are attempting again, the owner of this rate limit might have changed to us! if attempts != 0 { - if req.Peer.Info().IsOwner { + if reqState.IsOwner { resp.Resp, err = s.getLocalRateLimit(ctx, req.Req, reqState) if err != nil { s.log.WithContext(ctx). diff --git a/lrucache.go b/lrucache.go index 09bc36ba..03867209 100644 --- a/lrucache.go +++ b/lrucache.go @@ -112,16 +112,7 @@ func (c *LRUCache) GetItem(key string) (item *CacheItem, ok bool) { if ele, hit := c.cache[key]; hit { entry := ele.Value.(*CacheItem) - now := MillisecondNow() - // If the entry is invalidated - if entry.InvalidAt != 0 && entry.InvalidAt < now { - c.removeElement(ele) - metricCacheAccess.WithLabelValues("miss").Add(1) - return - } - - // If the entry has expired, remove it from the cache - if entry.ExpireAt < now { + if entry.IsExpired() { c.removeElement(ele) metricCacheAccess.WithLabelValues("miss").Add(1) return