"Fossies" - the Fresh Open Source Software Archive

Member "discourse-2.8.3/config/initializers/004-message_bus.rb" (14 Apr 2022, 4058 Bytes) of package /linux/www/discourse-2.8.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "004-message_bus.rb": 2.7.13_vs_2.8.0.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 # frozen_string_literal: true
    2 
    3 if GlobalSetting.skip_redis?
    4   MessageBus.configure(backend: :memory)
    5   return
    6 end
    7 
    8 MessageBus.site_id_lookup do |env = nil|
    9   if env
   10     setup_message_bus_env(env)
   11     env["__mb"][:site_id]
   12   else
   13     RailsMultisite::ConnectionManagement.current_db
   14   end
   15 end
   16 
   17 def setup_message_bus_env(env)
   18   return if env["__mb"]
   19 
   20   ::Middleware::RequestTracker.populate_request_queue_seconds!(env)
   21 
   22   if queue_time = env["REQUEST_QUEUE_SECONDS"]
   23     if queue_time > (GlobalSetting.reject_message_bus_queue_seconds).to_f
   24       raise RateLimiter::LimitExceeded, 30 + (rand * 120).to_i
   25     end
   26   end
   27 
   28   host = RailsMultisite::ConnectionManagement.host(env)
   29   RailsMultisite::ConnectionManagement.with_hostname(host) do
   30     extra_headers = {
   31       "Access-Control-Allow-Origin" => Discourse.base_url_no_prefix,
   32       "Access-Control-Allow-Methods" => "GET, POST",
   33       "Access-Control-Allow-Headers" => "X-SILENCE-LOGGER, X-Shared-Session-Key, Dont-Chunk, Discourse-Present",
   34       "Access-Control-Max-Age" => "7200",
   35     }
   36 
   37     user = nil
   38     begin
   39       user = CurrentUser.lookup_from_env(env)
   40     rescue Discourse::InvalidAccess => e
   41       # this is bad we need to remove the cookie
   42       if e.opts[:delete_cookie].present?
   43         extra_headers['Set-Cookie'] = '_t=del; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'
   44       end
   45     rescue => e
   46       Discourse.warn_exception(e, message: "Unexpected error in Message Bus", env: env)
   47     end
   48 
   49     user_id = user && user.id
   50 
   51     raise Discourse::InvalidAccess if !user_id && SiteSetting.login_required
   52 
   53     is_admin = !!(user && user.admin?)
   54     group_ids = if is_admin
   55       # special rule, admin is allowed access to all groups
   56       Group.pluck(:id)
   57     elsif user
   58       user.groups.pluck('groups.id')
   59     end
   60 
   61     if env[Auth::DefaultCurrentUserProvider::BAD_TOKEN]
   62       extra_headers['Discourse-Logged-Out'] = '1'
   63     end
   64 
   65     hash = {
   66       extra_headers: extra_headers,
   67       user_id: user_id,
   68       group_ids: group_ids,
   69       is_admin: is_admin,
   70       site_id: RailsMultisite::ConnectionManagement.current_db
   71     }
   72     env["__mb"] = hash
   73   end
   74 
   75   nil
   76 end
   77 
   78 MessageBus.extra_response_headers_lookup do |env|
   79   setup_message_bus_env(env)
   80   env["__mb"][:extra_headers]
   81 end
   82 
   83 MessageBus.user_id_lookup do |env|
   84   setup_message_bus_env(env)
   85   env["__mb"][:user_id]
   86 end
   87 
   88 MessageBus.group_ids_lookup do |env|
   89   setup_message_bus_env(env)
   90   env["__mb"][:group_ids]
   91 end
   92 
   93 MessageBus.is_admin_lookup do |env|
   94   setup_message_bus_env(env)
   95   env["__mb"][:is_admin]
   96 end
   97 
   98 MessageBus.on_middleware_error do |env, e|
   99   if Discourse::InvalidAccess === e
  100     [403, {}, ["Invalid Access"]]
  101   elsif RateLimiter::LimitExceeded === e
  102     [429, { 'Retry-After' => e.available_in.to_s }, [e.description]]
  103   end
  104 end
  105 
  106 MessageBus.on_connect do |site_id|
  107   RailsMultisite::ConnectionManagement.establish_connection(db: site_id)
  108 end
  109 
  110 MessageBus.on_disconnect do |site_id|
  111   ActiveRecord::Base.connection_handler.clear_active_connections!
  112 end
  113 
  114 if Rails.env == "test"
  115   MessageBus.configure(backend: :memory)
  116 else
  117   MessageBus.redis_config = GlobalSetting.message_bus_redis_config
  118 end
  119 
  120 MessageBus.backend_instance.max_backlog_size = GlobalSetting.message_bus_max_backlog_size
  121 
  122 if SiteSetting.table_exists? && SiteSetting.where(name: ['enable_long_polling', 'long_polling_interval']).exists?
  123   Discourse.deprecate("enable_long_polling/long_polling_interval have switched from site settings to global settings. Remove the override from the Site Settings UI, and use a config file or environment variables to set the global settings.", drop_from: '2.9.0')
  124 
  125   MessageBus.long_polling_enabled = SiteSetting.enable_long_polling
  126   MessageBus.long_polling_interval = SiteSetting.long_polling_interval
  127 else
  128   MessageBus.long_polling_enabled = GlobalSetting.enable_long_polling.nil? ? true : GlobalSetting.enable_long_polling
  129   MessageBus.long_polling_interval = GlobalSetting.long_polling_interval || 25000
  130 end
  131 
  132 if Rails.env == "test" || $0 =~ /rake$/
  133   # disable keepalive in testing
  134   MessageBus.keepalive_interval = -1
  135 end