mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FEATURE: if site is under extreme load show anon view
If a particular path is being hit extremely hard by logged on users, revert to anonymous cached view. This will only come into effect if 3 requests queue for longer than 2 seconds on a *single* path. This can happen if a URL is shared with the entire forum base and everyone is logged on
This commit is contained in:
@ -45,6 +45,55 @@ describe Middleware::AnonymousCache::Helper do
|
||||
end
|
||||
end
|
||||
|
||||
context 'force_anonymous!' do
|
||||
before do
|
||||
RateLimiter.enable
|
||||
end
|
||||
|
||||
after do
|
||||
RateLimiter.disable
|
||||
end
|
||||
|
||||
it 'will revert to anonymous once we reach the limit' do
|
||||
|
||||
RateLimiter.clear_all!
|
||||
|
||||
is_anon = false
|
||||
|
||||
app = Middleware::AnonymousCache.new(
|
||||
lambda do |env|
|
||||
is_anon = env["HTTP_COOKIE"].nil?
|
||||
[200, {}, ["ok"]]
|
||||
end
|
||||
)
|
||||
|
||||
global_setting :force_anonymous_min_per_10_seconds, 2
|
||||
global_setting :force_anonymous_min_queue_seconds, 1
|
||||
|
||||
env = {
|
||||
"HTTP_COOKIE" => "_t=#{SecureRandom.hex}",
|
||||
"HOST" => "site.com",
|
||||
"REQUEST_METHOD" => "GET",
|
||||
"REQUEST_URI" => "/somewhere/rainbow",
|
||||
"REQUEST_QUEUE_SECONDS" => 2.1,
|
||||
"rack.input" => StringIO.new
|
||||
}
|
||||
|
||||
app.call(env)
|
||||
expect(is_anon).to eq(false)
|
||||
|
||||
app.call(env)
|
||||
expect(is_anon).to eq(false)
|
||||
|
||||
app.call(env)
|
||||
expect(is_anon).to eq(true)
|
||||
|
||||
_status, headers, _body = app.call(env)
|
||||
expect(is_anon).to eq(true)
|
||||
expect(headers['Set-Cookie']).to eq('dosp=1')
|
||||
end
|
||||
end
|
||||
|
||||
context "cached" do
|
||||
let!(:helper) do
|
||||
new_helper("ANON_CACHE_DURATION" => 10)
|
||||
|
Reference in New Issue
Block a user