REFACTOR: Move the multisite middleware to the front

Both request tracking and message bus rely on multisite before the
middleware has run which is not ideal.

Follow-up-to: ca1208a636
This commit is contained in:
Daniel Waterworth 2020-03-30 12:01:06 +01:00
parent 724d2e99de
commit bca126f3f5
3 changed files with 21 additions and 24 deletions

View File

@ -1,9 +1,11 @@
# frozen_string_literal: true
# we want MesageBus in the absolute front
# we want MesageBus to be close to the front
# this is important cause the vast majority of web requests go to it
# this allows us to avoid full middleware crawls each time
# Pending https://github.com/rails/rails/pull/27936
#
# We aren't manipulating the middleware stack directly because of
# https://github.com/rails/rails/pull/27936
session_operations = Rails::Configuration::MiddlewareStackProxy.new([
[:delete, MessageBus::Rack::Middleware],
[:unshift, MessageBus::Rack::Middleware],
@ -21,3 +23,14 @@ if Rails.env != 'development' || ENV['TRACK_REQUESTS']
MethodProfiler.ensure_discourse_instrumentation!
end
end
if Rails.configuration.multisite
# Multisite needs to be first, because the request tracker and message bus
# rely on it
session_operations = Rails::Configuration::MiddlewareStackProxy.new([
[:delete, RailsMultisite::Middleware],
[:unshift, [RailsMultisite::Middleware, RailsMultisite::DiscoursePatches.config]],
])
Rails.configuration.middleware = Rails.configuration.middleware + session_operations
end

View File

@ -26,11 +26,3 @@ module RailsMultisite
end
end
end
if Rails.configuration.multisite
Rails.configuration.middleware.swap(
RailsMultisite::Middleware,
RailsMultisite::Middleware,
RailsMultisite::DiscoursePatches.config
)
end

View File

@ -51,14 +51,6 @@ class Middleware::RequestTracker
@app = app
end
def self.log_request_on_site(data, host)
RailsMultisite::ConnectionManagement.with_hostname(host) do
unless Discourse.pg_readonly_mode?
log_request(data)
end
end
end
def self.log_request(data)
status = data[:status]
track_view = data[:track_view]
@ -135,7 +127,6 @@ class Middleware::RequestTracker
# we got to skip this on error ... its just logging
data = self.class.get_data(env, result, info) rescue nil
host = RailsMultisite::ConnectionManagement.host(env)
if data
if result && (headers = result[1])
@ -146,7 +137,7 @@ class Middleware::RequestTracker
@@detailed_request_loggers.each { |logger| logger.call(env, data) }
end
log_later(data, host)
log_later(data)
end
end
@ -296,10 +287,11 @@ class Middleware::RequestTracker
end
end
def log_later(data, host)
Scheduler::Defer.later("Track view", _db = nil) do
self.class.log_request_on_site(data, host)
def log_later(data)
Scheduler::Defer.later("Track view") do
unless Discourse.pg_readonly_mode?
self.class.log_request(data)
end
end
end
end