2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-13 10:58:27 +08:00
|
|
|
if GlobalSetting.skip_redis?
|
|
|
|
MessageBus.configure(backend: :memory)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-12-09 08:48:41 +08:00
|
|
|
MessageBus.site_id_lookup do |env = nil|
|
|
|
|
if env
|
|
|
|
setup_message_bus_env(env)
|
|
|
|
env["__mb"][:site_id]
|
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.current_db
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_message_bus_env(env)
|
|
|
|
return if env["__mb"]
|
|
|
|
|
2019-08-09 15:47:44 +08:00
|
|
|
::Middleware::RequestTracker.populate_request_queue_seconds!(env)
|
|
|
|
|
|
|
|
if queue_time = env["REQUEST_QUEUE_SECONDS"]
|
|
|
|
if queue_time > (GlobalSetting.reject_message_bus_queue_seconds).to_f
|
|
|
|
raise RateLimiter::LimitExceeded, 30 + (rand * 120).to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-09 08:48:41 +08:00
|
|
|
host = RailsMultisite::ConnectionManagement.host(env)
|
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(host) do
|
2018-07-11 07:29:02 +08:00
|
|
|
extra_headers = {
|
|
|
|
"Access-Control-Allow-Origin" => Discourse.base_url_no_prefix,
|
|
|
|
"Access-Control-Allow-Methods" => "GET, POST",
|
2021-10-15 11:23:53 +08:00
|
|
|
"Access-Control-Allow-Headers" => "X-SILENCE-LOGGER, X-Shared-Session-Key, Dont-Chunk, Discourse-Present",
|
|
|
|
"Access-Control-Max-Age" => "7200",
|
2018-07-11 07:29:02 +08:00
|
|
|
}
|
|
|
|
|
2018-04-20 09:21:51 +08:00
|
|
|
user = nil
|
|
|
|
begin
|
|
|
|
user = CurrentUser.lookup_from_env(env)
|
|
|
|
rescue Discourse::InvalidAccess => e
|
|
|
|
# this is bad we need to remove the cookie
|
|
|
|
if e.opts[:delete_cookie].present?
|
|
|
|
extra_headers['Set-Cookie'] = '_t=del; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'
|
|
|
|
end
|
|
|
|
rescue => e
|
2021-06-01 09:15:26 +08:00
|
|
|
Discourse.warn_exception(e, message: "Unexpected error in Message Bus", env: env)
|
2018-04-20 09:21:51 +08:00
|
|
|
end
|
2021-06-01 09:15:26 +08:00
|
|
|
|
2015-12-09 08:48:41 +08:00
|
|
|
user_id = user && user.id
|
2019-08-13 21:44:22 +08:00
|
|
|
|
|
|
|
raise Discourse::InvalidAccess if !user_id && SiteSetting.login_required
|
|
|
|
|
2015-12-09 08:48:41 +08:00
|
|
|
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
|
|
|
|
|
2018-03-06 13:49:31 +08:00
|
|
|
if env[Auth::DefaultCurrentUserProvider::BAD_TOKEN]
|
|
|
|
extra_headers['Discourse-Logged-Out'] = '1'
|
|
|
|
end
|
|
|
|
|
2015-12-09 08:48:41 +08:00
|
|
|
hash = {
|
2018-03-06 13:49:31 +08:00
|
|
|
extra_headers: extra_headers,
|
2015-12-09 08:48:41 +08:00
|
|
|
user_id: user_id,
|
|
|
|
group_ids: group_ids,
|
|
|
|
is_admin: is_admin,
|
|
|
|
site_id: RailsMultisite::ConnectionManagement.current_db
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2016-08-25 13:39:15 +08:00
|
|
|
MessageBus.on_middleware_error do |env, e|
|
|
|
|
if Discourse::InvalidAccess === e
|
|
|
|
[403, {}, ["Invalid Access"]]
|
|
|
|
elsif RateLimiter::LimitExceeded === e
|
2021-03-24 03:32:36 +08:00
|
|
|
[429, { 'Retry-After' => e.available_in.to_s }, [e.description]]
|
2016-08-25 13:39:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-09 08:48:41 +08:00
|
|
|
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
|
|
|
|
|
2019-05-29 14:34:55 +08:00
|
|
|
if Rails.env == "test"
|
|
|
|
MessageBus.configure(backend: :memory)
|
|
|
|
else
|
|
|
|
MessageBus.redis_config = GlobalSetting.message_bus_redis_config
|
|
|
|
end
|
2022-01-13 05:14:52 +08:00
|
|
|
|
|
|
|
MessageBus.backend_instance.max_backlog_size = GlobalSetting.message_bus_max_backlog_size
|
2022-02-23 00:16:02 +08:00
|
|
|
MessageBus.backend_instance.clear_every = GlobalSetting.message_bus_clear_every
|
2022-03-30 16:32:40 +08:00
|
|
|
MessageBus.long_polling_enabled = GlobalSetting.enable_long_polling.nil? ? true : GlobalSetting.enable_long_polling
|
|
|
|
MessageBus.long_polling_interval = GlobalSetting.long_polling_interval || 25000
|
2021-11-12 01:12:25 +08:00
|
|
|
|
2015-12-09 08:48:41 +08:00
|
|
|
if Rails.env == "test" || $0 =~ /rake$/
|
|
|
|
# disable keepalive in testing
|
|
|
|
MessageBus.keepalive_interval = -1
|
|
|
|
end
|