FIX: Should flush rate limit keys before testing it
This commit is contained in:
parent
3a24df6956
commit
6e862e0e59
|
@ -63,9 +63,9 @@ class DiscourseRedis
|
||||||
@redis.del k
|
@redis.del k
|
||||||
end
|
end
|
||||||
|
|
||||||
def keys
|
def keys(pattern=nil)
|
||||||
len = DiscourseRedis.namespace.length + 1
|
len = DiscourseRedis.namespace.length + 1
|
||||||
@redis.keys("#{DiscourseRedis.namespace}:*").map{
|
@redis.keys("#{DiscourseRedis.namespace}:#{pattern || '*'}").map{
|
||||||
|k| k[len..-1]
|
|k| k[len..-1]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,8 @@ require_dependency 'rate_limiter/on_create_record'
|
||||||
# A redis backed rate limiter.
|
# A redis backed rate limiter.
|
||||||
class RateLimiter
|
class RateLimiter
|
||||||
|
|
||||||
|
KEY_PREFIX = "l-rate-limit:"
|
||||||
|
|
||||||
def self.disable
|
def self.disable
|
||||||
@disabled = true
|
@disabled = true
|
||||||
end
|
end
|
||||||
|
@ -17,9 +19,13 @@ class RateLimiter
|
||||||
@disabled || Rails.env.test?
|
@disabled || Rails.env.test?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.clear_all!
|
||||||
|
$redis.keys("#{KEY_PREFIX}:*").each { |k| $redis.del(k) }
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(user, key, max, secs)
|
def initialize(user, key, max, secs)
|
||||||
@user = user
|
@user = user
|
||||||
@key = "l-rate-limit:#{@user && @user.id}:#{key}"
|
@key = "#{KEY_PREFIX}:#{@user && @user.id}:#{key}"
|
||||||
@max = max
|
@max = max
|
||||||
@secs = secs
|
@secs = secs
|
||||||
end
|
end
|
||||||
|
@ -64,10 +70,10 @@ class RateLimiter
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_under_limit?
|
def is_under_limit?
|
||||||
# number of events in buffer less than max allowed? OR
|
# number of events in buffer less than max allowed? OR
|
||||||
($redis.llen(@key) < @max) ||
|
($redis.llen(@key) < @max) ||
|
||||||
# age bigger than silding window size?
|
# age bigger than silding window size?
|
||||||
(age_of_oldest > @secs)
|
(age_of_oldest > @secs)
|
||||||
end
|
end
|
||||||
|
|
||||||
def rate_unlimited?
|
def rate_unlimited?
|
||||||
|
|
|
@ -1245,6 +1245,7 @@ describe Topic do
|
||||||
SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)
|
SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)
|
||||||
RateLimiter.stubs(:rate_limit_create_topic).returns(100)
|
RateLimiter.stubs(:rate_limit_create_topic).returns(100)
|
||||||
RateLimiter.stubs(:disabled?).returns(false)
|
RateLimiter.stubs(:disabled?).returns(false)
|
||||||
|
RateLimiter.clear_all!
|
||||||
|
|
||||||
start = Time.now.tomorrow.beginning_of_day
|
start = Time.now.tomorrow.beginning_of_day
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue