fix redis leak in rate limiter

change key name so there is no overlap with old non-list key
This commit is contained in:
Sam 2013-05-28 08:58:45 +10:00
parent b73bb49076
commit bf4bdbefe0

View File

@ -11,7 +11,7 @@ class RateLimiter
def initialize(user, key, max, secs) def initialize(user, key, max, secs)
@user = user @user = user
@key = "rate-limit:#{@user.id}:#{key}" @key = "l-rate-limit:#{@user.id}:#{key}"
@max = max @max = max
@secs = secs @secs = secs
end end
@ -31,6 +31,9 @@ class RateLimiter
# simple ring buffer. # simple ring buffer.
$redis.lpush(@key, Time.now.to_i) $redis.lpush(@key, Time.now.to_i)
$redis.ltrim(@key, 0, @max - 1) $redis.ltrim(@key, 0, @max - 1)
# let's ensure we expire this key at some point, otherwise we have leaks
$redis.expire(@key, @secs * 2)
else else
raise LimitExceeded.new(seconds_to_wait) raise LimitExceeded.new(seconds_to_wait)
end end