diff --git a/lib/rate_limiter.rb b/lib/rate_limiter.rb index 0b3344606f3..a0bc4515be8 100644 --- a/lib/rate_limiter.rb +++ b/lib/rate_limiter.rb @@ -83,7 +83,7 @@ class RateLimiter def performed! return if rate_unlimited? now = Time.now.to_i - if eval_lua(PERFORM_LUA, PERFORM_LUA_SHA, [prefixed_key], [now, @secs, @max]) == 0 + if max <= 0 || (eval_lua(PERFORM_LUA, PERFORM_LUA_SHA, [prefixed_key], [now, @secs, @max]) == 0) raise RateLimiter::LimitExceeded.new(seconds_to_wait, @type) end rescue Redis::CommandError => e diff --git a/spec/components/rate_limiter_spec.rb b/spec/components/rate_limiter_spec.rb index ce9bed2a114..4ee586ed7db 100644 --- a/spec/components/rate_limiter_spec.rb +++ b/spec/components/rate_limiter_spec.rb @@ -93,6 +93,17 @@ describe RateLimiter do end end + context 'max is less than or equal to zero' do + + it 'should raise the right error' do + [-1, 0].each do |max| + expect do + RateLimiter.new(user, "a", max, 60).performed! + end.to raise_error(RateLimiter::LimitExceeded) + end + end + end + context "multiple calls" do before do rate_limiter.performed!