2018-04-18 14:58:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
require "mobile_detection"
|
|
|
|
require "crawler_detection"
|
|
|
|
require "guardian"
|
|
|
|
require "http_language_parser"
|
2014-01-09 11:08:42 +08:00
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
module Middleware
|
|
|
|
class AnonymousCache
|
|
|
|
|
2019-12-06 03:57:18 +08:00
|
|
|
def self.cache_key_segments
|
|
|
|
@@cache_key_segments ||= {
|
|
|
|
m: 'key_is_mobile?',
|
|
|
|
c: 'key_is_crawler?',
|
2022-04-14 21:25:52 +08:00
|
|
|
o: 'key_is_old_browser?',
|
|
|
|
d: 'key_is_modern_mobile_device?',
|
2019-12-06 03:57:18 +08:00
|
|
|
b: 'key_has_brotli?',
|
|
|
|
t: 'key_cache_theme_ids',
|
2020-07-23 01:00:07 +08:00
|
|
|
ca: 'key_compress_anon',
|
|
|
|
l: 'key_locale'
|
2019-12-06 03:57:18 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Compile a string builder method that will be called to create
|
|
|
|
# an anonymous cache key
|
|
|
|
def self.compile_key_builder
|
|
|
|
method = +"def self.__compiled_key_builder(h)\n \""
|
|
|
|
cache_key_segments.each do |k, v|
|
|
|
|
raise "Invalid key name" unless k =~ /^[a-z]+$/
|
|
|
|
raise "Invalid method name" unless v =~ /^key_[a-z_\?]+$/
|
|
|
|
method << "|#{k}=#\{h.#{v}}"
|
|
|
|
end
|
|
|
|
method << "\"\nend"
|
2021-10-27 16:39:28 +08:00
|
|
|
eval(method) # rubocop:disable Security/Eval
|
2019-12-06 03:57:18 +08:00
|
|
|
@@compiled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build_cache_key(helper)
|
|
|
|
compile_key_builder unless defined?(@@compiled)
|
|
|
|
__compiled_key_builder(helper)
|
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def self.anon_cache(env, duration)
|
|
|
|
env["ANON_CACHE_DURATION"] = duration
|
|
|
|
end
|
|
|
|
|
2019-12-06 03:57:18 +08:00
|
|
|
# This gives us an API to insert anonymous cache segments
|
2013-10-16 13:39:18 +08:00
|
|
|
class Helper
|
2019-08-31 02:45:18 +08:00
|
|
|
RACK_SESSION = "rack.session"
|
|
|
|
USER_AGENT = "HTTP_USER_AGENT"
|
|
|
|
ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING"
|
|
|
|
DISCOURSE_RENDER = "HTTP_DISCOURSE_RENDER"
|
2014-04-29 08:48:09 +08:00
|
|
|
|
2022-02-16 00:06:12 +08:00
|
|
|
REDIS_STORE_SCRIPT = DiscourseRedis::EvalHelper.new <<~LUA
|
|
|
|
local current = redis.call("incr", KEYS[1])
|
|
|
|
redis.call("expire",KEYS[1],ARGV[1])
|
|
|
|
return current
|
|
|
|
LUA
|
|
|
|
|
FEATURE: Apply rate limits per user instead of IP for trusted users (#14706)
Currently, Discourse rate limits all incoming requests by the IP address they
originate from regardless of the user making the request. This can be
frustrating if there are multiple users using Discourse simultaneously while
sharing the same IP address (e.g. employees in an office).
This commit implements a new feature to make Discourse apply rate limits by
user id rather than IP address for users at or higher than the configured trust
level (1 is the default).
For example, let's say a Discourse instance is configured to allow 200 requests
per minute per IP address, and we have 10 users at trust level 4 using
Discourse simultaneously from the same IP address. Before this feature, the 10
users could only make a total of 200 requests per minute before they got rate
limited. But with the new feature, each user is allowed to make 200 requests
per minute because the rate limits are applied on user id rather than the IP
address.
The minimum trust level for applying user-id-based rate limits can be
configured by the `skip_per_ip_rate_limit_trust_level` global setting. The
default is 1, but it can be changed by either adding the
`DISCOURSE_SKIP_PER_IP_RATE_LIMIT_TRUST_LEVEL` environment variable with the
desired value to your `app.yml`, or changing the setting's value in the
`discourse.conf` file.
Requests made with API keys are still rate limited by IP address and the
relevant global settings that control API keys rate limits.
Before this commit, Discourse's auth cookie (`_t`) was simply a 32 characters
string that Discourse used to lookup the current user from the database and the
cookie contained no additional information about the user. However, we had to
change the cookie content in this commit so we could identify the user from the
cookie without making a database query before the rate limits logic and avoid
introducing a bottleneck on busy sites.
Besides the 32 characters auth token, the cookie now includes the user id,
trust level and the cookie's generation date, and we encrypt/sign the cookie to
prevent tampering.
Internal ticket number: t54739.
2021-11-18 04:27:30 +08:00
|
|
|
def initialize(env, request = nil)
|
2013-10-16 13:39:18 +08:00
|
|
|
@env = env
|
FEATURE: Apply rate limits per user instead of IP for trusted users (#14706)
Currently, Discourse rate limits all incoming requests by the IP address they
originate from regardless of the user making the request. This can be
frustrating if there are multiple users using Discourse simultaneously while
sharing the same IP address (e.g. employees in an office).
This commit implements a new feature to make Discourse apply rate limits by
user id rather than IP address for users at or higher than the configured trust
level (1 is the default).
For example, let's say a Discourse instance is configured to allow 200 requests
per minute per IP address, and we have 10 users at trust level 4 using
Discourse simultaneously from the same IP address. Before this feature, the 10
users could only make a total of 200 requests per minute before they got rate
limited. But with the new feature, each user is allowed to make 200 requests
per minute because the rate limits are applied on user id rather than the IP
address.
The minimum trust level for applying user-id-based rate limits can be
configured by the `skip_per_ip_rate_limit_trust_level` global setting. The
default is 1, but it can be changed by either adding the
`DISCOURSE_SKIP_PER_IP_RATE_LIMIT_TRUST_LEVEL` environment variable with the
desired value to your `app.yml`, or changing the setting's value in the
`discourse.conf` file.
Requests made with API keys are still rate limited by IP address and the
relevant global settings that control API keys rate limits.
Before this commit, Discourse's auth cookie (`_t`) was simply a 32 characters
string that Discourse used to lookup the current user from the database and the
cookie contained no additional information about the user. However, we had to
change the cookie content in this commit so we could identify the user from the
cookie without making a database query before the rate limits logic and avoid
introducing a bottleneck on busy sites.
Besides the 32 characters auth token, the cookie now includes the user id,
trust level and the cookie's generation date, and we encrypt/sign the cookie to
prevent tampering.
Internal ticket number: t54739.
2021-11-18 04:27:30 +08:00
|
|
|
@request = request || Rack::Request.new(@env)
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
2018-07-04 09:14:43 +08:00
|
|
|
def blocked_crawler?
|
|
|
|
@request.get? &&
|
|
|
|
!@request.xhr? &&
|
|
|
|
!@request.path.ends_with?('robots.txt') &&
|
2018-07-18 10:33:06 +08:00
|
|
|
!@request.path.ends_with?('srv/status') &&
|
2018-09-15 03:34:21 +08:00
|
|
|
@request[Auth::DefaultCurrentUserProvider::API_KEY].nil? &&
|
|
|
|
@env[Auth::DefaultCurrentUserProvider::USER_API_KEY].nil? &&
|
2019-08-31 02:45:18 +08:00
|
|
|
CrawlerDetection.is_blocked_crawler?(@env[USER_AGENT])
|
2018-07-04 09:14:43 +08:00
|
|
|
end
|
|
|
|
|
2014-01-09 12:11:04 +08:00
|
|
|
def is_mobile=(val)
|
|
|
|
@is_mobile = val ? :true : :false
|
|
|
|
end
|
|
|
|
|
2019-12-06 04:07:22 +08:00
|
|
|
def is_mobile?
|
2014-01-09 11:08:42 +08:00
|
|
|
@is_mobile ||=
|
|
|
|
begin
|
2014-04-29 08:48:09 +08:00
|
|
|
session = @env[RACK_SESSION]
|
2019-08-31 02:45:18 +08:00
|
|
|
# don't initialize params until later
|
|
|
|
# otherwise you get a broken params on the request
|
2014-01-09 13:49:12 +08:00
|
|
|
params = {}
|
2014-01-09 11:08:42 +08:00
|
|
|
|
2019-08-31 02:45:18 +08:00
|
|
|
MobileDetection.resolve_mobile_view!(@env[USER_AGENT], params, session) ? :true : :false
|
2014-04-29 08:48:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@is_mobile == :true
|
|
|
|
end
|
2019-12-06 04:07:22 +08:00
|
|
|
alias_method :key_is_mobile?, :is_mobile?
|
2014-04-29 08:48:09 +08:00
|
|
|
|
2019-12-06 03:57:18 +08:00
|
|
|
def key_has_brotli?
|
2016-12-05 10:57:09 +08:00
|
|
|
@has_brotli ||=
|
|
|
|
begin
|
|
|
|
@env[ACCEPT_ENCODING].to_s =~ /br/ ? :true : :false
|
|
|
|
end
|
|
|
|
@has_brotli == :true
|
|
|
|
end
|
|
|
|
|
2020-07-23 01:00:07 +08:00
|
|
|
def key_locale
|
|
|
|
if SiteSetting.set_locale_from_accept_language_header
|
|
|
|
HttpLanguageParser.parse(@env["HTTP_ACCEPT_LANGUAGE"])
|
|
|
|
else
|
|
|
|
"" # No need to key, it is the same for all anon users
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-06 04:07:22 +08:00
|
|
|
def is_crawler?
|
2014-04-29 08:48:09 +08:00
|
|
|
@is_crawler ||=
|
|
|
|
begin
|
|
|
|
user_agent = @env[USER_AGENT]
|
2019-08-31 02:45:18 +08:00
|
|
|
|
|
|
|
if @env[DISCOURSE_RENDER] == "crawler" || CrawlerDetection.crawler?(user_agent, @env["HTTP_VIA"])
|
2019-05-08 22:38:55 +08:00
|
|
|
:true
|
|
|
|
else
|
2019-11-04 22:16:50 +08:00
|
|
|
user_agent.downcase.include?("discourse") && !user_agent.downcase.include?("mobile") ? :true : :false
|
2019-05-08 22:38:55 +08:00
|
|
|
end
|
2014-04-29 08:48:09 +08:00
|
|
|
end
|
|
|
|
@is_crawler == :true
|
2014-01-09 11:08:42 +08:00
|
|
|
end
|
2019-12-06 04:07:22 +08:00
|
|
|
alias_method :key_is_crawler?, :is_crawler?
|
2014-01-09 11:08:42 +08:00
|
|
|
|
2022-04-14 21:25:52 +08:00
|
|
|
def key_is_modern_mobile_device?
|
|
|
|
MobileDetection.modern_mobile_device?(@env[USER_AGENT]) if @env[USER_AGENT]
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_is_old_browser?
|
|
|
|
CrawlerDetection.show_browser_update?(@env[USER_AGENT]) if @env[USER_AGENT]
|
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def cache_key
|
2019-12-06 03:57:18 +08:00
|
|
|
return @cache_key if defined?(@cache_key)
|
|
|
|
|
2020-03-20 03:54:42 +08:00
|
|
|
@cache_key = +"ANON_CACHE_#{@env["HTTP_ACCEPT"]}_#{@env[Rack::RACK_URL_SCHEME]}_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}"
|
2019-12-06 03:57:18 +08:00
|
|
|
@cache_key << AnonymousCache.build_cache_key(self)
|
|
|
|
@cache_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_cache_theme_ids
|
|
|
|
theme_ids.join(',')
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_compress_anon
|
|
|
|
GlobalSetting.compress_anon_cache
|
2017-06-15 21:36:27 +08:00
|
|
|
end
|
|
|
|
|
2018-08-08 12:46:34 +08:00
|
|
|
def theme_ids
|
2018-07-12 12:18:21 +08:00
|
|
|
ids, _ = @request.cookies['theme_ids']&.split('|')
|
2021-06-15 14:57:17 +08:00
|
|
|
id = ids&.split(",")&.map(&:to_i)&.first
|
|
|
|
if id && Guardian.new.allow_themes?([id])
|
|
|
|
Theme.transform_ids(id)
|
2017-06-15 21:36:27 +08:00
|
|
|
else
|
2018-08-08 12:46:34 +08:00
|
|
|
[]
|
2017-06-15 21:36:27 +08:00
|
|
|
end
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
2019-09-04 15:18:32 +08:00
|
|
|
def cache_key_count
|
|
|
|
@cache_key_count ||= "#{cache_key}_count"
|
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def cache_key_body
|
|
|
|
@cache_key_body ||= "#{cache_key}_body"
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key_other
|
|
|
|
@cache_key_other || "#{cache_key}_other"
|
|
|
|
end
|
|
|
|
|
|
|
|
def get?
|
|
|
|
@env["REQUEST_METHOD"] == "GET"
|
|
|
|
end
|
|
|
|
|
2015-02-04 13:14:56 +08:00
|
|
|
def has_auth_cookie?
|
|
|
|
CurrentUser.has_auth_cookie?(@env)
|
|
|
|
end
|
|
|
|
|
2015-10-29 05:16:56 +08:00
|
|
|
def no_cache_bypass
|
|
|
|
request = Rack::Request.new(@env)
|
2018-04-18 14:58:40 +08:00
|
|
|
request.cookies['_bypass_cache'].nil? &&
|
2020-12-16 13:47:46 +08:00
|
|
|
(request.path != '/srv/status') &&
|
2018-04-18 14:58:40 +08:00
|
|
|
request[Auth::DefaultCurrentUserProvider::API_KEY].nil? &&
|
|
|
|
@env[Auth::DefaultCurrentUserProvider::USER_API_KEY].nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def force_anonymous!
|
|
|
|
@env[Auth::DefaultCurrentUserProvider::USER_API_KEY] = nil
|
|
|
|
@env['HTTP_COOKIE'] = nil
|
2021-10-07 19:31:42 +08:00
|
|
|
@env['HTTP_DISCOURSE_LOGGED_IN'] = nil
|
2018-04-18 14:58:40 +08:00
|
|
|
@env['rack.request.cookie.hash'] = {}
|
|
|
|
@env['rack.request.cookie.string'] = ''
|
|
|
|
@env['_bypass_cache'] = nil
|
|
|
|
request = Rack::Request.new(@env)
|
|
|
|
request.delete_param('api_username')
|
|
|
|
request.delete_param('api_key')
|
|
|
|
end
|
|
|
|
|
2018-04-23 09:54:58 +08:00
|
|
|
def logged_in_anon_limiter
|
|
|
|
@logged_in_anon_limiter ||= RateLimiter.new(
|
2018-04-18 14:58:40 +08:00
|
|
|
nil,
|
2019-08-31 02:45:18 +08:00
|
|
|
"logged_in_anon_cache_#{@env["HTTP_HOST"]}/#{@env["REQUEST_URI"]}",
|
2018-04-18 14:58:40 +08:00
|
|
|
GlobalSetting.force_anonymous_min_per_10_seconds,
|
|
|
|
10
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-04-23 09:54:58 +08:00
|
|
|
def check_logged_in_rate_limit!
|
|
|
|
!logged_in_anon_limiter.performed!(raise_error: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
MIN_TIME_TO_CHECK = 0.05
|
2019-09-03 08:51:49 +08:00
|
|
|
ADP = "action_dispatch.request.parameters"
|
2018-04-23 09:54:58 +08:00
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
def should_force_anonymous?
|
2018-04-23 09:54:58 +08:00
|
|
|
if (queue_time = @env['REQUEST_QUEUE_SECONDS']) && get?
|
|
|
|
if queue_time > GlobalSetting.force_anonymous_min_queue_seconds
|
2018-04-18 14:58:40 +08:00
|
|
|
return check_logged_in_rate_limit!
|
2018-04-23 09:54:58 +08:00
|
|
|
elsif queue_time >= MIN_TIME_TO_CHECK
|
|
|
|
if !logged_in_anon_limiter.can_perform?
|
|
|
|
return check_logged_in_rate_limit!
|
|
|
|
end
|
2018-04-18 14:58:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2015-10-29 05:16:56 +08:00
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def cacheable?
|
2015-10-29 05:16:56 +08:00
|
|
|
!!(!has_auth_cookie? && get? && no_cache_bypass)
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
2019-09-04 15:18:32 +08:00
|
|
|
def compress(val)
|
|
|
|
if val && GlobalSetting.compress_anon_cache
|
|
|
|
require "lz4-ruby" if !defined?(LZ4)
|
|
|
|
LZ4::compress(val)
|
|
|
|
else
|
|
|
|
val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def decompress(val)
|
|
|
|
if val && GlobalSetting.compress_anon_cache
|
|
|
|
require "lz4-ruby" if !defined?(LZ4)
|
|
|
|
LZ4::uncompress(val)
|
|
|
|
else
|
|
|
|
val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-03 08:51:49 +08:00
|
|
|
def cached(env = {})
|
2019-12-03 17:05:53 +08:00
|
|
|
if body = decompress(Discourse.redis.get(cache_key_body))
|
|
|
|
if other = Discourse.redis.get(cache_key_other)
|
2013-10-16 13:39:18 +08:00
|
|
|
other = JSON.parse(other)
|
2019-09-03 08:51:49 +08:00
|
|
|
if req_params = other[1].delete(ADP)
|
|
|
|
env[ADP] = req_params
|
|
|
|
end
|
2013-10-16 13:39:18 +08:00
|
|
|
[other[0], other[1], [body]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_duration
|
|
|
|
@env["ANON_CACHE_DURATION"]
|
|
|
|
end
|
|
|
|
|
|
|
|
# NOTE in an ideal world cache still serves out cached content except for one magic worker
|
|
|
|
# that fills it up, this avoids a herd killing you, we can probably do this using a job or redis tricks
|
|
|
|
# but coordinating this is tricky
|
2019-09-03 08:51:49 +08:00
|
|
|
def cache(result, env = {})
|
2019-09-04 15:18:32 +08:00
|
|
|
return result if GlobalSetting.anon_cache_store_threshold == 0
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
status, headers, response = result
|
|
|
|
|
|
|
|
if status == 200 && cache_duration
|
2019-09-04 15:18:32 +08:00
|
|
|
|
|
|
|
if GlobalSetting.anon_cache_store_threshold > 1
|
2022-02-16 00:06:12 +08:00
|
|
|
count = REDIS_STORE_SCRIPT.eval(Discourse.redis, [cache_key_count], [cache_duration])
|
2019-09-04 15:18:32 +08:00
|
|
|
|
|
|
|
# technically lua will cast for us, but might as well be
|
|
|
|
# prudent here, hence the to_i
|
|
|
|
if count.to_i < GlobalSetting.anon_cache_store_threshold
|
|
|
|
headers["X-Discourse-Cached"] = "skip"
|
|
|
|
return [status, headers, response]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-15 05:54:55 +08:00
|
|
|
headers_stripped = headers.dup.delete_if { |k, _| ["Set-Cookie", "X-MiniProfiler-Ids"].include? k }
|
2015-06-16 08:30:06 +08:00
|
|
|
headers_stripped["X-Discourse-Cached"] = "true"
|
2013-10-16 13:39:18 +08:00
|
|
|
parts = []
|
|
|
|
response.each do |part|
|
|
|
|
parts << part
|
|
|
|
end
|
|
|
|
|
2019-09-03 08:51:49 +08:00
|
|
|
if req_params = env[ADP]
|
|
|
|
headers_stripped[ADP] = {
|
|
|
|
"action" => req_params["action"],
|
|
|
|
"controller" => req_params["controller"]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.setex(cache_key_body, cache_duration, compress(parts.join))
|
|
|
|
Discourse.redis.setex(cache_key_other, cache_duration, [status, headers_stripped].to_json)
|
2019-09-02 16:45:35 +08:00
|
|
|
|
|
|
|
headers["X-Discourse-Cached"] = "store"
|
2013-10-16 13:39:18 +08:00
|
|
|
else
|
|
|
|
parts = response
|
|
|
|
end
|
|
|
|
|
|
|
|
[status, headers, parts]
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.del(cache_key_body)
|
|
|
|
Discourse.redis.del(cache_key_other)
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(app, settings = {})
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
2020-08-03 17:02:50 +08:00
|
|
|
PAYLOAD_INVALID_REQUEST_METHODS = ["GET", "HEAD"]
|
2020-08-03 14:11:17 +08:00
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def call(env)
|
2020-08-03 14:11:17 +08:00
|
|
|
if PAYLOAD_INVALID_REQUEST_METHODS.include?(env[Rack::REQUEST_METHOD]) &&
|
|
|
|
env[Rack::RACK_INPUT].size > 0
|
|
|
|
|
2021-11-13 02:52:25 +08:00
|
|
|
return [413, { "Cache-Control" => "private, max-age=0, must-revalidate" }, []]
|
2020-08-03 14:11:17 +08:00
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
helper = Helper.new(env)
|
2018-04-18 14:58:40 +08:00
|
|
|
force_anon = false
|
|
|
|
|
2018-07-04 09:14:43 +08:00
|
|
|
if helper.blocked_crawler?
|
|
|
|
env["discourse.request_tracker.skip"] = true
|
2018-09-15 03:39:24 +08:00
|
|
|
return [403, {}, ["Crawler is not allowed!"]]
|
2018-07-04 09:14:43 +08:00
|
|
|
end
|
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
if helper.should_force_anonymous?
|
|
|
|
force_anon = env["DISCOURSE_FORCE_ANON"] = true
|
|
|
|
helper.force_anonymous!
|
|
|
|
end
|
2013-10-16 13:39:18 +08:00
|
|
|
|
2020-10-13 13:56:03 +08:00
|
|
|
if (env["HTTP_DISCOURSE_BACKGROUND"] == "true") && (queue_time = env["REQUEST_QUEUE_SECONDS"])
|
2020-10-13 15:08:38 +08:00
|
|
|
max_time = GlobalSetting.background_requests_max_queue_length.to_f
|
|
|
|
if max_time > 0 && queue_time.to_f > max_time
|
2020-10-13 13:56:03 +08:00
|
|
|
return [
|
|
|
|
429,
|
|
|
|
{
|
|
|
|
"content-type" => "application/json; charset=utf-8"
|
|
|
|
},
|
|
|
|
[{
|
|
|
|
errors: I18n.t("rate_limiter.slow_down"),
|
|
|
|
extras: {
|
|
|
|
wait_seconds: 5 + (5 * rand).round(2)
|
|
|
|
}
|
|
|
|
}.to_json]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
result =
|
|
|
|
if helper.cacheable?
|
2019-09-03 08:51:49 +08:00
|
|
|
helper.cached(env) || helper.cache(@app.call(env), env)
|
2018-04-18 14:58:40 +08:00
|
|
|
else
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
if force_anon
|
2018-04-24 11:24:26 +08:00
|
|
|
result[1]["Set-Cookie"] = "dosp=1; Path=/"
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
result
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|