Skip to content

Commit

Permalink
fix: HitQuota should be returned instead of Allowed when limit is equ…
Browse files Browse the repository at this point in the history
…al to 1. (zeromicro#1581)
  • Loading branch information
alariczq authored Mar 4, 2022
1 parent 0c35f39 commit 96c128c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/limit/periodlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ local window = tonumber(ARGV[2])
local current = redis.call("INCRBY", KEYS[1], 1)
if current == 1 then
redis.call("expire", KEYS[1], window)
return 1
elseif current < limit then
end
if current < limit then
return 1
elseif current == limit then
return 2
Expand Down
10 changes: 10 additions & 0 deletions core/limit/periodlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ func testPeriodLimit(t *testing.T, opts ...PeriodOption) {
assert.Equal(t, 1, hitQuota)
assert.Equal(t, total-quota, overQuota)
}

func TestQuotaFull(t *testing.T) {
s, err := miniredis.Run()
assert.Nil(t, err)

l := NewPeriodLimit(1, 1, redis.New(s.Addr()), "periodlimit")
val, err := l.Take("first")
assert.Nil(t, err)
assert.Equal(t, HitQuota, val)
}

0 comments on commit 96c128c

Please sign in to comment.