mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 16:42:46 +08:00
fix redis leak in rate limiter
change key name so there is no overlap with old non-list key
This commit is contained in:
parent
b73bb49076
commit
bf4bdbefe0
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user