From 247a0b3ea1960da51e12cc471a4d1129f0f81b8f Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Thu, 23 May 2013 17:18:59 -0700 Subject: [PATCH] small refactor of RateLimiter for clarity --- lib/rate_limiter.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/rate_limiter.rb b/lib/rate_limiter.rb index 3c027294e..5d794e9c9 100644 --- a/lib/rate_limiter.rb +++ b/lib/rate_limiter.rb @@ -21,18 +21,11 @@ class RateLimiter end def can_perform? - return true if RateLimiter.disabled? - return true if @user.staff? - - result = $redis.get(@key) - return true if result.blank? - return true if result.to_i < @max - false + rate_unlimited? || is_under_limit? end def performed! - return if RateLimiter.disabled? - return if @user.staff? + return if rate_unlimited? result = $redis.incr(@key).to_i $redis.expire(@key, @secs) if result == 1 @@ -50,4 +43,13 @@ class RateLimiter $redis.decr(@key) end + private + + def is_under_limit? + $redis.get(@key).to_i < @max + end + + def rate_unlimited? + !!(RateLimiter.disabled? || @user.staff?) + end end