discourse/lib/middleware/processing_request.rb
Alan Guo Xiang Tan 9c781af9d1
PERF: Reject anonymous requests with 503 upon aggressive queuing (#36519)
This commit adds a default protection to all Discourse app which will
turn away all anonymous requests that has queued for more than the
duration configured
by the `reject_anonymous_min_queue_seconds` global setting, 1 second
by default. A queue time of 1 second is a sign that the app is very
overloaded so we are opting to turn away anonymous requests when that
happens.

While there may be better solutions like introducing an adaptive
concurrency limit somewhere in the stack, we like this solution for its
simplicity. We may consider the more complex solutions in the future but
the time is not now.

Other significant changes in this commit:

1. Moved setting of the `REQUEST_QUEUE_SECONDS` key in `env` to
   `Middleware::ProcessingRequest`.
2. Introduction of the `reject_anonymous_min_queue_seconds` global
   Setting.
2025-12-18 10:23:42 +08:00

34 lines
829 B
Ruby

# frozen_string_literal: true
class Middleware::ProcessingRequest
PROCESSING_REQUEST_THREAD_KEY = "discourse.processing_request"
REQUEST_QUEUE_SECONDS_ENV_KEY = "REQUEST_QUEUE_SECONDS"
def initialize(app)
@app = app
end
def call(env)
Thread.current[PROCESSING_REQUEST_THREAD_KEY] = true
populate_request_queue_seconds!(env)
@app.call(env)
ensure
Thread.current[PROCESSING_REQUEST_THREAD_KEY] = nil
end
private
def populate_request_queue_seconds!(env)
if queue_start = env["HTTP_X_REQUEST_START"]
queue_start =
if queue_start.start_with?("t=")
queue_start.split("t=")[1].to_f
else
queue_start.to_f / 1000.0
end
queue_time = (Time.now.to_f - queue_start)
env[REQUEST_QUEUE_SECONDS_ENV_KEY] = queue_time
end
end
end