Commit e84915d9 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu Committed by Dylan Griffith

Allow ActionCable in-app mode

Adds an option in gitlab.yml to run ActionCable in in-app mode

Connection pool settings are also adjusted to account for the extra
threads
parent 4c1dff36
......@@ -17,10 +17,6 @@ module Gitlab
class Application < Rails::Application
require_dependency Rails.root.join('lib/gitlab')
require_dependency Rails.root.join('lib/gitlab/utils')
require_dependency Rails.root.join('lib/gitlab/redis/wrapper')
require_dependency Rails.root.join('lib/gitlab/redis/cache')
require_dependency Rails.root.join('lib/gitlab/redis/queues')
require_dependency Rails.root.join('lib/gitlab/redis/shared_state')
require_dependency Rails.root.join('lib/gitlab/current_settings')
require_dependency Rails.root.join('lib/gitlab/middleware/read_only')
require_dependency Rails.root.join('lib/gitlab/middleware/basic_health_check')
......@@ -262,17 +258,6 @@ module Gitlab
end
end
# Use caching across all environments
# Full list of options:
# https://api.rubyonrails.org/classes/ActiveSupport/Cache/RedisCacheStore.html#method-c-new
caching_config_hash = {}
caching_config_hash[:redis] = Gitlab::Redis::Cache.pool
caching_config_hash[:compress] = Gitlab::Utils.to_boolean(ENV.fetch('ENABLE_REDIS_CACHE_COMPRESSION', '1'))
caching_config_hash[:namespace] = Gitlab::Redis::Cache::CACHE_NAMESPACE
caching_config_hash[:expires_in] = 2.weeks # Cache should not grow forever
config.cache_store = :redis_cache_store, caching_config_hash
config.active_job.queue_adapter = :sidekiq
# This is needed for gitlab-shell
......
......@@ -1070,6 +1070,9 @@ production: &base
## ActionCable settings
action_cable:
# Enables handling of ActionCable requests on the Puma web workers
# When this is disabled, a standalone ActionCable server must be started
in_app: true
# Number of threads used to process ActionCable connection callbacks and channel actions
# worker_pool_size: 4
......
......@@ -726,6 +726,7 @@ Settings.webpack.dev_server['port'] ||= 3808
# ActionCable settings
#
Settings['action_cable'] ||= Settingslogic.new({})
Settings.action_cable['in_app'] ||= false
Settings.action_cable['worker_pool_size'] ||= 4
#
......
# Use caching across all environments
# Full list of options:
# https://api.rubyonrails.org/classes/ActiveSupport/Cache/RedisCacheStore.html#method-c-new
caching_config_hash = {}
caching_config_hash[:redis] = Gitlab::Redis::Cache.pool
caching_config_hash[:compress] = Gitlab::Utils.to_boolean(ENV.fetch('ENABLE_REDIS_CACHE_COMPRESSION', '1'))
caching_config_hash[:namespace] = Gitlab::Redis::Cache::CACHE_NAMESPACE
caching_config_hash[:expires_in] = 2.weeks # Cache should not grow forever
Gitlab::Application.config.cache_store = :redis_cache_store, caching_config_hash
# Make sure we initialize a Redis connection pool before multi-threaded
# execution starts by
# 1. Sidekiq
......
......@@ -3,9 +3,9 @@
require 'action_cable/subscription_adapter/redis'
Rails.application.configure do
# We only mount the ActionCable engine in tests where we run it in-app
# For other environments, we run it on a standalone Puma server
config.action_cable.mount_path = Rails.env.test? ? '/-/cable' : nil
# Mount the ActionCable engine when in-app mode is enabled
config.action_cable.mount_path = Gitlab.config.action_cable.in_app ? '/-/cable' : nil
config.action_cable.url = Gitlab::Utils.append_path(Gitlab.config.gitlab.relative_url_root, '/-/cable')
config.action_cable.worker_pool_size = Gitlab.config.action_cable.worker_pool_size
end
......
......@@ -37,7 +37,7 @@ module Gitlab
end
def puma?
!!defined?(::Puma) && !defined?(ACTION_CABLE_SERVER)
!!defined?(::Puma)
end
# For unicorn, we need to check for actual server instances to avoid false positives.
......@@ -70,11 +70,11 @@ module Gitlab
end
def web_server?
puma? || unicorn? || action_cable?
puma? || unicorn?
end
def action_cable?
!!defined?(ACTION_CABLE_SERVER)
web_server? && (!!defined?(ACTION_CABLE_SERVER) || Gitlab.config.action_cable.in_app)
end
def multi_threaded?
......@@ -82,19 +82,21 @@ module Gitlab
end
def max_threads
main_thread = 1
threads = 1 # main thread
if action_cable?
Gitlab::Application.config.action_cable.worker_pool_size
elsif puma?
Puma.cli_config.options[:max_threads]
if puma?
threads += Puma.cli_config.options[:max_threads]
elsif sidekiq?
# An extra thread for the poller in Sidekiq Cron:
# https://github.com/ondrejbartas/sidekiq-cron#under-the-hood
Sidekiq.options[:concurrency] + 1
else
0
end + main_thread
threads += Sidekiq.options[:concurrency] + 1
end
if action_cable?
threads += Gitlab.config.action_cable.worker_pool_size
end
threads
end
end
end
......
......@@ -48,18 +48,45 @@ describe Gitlab::Runtime do
before do
stub_const('::Puma', puma_type)
allow(puma_type).to receive_message_chain(:cli_config, :options).and_return(max_threads: 2)
stub_config(action_cable: { in_app: false })
end
it_behaves_like "valid runtime", :puma, 3
context "when ActionCable in-app mode is enabled" do
before do
stub_config(action_cable: { in_app: true, worker_pool_size: 3 })
end
it_behaves_like "valid runtime", :puma, 6
end
context "when ActionCable standalone is run" do
before do
stub_const('ACTION_CABLE_SERVER', true)
stub_config(action_cable: { worker_pool_size: 8 })
end
it_behaves_like "valid runtime", :puma, 11
end
end
context "unicorn" do
before do
stub_const('::Unicorn', Module.new)
stub_const('::Unicorn::HttpServer', Class.new)
stub_config(action_cable: { in_app: false })
end
it_behaves_like "valid runtime", :unicorn, 1
context "when ActionCable in-app mode is enabled" do
before do
stub_config(action_cable: { in_app: true, worker_pool_size: 3 })
end
it_behaves_like "valid runtime", :unicorn, 4
end
end
context "sidekiq" do
......@@ -105,17 +132,4 @@ describe Gitlab::Runtime do
it_behaves_like "valid runtime", :rails_runner, 1
end
context "action_cable" do
before do
stub_const('ACTION_CABLE_SERVER', true)
stub_const('::Puma', Module.new)
allow(Gitlab::Application).to receive_message_chain(:config, :action_cable, :worker_pool_size).and_return(8)
end
it "reports its maximum concurrency based on ActionCable's worker pool size" do
expect(subject.max_threads).to eq(9)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment