Add guard for `nil` in our `RateLimiter`.

This commit is contained in:
Guo Xiang Tan 2018-03-01 13:20:42 +08:00
parent 5d9f9c2614
commit 81ca3677f7
2 changed files with 5 additions and 2 deletions

View File

@ -83,7 +83,10 @@ class RateLimiter
def performed! def performed!
return if rate_unlimited? return if rate_unlimited?
now = Time.now.to_i now = Time.now.to_i
if max <= 0 || (eval_lua(PERFORM_LUA, PERFORM_LUA_SHA, [prefixed_key], [now, @secs, @max]) == 0)
if ((max || 0) <= 0) ||
(eval_lua(PERFORM_LUA, PERFORM_LUA_SHA, [prefixed_key], [now, @secs, @max]) == 0)
raise RateLimiter::LimitExceeded.new(seconds_to_wait, @type) raise RateLimiter::LimitExceeded.new(seconds_to_wait, @type)
end end
rescue Redis::CommandError => e rescue Redis::CommandError => e

View File

@ -96,7 +96,7 @@ describe RateLimiter do
context 'max is less than or equal to zero' do context 'max is less than or equal to zero' do
it 'should raise the right error' do it 'should raise the right error' do
[-1, 0].each do |max| [-1, 0, nil].each do |max|
expect do expect do
RateLimiter.new(user, "a", max, 60).performed! RateLimiter.new(user, "a", max, 60).performed!
end.to raise_error(RateLimiter::LimitExceeded) end.to raise_error(RateLimiter::LimitExceeded)