Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of Puma's automatic worker count configuration #4166

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was already a sub-dependency, but I wanted to make it explicit since this feature requires it

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 @@ -267,6 +267,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
Loading