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:
Sam
2018-04-18 16:58:40 +10:00
parent 7bf9650e96
commit 59cd7894d9
6 changed files with 125 additions and 10 deletions

View File

@ -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)