Skip to content

Commit

Permalink
Allow use of Puma's automatic worker count configuration (#4166)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethboyles authored Jan 9, 2025
1 parent 18f876a commit da22df3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gem 'addressable'
gem 'allowy', '>= 2.1.0'
gem 'clockwork', require: false
gem 'cloudfront-signer'
gem 'concurrent-ruby'
gem 'digest-xxhash'
gem 'eventmachine', '~> 1.2.7'
gem 'fluent-logger'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ DEPENDENCIES
clockwork
cloudfront-signer
codeclimate-test-reporter (>= 1.0.8)
concurrent-ruby
debug (~> 1.10)
digest-xxhash
eventmachine (~> 1.2.7)
Expand Down
1 change: 1 addition & 0 deletions lib/cloud_controller/config_schemas/base/api_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class ApiSchema < VCAP::Config

webserver: String, # thin or puma
optional(:puma) => {
automatic_worker_count: bool,
workers: Integer,
max_threads: Integer,
optional(:max_db_connections_per_process) => Integer
Expand Down
3 changes: 2 additions & 1 deletion lib/cloud_controller/runners/puma_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PumaRunner
def initialize(config, app, logger, periodic_updater, request_logs)
@logger = logger

ENV['WEB_CONCURRENCY'] = 'auto' if config.get(:puma, :automatic_worker_count)
puma_config = Puma::Configuration.new do |conf|
if config.get(:nginx, :use_nginx)
if config.get(:nginx, :instance_socket).nil? || config.get(:nginx, :instance_socket).empty?
Expand All @@ -19,7 +20,7 @@ def initialize(config, app, logger, periodic_updater, request_logs)
conf.bind "tcp://0.0.0.0:#{config.get(:external_port)}"
end

conf.workers(config.get(:puma, :workers) || 1)
conf.workers(config.get(:puma, :workers) || 1) unless config.get(:puma, :automatic_worker_count)
num_threads = config.get(:puma, :max_threads) || 1
conf.threads(num_threads, num_threads)

Expand Down
53 changes: 51 additions & 2 deletions spec/unit/lib/cloud_controller/runners/puma_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module VCAP::CloudController
let(:dependency_locator) { instance_spy(CloudController::DependencyLocator) }
let(:prometheus_updater) { spy(VCAP::CloudController::Metrics::PrometheusUpdater) }

subject do
let(:test_config) do
TestConfig.override(
external_port: port,
nginx: {
Expand All @@ -27,7 +27,10 @@ module VCAP::CloudController
max_threads: max_threads
}
)
PumaRunner.new(TestConfig.config_instance, app, logger, periodic_updater, request_logs)
end

subject do
PumaRunner.new(test_config, app, logger, periodic_updater, request_logs)
end

before do
Expand Down Expand Up @@ -93,6 +96,52 @@ module VCAP::CloudController
end
end

context 'when setting "automatic_worker_count" to false' do
let(:test_config) do
TestConfig.override(
puma: {
workers: 1,
automatic_worker_count: false
}
)
end

before do
allow(::Concurrent).to receive(:available_processor_count).and_return 8
end

it 'configures number of workers to the detected number of cores' do
subject

expect(puma_launcher.config.final_options[:workers]).to eq(1)
expect(puma_launcher.config.final_options[:min_threads]).to eq(1)
expect(puma_launcher.config.final_options[:max_threads]).to eq(1)
end
end

context 'when setting "automatic_worker_count" to true' do
let(:test_config) do
TestConfig.override(
puma: {
workers: 1,
automatic_worker_count: true
}
)
end

before do
allow(::Concurrent).to receive(:available_processor_count).and_return 8
end

it 'configures number of workers the specified number of workers' do
subject

expect(puma_launcher.config.final_options[:workers]).to eq(8)
expect(puma_launcher.config.final_options[:min_threads]).to eq(1)
expect(puma_launcher.config.final_options[:max_threads]).to eq(1)
end
end

it 'configures the app as middleware' do
subject

Expand Down

0 comments on commit da22df3

Please sign in to comment.