Skip to content

Commit d3b1b89

Browse files
committed
fix: move redis expire window
1 parent 1cc00c9 commit d3b1b89

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

redis/redis.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ func (r *Redis) Increment(ctx context.Context, key string, incr int) error {
3030
return err
3131
}
3232

33-
if val == 1 {
34-
// If this hash was only just created, set its expiry.
35-
r.client.Expire(ctx, key, r.limitPeriod)
36-
} else if val >= int64(r.limit) {
33+
// check if current window has exceeded the limit
34+
if val >= int64(r.limit) {
3735
// Otherwise, check if just this fixed window counter period is over
3836
return ratelimit.ErrRateLimitExceeded(0, r.limit, r.limitPeriod, now.Add(r.limitPeriod))
3937
}
4038

39+
// create or move whole limit period window expiry
40+
r.client.Expire(ctx, key, r.limitPeriod)
41+
4142
// Get all the bucket values and sum them.
4243
vals, err := r.client.HGetAll(ctx, key).Result()
4344
if err != nil {

0 commit comments

Comments
 (0)