2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
require_dependency 'rate_limiter/limit_exceeded'
|
|
|
|
require_dependency 'rate_limiter/on_create_record'
|
|
|
|
|
|
|
|
# A redis backed rate limiter.
|
|
|
|
class RateLimiter
|
|
|
|
|
2015-04-15 18:44:30 -05:00
|
|
|
attr_reader :max, :secs, :user, :key
|
|
|
|
|
2015-02-02 11:44:21 -06:00
|
|
|
def self.key_prefix
|
2017-12-06 18:48:11 -06:00
|
|
|
"l-rate-limit3:"
|
2015-02-02 11:44:21 -06:00
|
|
|
end
|
2015-01-29 10:44:51 -06:00
|
|
|
|
2013-10-08 23:10:37 -05:00
|
|
|
def self.disable
|
|
|
|
@disabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enable
|
|
|
|
@disabled = false
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# We don't observe rate limits in test mode
|
|
|
|
def self.disabled?
|
2017-12-04 04:23:11 -06:00
|
|
|
@disabled
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-12-04 04:23:11 -06:00
|
|
|
# Only used in test, only clears current namespace, does not clear globals
|
2015-01-29 10:44:51 -06:00
|
|
|
def self.clear_all!
|
2015-02-02 11:44:21 -06:00
|
|
|
$redis.delete_prefixed(RateLimiter.key_prefix)
|
2015-01-29 10:44:51 -06:00
|
|
|
end
|
|
|
|
|
2017-12-11 00:21:00 -06:00
|
|
|
def self.clear_all_global!
|
|
|
|
$redis.without_namespace.keys("GLOBAL::#{key_prefix}*").each do |k|
|
|
|
|
$redis.without_namespace.del k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-24 12:52:32 -05:00
|
|
|
def build_key(type)
|
|
|
|
"#{RateLimiter.key_prefix}:#{@user && @user.id}:#{type}"
|
|
|
|
end
|
|
|
|
|
2017-12-04 04:23:11 -06:00
|
|
|
def initialize(user, type, max, secs, global: false)
|
2013-02-05 13:16:51 -06:00
|
|
|
@user = user
|
2015-09-24 12:52:32 -05:00
|
|
|
@type = type
|
|
|
|
@key = build_key(type)
|
2013-02-05 13:16:51 -06:00
|
|
|
@max = max
|
|
|
|
@secs = secs
|
2017-12-11 00:21:00 -06:00
|
|
|
@global = global
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear!
|
2017-12-04 04:23:11 -06:00
|
|
|
redis.del(prefixed_key)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_perform?
|
2013-05-23 19:18:59 -05:00
|
|
|
rate_unlimited? || is_under_limit?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-12-04 04:23:11 -06:00
|
|
|
# reloader friendly
|
|
|
|
unless defined? PERFORM_LUA
|
|
|
|
PERFORM_LUA = <<~LUA
|
|
|
|
local now = tonumber(ARGV[1])
|
|
|
|
local secs = tonumber(ARGV[2])
|
|
|
|
local max = tonumber(ARGV[3])
|
2017-12-04 04:19:28 -06:00
|
|
|
|
2017-12-04 04:23:11 -06:00
|
|
|
local key = KEYS[1]
|
2017-12-04 04:19:28 -06:00
|
|
|
|
2017-12-04 04:23:11 -06:00
|
|
|
|
|
|
|
if ((tonumber(redis.call("LLEN", key)) < max) or
|
|
|
|
(now - tonumber(redis.call("LRANGE", key, -1, -1)[1])) > secs) then
|
|
|
|
redis.call("LPUSH", key, now)
|
|
|
|
redis.call("LTRIM", key, 0, max - 1)
|
|
|
|
redis.call("EXPIRE", key, secs * 2)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
LUA
|
|
|
|
|
|
|
|
PERFORM_LUA_SHA = Digest::SHA1.hexdigest(PERFORM_LUA)
|
|
|
|
end
|
|
|
|
|
2018-04-18 01:58:40 -05:00
|
|
|
def performed!(raise_error: true)
|
2018-04-24 17:44:07 -05:00
|
|
|
return true if rate_unlimited?
|
2017-12-06 18:48:11 -06:00
|
|
|
now = Time.now.to_i
|
2018-02-28 23:20:42 -06:00
|
|
|
|
|
|
|
if ((max || 0) <= 0) ||
|
|
|
|
(eval_lua(PERFORM_LUA, PERFORM_LUA_SHA, [prefixed_key], [now, @secs, @max]) == 0)
|
|
|
|
|
2018-04-18 01:58:40 -05:00
|
|
|
raise RateLimiter::LimitExceeded.new(seconds_to_wait, @type) if raise_error
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
2017-12-04 01:17:18 -06:00
|
|
|
end
|
2017-12-04 04:23:11 -06:00
|
|
|
rescue Redis::CommandError => e
|
|
|
|
if e.message =~ /READONLY/
|
|
|
|
# TODO,switch to in-memory rate limiter
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def rollback!
|
|
|
|
return if RateLimiter.disabled?
|
2017-12-04 04:23:11 -06:00
|
|
|
redis.lpop(prefixed_key)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2016-03-18 10:17:51 -05:00
|
|
|
def remaining
|
|
|
|
return @max if @user && @user.staff?
|
|
|
|
|
2017-12-04 04:23:11 -06:00
|
|
|
arr = redis.lrange(prefixed_key, 0, @max) || []
|
2017-12-06 18:48:11 -06:00
|
|
|
t0 = Time.now.to_i
|
2017-07-27 20:20:09 -05:00
|
|
|
arr.reject! { |a| (t0 - a.to_i) > @secs }
|
2016-03-18 10:17:51 -05:00
|
|
|
@max - arr.size
|
|
|
|
end
|
|
|
|
|
2013-05-23 19:18:59 -05:00
|
|
|
private
|
|
|
|
|
2017-12-04 04:23:11 -06:00
|
|
|
def prefixed_key
|
|
|
|
if @global
|
|
|
|
"GLOBAL::#{key}"
|
|
|
|
else
|
|
|
|
$redis.namespace_key(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis
|
|
|
|
$redis.without_namespace
|
|
|
|
end
|
|
|
|
|
2013-05-25 14:37:28 -05:00
|
|
|
def seconds_to_wait
|
|
|
|
@secs - age_of_oldest
|
|
|
|
end
|
|
|
|
|
|
|
|
def age_of_oldest
|
|
|
|
# age of oldest event in buffer, in seconds
|
2017-12-06 18:48:11 -06:00
|
|
|
Time.now.to_i - redis.lrange(prefixed_key, -1, -1).first.to_i
|
2013-05-25 14:37:28 -05:00
|
|
|
end
|
|
|
|
|
2013-05-23 19:18:59 -05:00
|
|
|
def is_under_limit?
|
2015-01-29 10:44:51 -06:00
|
|
|
# number of events in buffer less than max allowed? OR
|
2017-12-04 04:23:11 -06:00
|
|
|
(redis.llen(prefixed_key) < @max) ||
|
2015-01-29 10:44:51 -06:00
|
|
|
# age bigger than silding window size?
|
|
|
|
(age_of_oldest > @secs)
|
2013-05-23 19:18:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rate_unlimited?
|
2014-08-17 19:55:30 -05:00
|
|
|
!!(RateLimiter.disabled? || (@user && @user.staff?))
|
2013-05-23 19:18:59 -05:00
|
|
|
end
|
2017-12-04 04:23:11 -06:00
|
|
|
|
|
|
|
def eval_lua(lua, sha, keys, args)
|
|
|
|
redis.evalsha(sha, keys, args)
|
|
|
|
rescue Redis::CommandError => e
|
|
|
|
if e.to_s =~ /^NOSCRIPT/
|
|
|
|
redis.eval(lua, keys, args)
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|