From 96c128c58a338453375e49c47a0d0b91fb9b3a0c Mon Sep 17 00:00:00 2001 From: qi <51255100+imzhongqi@users.noreply.github.com> Date: Fri, 4 Mar 2022 16:14:45 +0800 Subject: [PATCH] fix: HitQuota should be returned instead of Allowed when limit is equal to 1. (#1581) --- core/limit/periodlimit.go | 4 ++-- core/limit/periodlimit_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/limit/periodlimit.go b/core/limit/periodlimit.go index edb737e8..73fb9d00 100644 --- a/core/limit/periodlimit.go +++ b/core/limit/periodlimit.go @@ -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 diff --git a/core/limit/periodlimit_test.go b/core/limit/periodlimit_test.go index 8fc4403c..c23c14db 100644 --- a/core/limit/periodlimit_test.go +++ b/core/limit/periodlimit_test.go @@ -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) +}