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

Commit d55016d

Browse files
committed
Simplify cache item expiration check.
1 parent f51861d commit d55016d

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

algorithms.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ func tokenBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
146146
rl.ResetTime = expire
147147
}
148148

149-
if s != nil && reqState.IsOwner {
150-
defer func() {
151-
s.OnChange(ctx, r, item)
152-
}()
153-
}
154-
155149
// Client is only interested in retrieving the current status or
156150
// updating the rate limit config.
157151
if r.Hits == 0 {
158152
return rl, nil
159153
}
160154

155+
if s != nil && reqState.IsOwner {
156+
defer func() {
157+
s.OnChange(ctx, r, item)
158+
}()
159+
}
160+
161161
// If we are already at the limit.
162162
if rl.Remaining == 0 && r.Hits > 0 {
163163
trace.SpanFromContext(ctx).AddEvent("Already over the limit")

cache.go

+16
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,19 @@ type CacheItem struct {
3939
// for the latest rate limit data.
4040
InvalidAt int64
4141
}
42+
43+
func (item *CacheItem) IsExpired() bool {
44+
now := MillisecondNow()
45+
46+
// If the entry is invalidated
47+
if item.InvalidAt != 0 && item.InvalidAt < now {
48+
return true
49+
}
50+
51+
// If the entry has expired, remove it from the cache
52+
if item.ExpireAt < now {
53+
return true
54+
}
55+
56+
return false
57+
}

gubernator.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (s *V1Instance) asyncRequest(ctx context.Context, req *AsyncReq) {
318318
funcTimer := prometheus.NewTimer(metricFuncTimeDuration.WithLabelValues("V1Instance.asyncRequest"))
319319
defer funcTimer.ObserveDuration()
320320

321-
reqState := RateLimitReqState{IsOwner: false}
321+
reqState := RateLimitReqState{IsOwner: req.Peer.Info().IsOwner}
322322
resp := AsyncResp{
323323
Idx: req.Idx,
324324
}
@@ -337,7 +337,7 @@ func (s *V1Instance) asyncRequest(ctx context.Context, req *AsyncReq) {
337337

338338
// If we are attempting again, the owner of this rate limit might have changed to us!
339339
if attempts != 0 {
340-
if req.Peer.Info().IsOwner {
340+
if reqState.IsOwner {
341341
resp.Resp, err = s.getLocalRateLimit(ctx, req.Req, reqState)
342342
if err != nil {
343343
s.log.WithContext(ctx).

lrucache.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,7 @@ func (c *LRUCache) GetItem(key string) (item *CacheItem, ok bool) {
112112
if ele, hit := c.cache[key]; hit {
113113
entry := ele.Value.(*CacheItem)
114114

115-
now := MillisecondNow()
116-
// If the entry is invalidated
117-
if entry.InvalidAt != 0 && entry.InvalidAt < now {
118-
c.removeElement(ele)
119-
metricCacheAccess.WithLabelValues("miss").Add(1)
120-
return
121-
}
122-
123-
// If the entry has expired, remove it from the cache
124-
if entry.ExpireAt < now {
115+
if entry.IsExpired() {
125116
c.removeElement(ele)
126117
metricCacheAccess.WithLabelValues("miss").Add(1)
127118
return

0 commit comments

Comments
 (0)