Revert "PERF: move message bus to the front of the middleware stack"

I suspect this commit is preventing Sidekiq from running inprocess.
This commit is contained in:
Robin Ward 2015-12-07 14:57:23 -05:00
parent 4e80323567
commit cd1dd18f01
37 changed files with 83 additions and 94 deletions

View File

@ -193,7 +193,7 @@ GEM
mail (2.6.3)
mime-types (>= 1.16, < 3)
memory_profiler (0.9.4)
message_bus (1.1.0)
message_bus (1.0.16)
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.2)
redis (3.2.1)
redis-namespace (1.5.2)
redis (~> 3.0, >= 3.0.4)
ref (2.0.0)

View File

@ -1,79 +0,0 @@
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

View File

@ -0,0 +1,57 @@
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

View File

@ -1,13 +0,0 @@
# 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

View File

@ -12,3 +12,6 @@ class Discourse::ForceHttpsMiddleware
end
# this code plays up, skip for now
#Rails.configuration.middleware.insert_before MessageBus::Rack::Middleware, Discourse::ForceHttpsMiddleware

View File

@ -0,0 +1,6 @@
# 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

View File

@ -0,0 +1,10 @@
# 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

View File

@ -0,0 +1,5 @@
# 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