Skip to content

Commit da22df3

Browse files
authored
Allow use of Puma's automatic worker count configuration (#4166)
1 parent 18f876a commit da22df3

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ gem 'addressable'
44
gem 'allowy', '>= 2.1.0'
55
gem 'clockwork', require: false
66
gem 'cloudfront-signer'
7+
gem 'concurrent-ruby'
78
gem 'digest-xxhash'
89
gem 'eventmachine', '~> 1.2.7'
910
gem 'fluent-logger'

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ DEPENDENCIES
595595
clockwork
596596
cloudfront-signer
597597
codeclimate-test-reporter (>= 1.0.8)
598+
concurrent-ruby
598599
debug (~> 1.10)
599600
digest-xxhash
600601
eventmachine (~> 1.2.7)

lib/cloud_controller/config_schemas/base/api_schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class ApiSchema < VCAP::Config
276276

277277
webserver: String, # thin or puma
278278
optional(:puma) => {
279+
automatic_worker_count: bool,
279280
workers: Integer,
280281
max_threads: Integer,
281282
optional(:max_db_connections_per_process) => Integer

lib/cloud_controller/runners/puma_runner.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class PumaRunner
88
def initialize(config, app, logger, periodic_updater, request_logs)
99
@logger = logger
1010

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

22-
conf.workers(config.get(:puma, :workers) || 1)
23+
conf.workers(config.get(:puma, :workers) || 1) unless config.get(:puma, :automatic_worker_count)
2324
num_threads = config.get(:puma, :max_threads) || 1
2425
conf.threads(num_threads, num_threads)
2526

spec/unit/lib/cloud_controller/runners/puma_runner_spec.rb

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module VCAP::CloudController
1515
let(:dependency_locator) { instance_spy(CloudController::DependencyLocator) }
1616
let(:prometheus_updater) { spy(VCAP::CloudController::Metrics::PrometheusUpdater) }
1717

18-
subject do
18+
let(:test_config) do
1919
TestConfig.override(
2020
external_port: port,
2121
nginx: {
@@ -27,7 +27,10 @@ module VCAP::CloudController
2727
max_threads: max_threads
2828
}
2929
)
30-
PumaRunner.new(TestConfig.config_instance, app, logger, periodic_updater, request_logs)
30+
end
31+
32+
subject do
33+
PumaRunner.new(test_config, app, logger, periodic_updater, request_logs)
3134
end
3235

3336
before do
@@ -93,6 +96,52 @@ module VCAP::CloudController
9396
end
9497
end
9598

99+
context 'when setting "automatic_worker_count" to false' do
100+
let(:test_config) do
101+
TestConfig.override(
102+
puma: {
103+
workers: 1,
104+
automatic_worker_count: false
105+
}
106+
)
107+
end
108+
109+
before do
110+
allow(::Concurrent).to receive(:available_processor_count).and_return 8
111+
end
112+
113+
it 'configures number of workers to the detected number of cores' do
114+
subject
115+
116+
expect(puma_launcher.config.final_options[:workers]).to eq(1)
117+
expect(puma_launcher.config.final_options[:min_threads]).to eq(1)
118+
expect(puma_launcher.config.final_options[:max_threads]).to eq(1)
119+
end
120+
end
121+
122+
context 'when setting "automatic_worker_count" to true' do
123+
let(:test_config) do
124+
TestConfig.override(
125+
puma: {
126+
workers: 1,
127+
automatic_worker_count: true
128+
}
129+
)
130+
end
131+
132+
before do
133+
allow(::Concurrent).to receive(:available_processor_count).and_return 8
134+
end
135+
136+
it 'configures number of workers the specified number of workers' do
137+
subject
138+
139+
expect(puma_launcher.config.final_options[:workers]).to eq(8)
140+
expect(puma_launcher.config.final_options[:min_threads]).to eq(1)
141+
expect(puma_launcher.config.final_options[:max_threads]).to eq(1)
142+
end
143+
end
144+
96145
it 'configures the app as middleware' do
97146
subject
98147

0 commit comments

Comments
 (0)