Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Simplify cache item expiration check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Baliedge committed Mar 13, 2024
1 parent f51861d commit d55016d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
12 changes: 6 additions & 6 deletions algorithms.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 16 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions gubernator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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).
Expand Down
11 changes: 1 addition & 10 deletions lrucache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d55016d

Please sign in to comment.