From c04bcf86550a1c8f9bea98c25088c22d09ed8e4d Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 7 Dec 2015 14:51:24 +1100 Subject: [PATCH] PERF: move message bus to the front of the middleware stack Organise all initializers so they are properly ordered and use the same naming scheme --- Gemfile.lock | 4 +- ...fills.rb => 000-rails-master-polyfills.rb} | 0 .../{01-redis.rb => 001-redis.rb} | 0 ...edom_patches.rb => 002-freedom_patches.rb} | 0 .../{03-sql_builder.rb => 003-sql_builder.rb} | 0 config/initializers/004-message_bus.rb | 79 +++++++++++++++++++ ...-site_settings.rb => 005-site_settings.rb} | 0 ...login_hint.rb => 006-ensure_login_hint.rb} | 0 ...-mini_profiler.rb => 006-mini_profiler.rb} | 0 .../{08-rack-cors.rb => 008-rack-cors.rb} | 0 .../{09-omniauth.rb => 009-omniauth.rb} | 0 ...iscourse_iife.rb => 010-discourse_iife.rb} | 0 ...k-protection.rb => 011-rack-protection.rb} | 0 config/initializers/04-message_bus.rb | 57 ------------- .../{50-force_https.rb => 050-force_https.rb} | 3 - .../{99-anon-cache.rb => 099-anon-cache.rb} | 0 .../{99-drain_pool.rb => 099-drain_pool.rb} | 0 .../{99-unicorn.rb => 099-unicorn.rb} | 0 config/initializers/{i18n.rb => 100-i18n.rb} | 0 .../{logster.rb => 100-logster.rb} | 0 config/initializers/{oj.rb => 100-oj.rb} | 0 ...nebox_options.rb => 100-onebox_options.rb} | 0 .../{quiet_logger.rb => 100-quiet_logger.rb} | 0 ...rb => 100-rails3_ar_after_commit_tests.rb} | 0 .../{secret_token.rb => 100-secret_token.rb} | 0 ...{session_store.rb => 100-session_store.rb} | 0 .../{sidekiq.rb => 100-sidekiq.rb} | 0 ...ilence_logger.rb => 100-silence_logger.rb} | 0 .../{sprockets.rb => 100-sprockets.rb} | 0 ...parameters.rb => 100-strong_parameters.rb} | 0 ...{verify_config.rb => 100-verify_config.rb} | 0 ...or_restart.rb => 100-watch_for_restart.rb} | 0 ...p_parameters.rb => 100-wrap_parameters.rb} | 0 .../200-message_bus_request_tracker.rb | 13 +++ config/initializers/99-request_tracker.rb | 6 -- config/initializers/inflections.rb | 10 --- config/initializers/mime_types.rb | 5 -- 37 files changed, 94 insertions(+), 83 deletions(-) rename config/initializers/{00-rails-master-polyfills.rb => 000-rails-master-polyfills.rb} (100%) rename config/initializers/{01-redis.rb => 001-redis.rb} (100%) rename config/initializers/{02-freedom_patches.rb => 002-freedom_patches.rb} (100%) rename config/initializers/{03-sql_builder.rb => 003-sql_builder.rb} (100%) create mode 100644 config/initializers/004-message_bus.rb rename config/initializers/{05-site_settings.rb => 005-site_settings.rb} (100%) rename config/initializers/{06-ensure_login_hint.rb => 006-ensure_login_hint.rb} (100%) rename config/initializers/{06-mini_profiler.rb => 006-mini_profiler.rb} (100%) rename config/initializers/{08-rack-cors.rb => 008-rack-cors.rb} (100%) rename config/initializers/{09-omniauth.rb => 009-omniauth.rb} (100%) rename config/initializers/{10-discourse_iife.rb => 010-discourse_iife.rb} (100%) rename config/initializers/{11-rack-protection.rb => 011-rack-protection.rb} (100%) delete mode 100644 config/initializers/04-message_bus.rb rename config/initializers/{50-force_https.rb => 050-force_https.rb} (63%) rename config/initializers/{99-anon-cache.rb => 099-anon-cache.rb} (100%) rename config/initializers/{99-drain_pool.rb => 099-drain_pool.rb} (100%) rename config/initializers/{99-unicorn.rb => 099-unicorn.rb} (100%) rename config/initializers/{i18n.rb => 100-i18n.rb} (100%) rename config/initializers/{logster.rb => 100-logster.rb} (100%) rename config/initializers/{oj.rb => 100-oj.rb} (100%) rename config/initializers/{onebox_options.rb => 100-onebox_options.rb} (100%) rename config/initializers/{quiet_logger.rb => 100-quiet_logger.rb} (100%) rename config/initializers/{rails3_ar_after_commit_tests.rb => 100-rails3_ar_after_commit_tests.rb} (100%) rename config/initializers/{secret_token.rb => 100-secret_token.rb} (100%) rename config/initializers/{session_store.rb => 100-session_store.rb} (100%) rename config/initializers/{sidekiq.rb => 100-sidekiq.rb} (100%) rename config/initializers/{silence_logger.rb => 100-silence_logger.rb} (100%) rename config/initializers/{sprockets.rb => 100-sprockets.rb} (100%) rename config/initializers/{strong_parameters.rb => 100-strong_parameters.rb} (100%) rename config/initializers/{verify_config.rb => 100-verify_config.rb} (100%) rename config/initializers/{watch_for_restart.rb => 100-watch_for_restart.rb} (100%) rename config/initializers/{wrap_parameters.rb => 100-wrap_parameters.rb} (100%) create mode 100644 config/initializers/200-message_bus_request_tracker.rb delete mode 100644 config/initializers/99-request_tracker.rb delete mode 100644 config/initializers/inflections.rb delete mode 100644 config/initializers/mime_types.rb diff --git a/Gemfile.lock b/Gemfile.lock index ebb9d4503..77f5ad819 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -193,7 +193,7 @@ GEM mail (2.6.3) mime-types (>= 1.16, < 3) memory_profiler (0.9.4) - message_bus (1.0.16) + message_bus (1.1.0) rack (>= 1.1.3) redis metaclass (0.0.4) @@ -315,7 +315,7 @@ GEM ffi (>= 1.0.6) msgpack (>= 0.4.3) trollop (>= 1.16.2) - redis (3.2.1) + redis (3.2.2) redis-namespace (1.5.2) redis (~> 3.0, >= 3.0.4) ref (2.0.0) diff --git a/config/initializers/00-rails-master-polyfills.rb b/config/initializers/000-rails-master-polyfills.rb similarity index 100% rename from config/initializers/00-rails-master-polyfills.rb rename to config/initializers/000-rails-master-polyfills.rb diff --git a/config/initializers/01-redis.rb b/config/initializers/001-redis.rb similarity index 100% rename from config/initializers/01-redis.rb rename to config/initializers/001-redis.rb diff --git a/config/initializers/02-freedom_patches.rb b/config/initializers/002-freedom_patches.rb similarity index 100% rename from config/initializers/02-freedom_patches.rb rename to config/initializers/002-freedom_patches.rb diff --git a/config/initializers/03-sql_builder.rb b/config/initializers/003-sql_builder.rb similarity index 100% rename from config/initializers/03-sql_builder.rb rename to config/initializers/003-sql_builder.rb diff --git a/config/initializers/004-message_bus.rb b/config/initializers/004-message_bus.rb new file mode 100644 index 000000000..2faee3473 --- /dev/null +++ b/config/initializers/004-message_bus.rb @@ -0,0 +1,79 @@ +MessageBus.site_id_lookup do + RailsMultisite::ConnectionManagement.current_db +end + +def setup_message_bus_env(env) + return if env["__mb"] + + host = RailsMultisite::ConnectionManagement.host(env) + RailsMultisite::ConnectionManagement.with_hostname(host) do + user = CurrentUser.lookup_from_env(env) + user_id = user && user.id + is_admin = !!(user && user.admin?) + group_ids = if is_admin + # special rule, admin is allowed access to all groups + Group.pluck(:id) + elsif user + user.groups.pluck('groups.id') + end + + hash = { + extra_headers: + { + "Access-Control-Allow-Origin" => Discourse.base_url_no_prefix, + "Access-Control-Allow-Methods" => "GET, POST", + "Access-Control-Allow-Headers" => "X-SILENCE-LOGGER, X-Shared-Session-Key" + }, + user_id: user_id, + group_ids: group_ids, + is_admin: is_admin + + } + env["__mb"] = hash + end + + nil +end + +MessageBus.extra_response_headers_lookup do |env| + setup_message_bus_env(env) + env["__mb"][:extra_headers] +end + +MessageBus.user_id_lookup do |env| + setup_message_bus_env(env) + env["__mb"][:user_id] +end + +MessageBus.group_ids_lookup do |env| + setup_message_bus_env(env) + env["__mb"][:group_ids] +end + +MessageBus.is_admin_lookup do |env| + setup_message_bus_env(env) + env["__mb"][:is_admin] +end + +MessageBus.on_connect do |site_id| + RailsMultisite::ConnectionManagement.establish_connection(db: site_id) +end + +MessageBus.on_disconnect do |site_id| + ActiveRecord::Base.connection_handler.clear_active_connections! +end + +# Point at our redis +MessageBus.redis_config = GlobalSetting.redis_config + +MessageBus.long_polling_enabled = SiteSetting.enable_long_polling +MessageBus.long_polling_interval = SiteSetting.long_polling_interval + + +MessageBus.cache_assets = !Rails.env.development? +MessageBus.enable_diagnostics + +if Rails.env == "test" || $0 =~ /rake$/ + # disable keepalive in testing + MessageBus.keepalive_interval = -1 +end diff --git a/config/initializers/05-site_settings.rb b/config/initializers/005-site_settings.rb similarity index 100% rename from config/initializers/05-site_settings.rb rename to config/initializers/005-site_settings.rb diff --git a/config/initializers/06-ensure_login_hint.rb b/config/initializers/006-ensure_login_hint.rb similarity index 100% rename from config/initializers/06-ensure_login_hint.rb rename to config/initializers/006-ensure_login_hint.rb diff --git a/config/initializers/06-mini_profiler.rb b/config/initializers/006-mini_profiler.rb similarity index 100% rename from config/initializers/06-mini_profiler.rb rename to config/initializers/006-mini_profiler.rb diff --git a/config/initializers/08-rack-cors.rb b/config/initializers/008-rack-cors.rb similarity index 100% rename from config/initializers/08-rack-cors.rb rename to config/initializers/008-rack-cors.rb diff --git a/config/initializers/09-omniauth.rb b/config/initializers/009-omniauth.rb similarity index 100% rename from config/initializers/09-omniauth.rb rename to config/initializers/009-omniauth.rb diff --git a/config/initializers/10-discourse_iife.rb b/config/initializers/010-discourse_iife.rb similarity index 100% rename from config/initializers/10-discourse_iife.rb rename to config/initializers/010-discourse_iife.rb diff --git a/config/initializers/11-rack-protection.rb b/config/initializers/011-rack-protection.rb similarity index 100% rename from config/initializers/11-rack-protection.rb rename to config/initializers/011-rack-protection.rb diff --git a/config/initializers/04-message_bus.rb b/config/initializers/04-message_bus.rb deleted file mode 100644 index ec0a7f974..000000000 --- a/config/initializers/04-message_bus.rb +++ /dev/null @@ -1,57 +0,0 @@ -MessageBus.site_id_lookup do - RailsMultisite::ConnectionManagement.current_db -end - -MessageBus.extra_response_headers_lookup do |env| - { - "Access-Control-Allow-Origin" => Discourse.base_url_no_prefix, - "Access-Control-Allow-Methods" => "GET, POST", - "Access-Control-Allow-Headers" => "X-SILENCE-LOGGER, X-Shared-Session-Key" - } -end - -MessageBus.user_id_lookup do |env| - user = CurrentUser.lookup_from_env(env) - user.id if user -end - -MessageBus.group_ids_lookup do |env| - user = CurrentUser.lookup_from_env(env) - if user && user.admin? - # special rule, admin is allowed access to all groups - Group.pluck(:id) - elsif user - user.groups.pluck('groups.id') - end -end - -MessageBus.on_connect do |site_id| - RailsMultisite::ConnectionManagement.establish_connection(db: site_id) -end - -MessageBus.on_disconnect do |site_id| - ActiveRecord::Base.connection_handler.clear_active_connections! -end - -# Point at our redis -MessageBus.redis_config = GlobalSetting.redis_config - -MessageBus.long_polling_enabled = SiteSetting.enable_long_polling -MessageBus.long_polling_interval = SiteSetting.long_polling_interval - -MessageBus.is_admin_lookup do |env| - user = CurrentUser.lookup_from_env(env) - if user && user.admin - true - else - false - end -end - -MessageBus.cache_assets = !Rails.env.development? -MessageBus.enable_diagnostics - -if Rails.env == "test" || $0 =~ /rake$/ - # disable keepalive in testing - MessageBus.keepalive_interval = -1 -end diff --git a/config/initializers/50-force_https.rb b/config/initializers/050-force_https.rb similarity index 63% rename from config/initializers/50-force_https.rb rename to config/initializers/050-force_https.rb index 126c8b9d4..0e91da8b2 100644 --- a/config/initializers/50-force_https.rb +++ b/config/initializers/050-force_https.rb @@ -12,6 +12,3 @@ class Discourse::ForceHttpsMiddleware end -# this code plays up, skip for now -#Rails.configuration.middleware.insert_before MessageBus::Rack::Middleware, Discourse::ForceHttpsMiddleware - diff --git a/config/initializers/99-anon-cache.rb b/config/initializers/099-anon-cache.rb similarity index 100% rename from config/initializers/99-anon-cache.rb rename to config/initializers/099-anon-cache.rb diff --git a/config/initializers/99-drain_pool.rb b/config/initializers/099-drain_pool.rb similarity index 100% rename from config/initializers/99-drain_pool.rb rename to config/initializers/099-drain_pool.rb diff --git a/config/initializers/99-unicorn.rb b/config/initializers/099-unicorn.rb similarity index 100% rename from config/initializers/99-unicorn.rb rename to config/initializers/099-unicorn.rb diff --git a/config/initializers/i18n.rb b/config/initializers/100-i18n.rb similarity index 100% rename from config/initializers/i18n.rb rename to config/initializers/100-i18n.rb diff --git a/config/initializers/logster.rb b/config/initializers/100-logster.rb similarity index 100% rename from config/initializers/logster.rb rename to config/initializers/100-logster.rb diff --git a/config/initializers/oj.rb b/config/initializers/100-oj.rb similarity index 100% rename from config/initializers/oj.rb rename to config/initializers/100-oj.rb diff --git a/config/initializers/onebox_options.rb b/config/initializers/100-onebox_options.rb similarity index 100% rename from config/initializers/onebox_options.rb rename to config/initializers/100-onebox_options.rb diff --git a/config/initializers/quiet_logger.rb b/config/initializers/100-quiet_logger.rb similarity index 100% rename from config/initializers/quiet_logger.rb rename to config/initializers/100-quiet_logger.rb diff --git a/config/initializers/rails3_ar_after_commit_tests.rb b/config/initializers/100-rails3_ar_after_commit_tests.rb similarity index 100% rename from config/initializers/rails3_ar_after_commit_tests.rb rename to config/initializers/100-rails3_ar_after_commit_tests.rb diff --git a/config/initializers/secret_token.rb b/config/initializers/100-secret_token.rb similarity index 100% rename from config/initializers/secret_token.rb rename to config/initializers/100-secret_token.rb diff --git a/config/initializers/session_store.rb b/config/initializers/100-session_store.rb similarity index 100% rename from config/initializers/session_store.rb rename to config/initializers/100-session_store.rb diff --git a/config/initializers/sidekiq.rb b/config/initializers/100-sidekiq.rb similarity index 100% rename from config/initializers/sidekiq.rb rename to config/initializers/100-sidekiq.rb diff --git a/config/initializers/silence_logger.rb b/config/initializers/100-silence_logger.rb similarity index 100% rename from config/initializers/silence_logger.rb rename to config/initializers/100-silence_logger.rb diff --git a/config/initializers/sprockets.rb b/config/initializers/100-sprockets.rb similarity index 100% rename from config/initializers/sprockets.rb rename to config/initializers/100-sprockets.rb diff --git a/config/initializers/strong_parameters.rb b/config/initializers/100-strong_parameters.rb similarity index 100% rename from config/initializers/strong_parameters.rb rename to config/initializers/100-strong_parameters.rb diff --git a/config/initializers/verify_config.rb b/config/initializers/100-verify_config.rb similarity index 100% rename from config/initializers/verify_config.rb rename to config/initializers/100-verify_config.rb diff --git a/config/initializers/watch_for_restart.rb b/config/initializers/100-watch_for_restart.rb similarity index 100% rename from config/initializers/watch_for_restart.rb rename to config/initializers/100-watch_for_restart.rb diff --git a/config/initializers/wrap_parameters.rb b/config/initializers/100-wrap_parameters.rb similarity index 100% rename from config/initializers/wrap_parameters.rb rename to config/initializers/100-wrap_parameters.rb diff --git a/config/initializers/200-message_bus_request_tracker.rb b/config/initializers/200-message_bus_request_tracker.rb new file mode 100644 index 000000000..203c8d271 --- /dev/null +++ b/config/initializers/200-message_bus_request_tracker.rb @@ -0,0 +1,13 @@ +# we want MesageBus in the absolute front +# this is important cause the vast majority of web requests go to it +# this allows us to avoid full middleware crawls each time +Rails.configuration.middleware.delete MessageBus::Rack::Middleware +Rails.configuration.middleware.unshift MessageBus::Rack::Middleware + +# no reason to track this in development, that is 300+ redis calls saved per +# page view (we serve all assets out of thin in development) +if Rails.env != 'development' || ENV['TRACK_REQUESTS'] + require 'middleware/request_tracker' + Rails.configuration.middleware.unshift Middleware::RequestTracker +end + diff --git a/config/initializers/99-request_tracker.rb b/config/initializers/99-request_tracker.rb deleted file mode 100644 index 5ba1330e1..000000000 --- a/config/initializers/99-request_tracker.rb +++ /dev/null @@ -1,6 +0,0 @@ -# no reason to track this in development, that is 300+ redis calls saved per -# page view (we serve all assets out of thin in development) -if Rails.env != 'development' || ENV['TRACK_REQUESTS'] - require 'middleware/request_tracker' - Rails.configuration.middleware.unshift Middleware::RequestTracker -end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb deleted file mode 100644 index 9e8b0131f..000000000 --- a/config/initializers/inflections.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Add new inflection rules using the following format -# (all these examples are active by default): -# ActiveSupport::Inflector.inflections do |inflect| -# inflect.plural /^(ox)$/i, '\1en' -# inflect.singular /^(ox)en/i, '\1' -# inflect.irregular 'person', 'people' -# inflect.uncountable %w( fish sheep ) -# end diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb deleted file mode 100644 index 72aca7e44..000000000 --- a/config/initializers/mime_types.rb +++ /dev/null @@ -1,5 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Add new mime types for use in respond_to blocks: -# Mime::Type.register "text/richtext", :rtf -# Mime::Type.register_alias "text/html", :iphone