mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
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
This commit is contained in:
parent
1ef9233aec
commit
c04bcf8655
37 changed files with 94 additions and 83 deletions
|
@ -193,7 +193,7 @@ GEM
|
||||||
mail (2.6.3)
|
mail (2.6.3)
|
||||||
mime-types (>= 1.16, < 3)
|
mime-types (>= 1.16, < 3)
|
||||||
memory_profiler (0.9.4)
|
memory_profiler (0.9.4)
|
||||||
message_bus (1.0.16)
|
message_bus (1.1.0)
|
||||||
rack (>= 1.1.3)
|
rack (>= 1.1.3)
|
||||||
redis
|
redis
|
||||||
metaclass (0.0.4)
|
metaclass (0.0.4)
|
||||||
|
@ -315,7 +315,7 @@ GEM
|
||||||
ffi (>= 1.0.6)
|
ffi (>= 1.0.6)
|
||||||
msgpack (>= 0.4.3)
|
msgpack (>= 0.4.3)
|
||||||
trollop (>= 1.16.2)
|
trollop (>= 1.16.2)
|
||||||
redis (3.2.1)
|
redis (3.2.2)
|
||||||
redis-namespace (1.5.2)
|
redis-namespace (1.5.2)
|
||||||
redis (~> 3.0, >= 3.0.4)
|
redis (~> 3.0, >= 3.0.4)
|
||||||
ref (2.0.0)
|
ref (2.0.0)
|
||||||
|
|
79
config/initializers/004-message_bus.rb
Normal file
79
config/initializers/004-message_bus.rb
Normal file
|
@ -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
|
|
@ -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
|
|
|
@ -12,6 +12,3 @@ class Discourse::ForceHttpsMiddleware
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# this code plays up, skip for now
|
|
||||||
#Rails.configuration.middleware.insert_before MessageBus::Rack::Middleware, Discourse::ForceHttpsMiddleware
|
|
||||||
|
|
13
config/initializers/200-message_bus_request_tracker.rb
Normal file
13
config/initializers/200-message_bus_request_tracker.rb
Normal file
|
@ -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
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -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
|
|
Loading…
Reference in a new issue