mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 09:12:45 +08:00
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
|
||||
end
|
||||
|
||||
def keys
|
||||
def keys(pattern=nil)
|
||||
len = DiscourseRedis.namespace.length + 1
|
||||
@redis.keys("#{DiscourseRedis.namespace}:*").map{
|
||||
@redis.keys("#{DiscourseRedis.namespace}:#{pattern || '*'}").map{
|
||||
|k| k[len..-1]
|
||||
}
|
||||
end
|
||||
|
|
|
@ -4,6 +4,8 @@ require_dependency 'rate_limiter/on_create_record'
|
|||
# A redis backed rate limiter.
|
||||
class RateLimiter
|
||||
|
||||
KEY_PREFIX = "l-rate-limit:"
|
||||
|
||||
def self.disable
|
||||
@disabled = true
|
||||
end
|
||||
|
@ -17,9 +19,13 @@ class RateLimiter
|
|||
@disabled || Rails.env.test?
|
||||
end
|
||||
|
||||
def self.clear_all!
|
||||
$redis.keys("#{KEY_PREFIX}:*").each { |k| $redis.del(k) }
|
||||
end
|
||||
|
||||
def initialize(user, key, max, secs)
|
||||
@user = user
|
||||
@key = "l-rate-limit:#{@user && @user.id}:#{key}"
|
||||
@key = "#{KEY_PREFIX}:#{@user && @user.id}:#{key}"
|
||||
@max = max
|
||||
@secs = secs
|
||||
end
|
||||
|
@ -64,10 +70,10 @@ class RateLimiter
|
|||
end
|
||||
|
||||
def is_under_limit?
|
||||
# number of events in buffer less than max allowed? OR
|
||||
($redis.llen(@key) < @max) ||
|
||||
# age bigger than silding window size?
|
||||
(age_of_oldest > @secs)
|
||||
# number of events in buffer less than max allowed? OR
|
||||
($redis.llen(@key) < @max) ||
|
||||
# age bigger than silding window size?
|
||||
(age_of_oldest > @secs)
|
||||
end
|
||||
|
||||
def rate_unlimited?
|
||||
|
|
|
@ -1245,6 +1245,7 @@ describe Topic do
|
|||
SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)
|
||||
RateLimiter.stubs(:rate_limit_create_topic).returns(100)
|
||||
RateLimiter.stubs(:disabled?).returns(false)
|
||||
RateLimiter.clear_all!
|
||||
|
||||
start = Time.now.tomorrow.beginning_of_day
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user