mirror of
https://github.com/discourse/discourse.git
synced 2025-09-05 08:59:27 +08:00
FIX: load balanced servers do not share monotonic clock
This means then when a service is load balanced and you reach rate limits there was a case where they counting was way off also remove the stub from clock_gettime cause we need to be super careful with it, so we should probably just stub by hand when needed
This commit is contained in:
parent
90a55d6f7c
commit
47c44356f8
2 changed files with 4 additions and 6 deletions
|
@ -7,7 +7,7 @@ class RateLimiter
|
|||
attr_reader :max, :secs, :user, :key
|
||||
|
||||
def self.key_prefix
|
||||
"l-rate-limit2:"
|
||||
"l-rate-limit3:"
|
||||
end
|
||||
|
||||
def self.disable
|
||||
|
@ -76,7 +76,7 @@ class RateLimiter
|
|||
|
||||
def performed!
|
||||
return if rate_unlimited?
|
||||
now = Process.clock_gettime(Process::CLOCK_MONOTONIC).to_i
|
||||
now = Time.now.to_i
|
||||
if eval_lua(PERFORM_LUA, PERFORM_LUA_SHA, [prefixed_key], [now, @secs, @max]) == 0
|
||||
raise RateLimiter::LimitExceeded.new(seconds_to_wait, @type)
|
||||
end
|
||||
|
@ -97,7 +97,7 @@ class RateLimiter
|
|||
return @max if @user && @user.staff?
|
||||
|
||||
arr = redis.lrange(prefixed_key, 0, @max) || []
|
||||
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC).to_i
|
||||
t0 = Time.now.to_i
|
||||
arr.reject! { |a| (t0 - a.to_i) > @secs }
|
||||
@max - arr.size
|
||||
end
|
||||
|
@ -122,7 +122,7 @@ class RateLimiter
|
|||
|
||||
def age_of_oldest
|
||||
# age of oldest event in buffer, in seconds
|
||||
Process.clock_gettime(Process::CLOCK_MONOTONIC).to_i - redis.lrange(prefixed_key, -1, -1).first.to_i
|
||||
Time.now.to_i - redis.lrange(prefixed_key, -1, -1).first.to_i
|
||||
end
|
||||
|
||||
def is_under_limit?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue