Skip to content

Commit b3ccafd

Browse files
authored
[ruby/rack] Hardcode Puma to 5 threads (#10388)
This should also improve the performance of JRuby where previously the threads were hardcoded to 512: +---------------------+-----+-----+-----+------+-------+---------+--------------+ | branch_name| json| db|query|update|fortune|plaintext|weighted_score| +---------------------+-----+-----+-----+------+-------+---------+--------------+ | master|15415|14528|12212| 11690| 13936| 144433| 1185| | threads = processors|30345|45011|32726| 22101| 39920| 112966| 2512| |rack/puma-max-threads|29204|50077|45459| 25358| 41560| 117562| 3026| +---------------------+-----+-----+-----+------+-------+---------+--------------+
1 parent 94a8449 commit b3ccafd

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
require_relative 'auto_tune'
2-
3-
# FWBM only... use the puma_auto_tune gem in production!
4-
num_workers, num_threads = auto_tune
5-
6-
if RUBY_PLATFORM == 'java'
7-
num_threads = 512
8-
num_workers = 0
9-
end
10-
11-
threads num_threads
12-
13-
if num_workers > 0
1+
if ENV.fetch('WEB_CONCURRENCY') == 'auto'
142
before_fork do
153
Sequel::DATABASES.each(&:disconnect)
164
end
5+
else
6+
workers ENV.fetch('WEB_CONCURRENCY')
7+
require 'concurrent/utility/processor_counter'
8+
threads = (::Concurrent.available_processor_count * 1.5).to_i
9+
threads threads
10+
ENV['MAX_THREADS'] = threads.to_s
1711
end

frameworks/Ruby/rack/hello_world.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class HelloWorld
4343
</html>'
4444

4545
def initialize
46-
if defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
47-
max_connections = threads
46+
if defined?(Puma)
47+
max_connections = ENV.fetch('MAX_THREADS')
4848
elsif defined?(Itsi)
4949
require_relative 'config/auto_tune'
5050
_num_workers, num_threads = auto_tune

frameworks/Ruby/rack/pg_db.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
Jdbc::Postgres.load_driver
88
end
99

10-
11-
1210
class PgDb
1311
QUERY_RANGE = (1..10_000).freeze # range of IDs in the Fortune DB
1412
ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB

frameworks/Ruby/rack/rack-jruby.dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ WORKDIR /rack
66

77
COPY Gemfile* ./
88

9+
#RUN echo $(ruby config/auto_tune.rb | grep -Eo '[0-9]+' | head -n 1)
10+
911
RUN bundle config set with 'puma'
1012
RUN bundle install --jobs=8
1113

1214
COPY . .
1315

14-
EXPOSE 8080
16+
ENV WEB_CONCURRENCY=0
1517

16-
CMD config/java_tune.sh
18+
EXPOSE 8080
1719

1820
CMD bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:8080 -e production

frameworks/Ruby/rack/rack.dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ RUN bundle install --jobs=8
1717

1818
COPY . .
1919

20-
EXPOSE 8080
2120
ENV WEB_CONCURRENCY=auto
21+
ENV MAX_THREADS=5
22+
23+
EXPOSE 8080
2224

2325
CMD bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:8080 -e production

0 commit comments

Comments
 (0)