Skip to content

Commit 33626d0

Browse files
authored
Merge pull request #685 from p8/rails/puma-8
[rails] Upgrade Puma to 8
2 parents 0805486 + 1e12234 commit 33626d0

5 files changed

Lines changed: 48 additions & 30 deletions

File tree

frameworks/rails/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ENV RUBY_MN_THREADS=1
1212
ENV RACK_ENV=production
1313
ENV WEB_CONCURRENCY=auto
1414
ENV RAILS_MAX_THREADS=3
15+
ENV MAX_IO_THREADS=5
1516

1617
WORKDIR /app
1718

frameworks/rails/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source 'https://rubygems.org'
22

33
gem 'rails', '~> 8.0'
4-
gem 'puma', '~> 7.2'
4+
gem 'puma', '~> 8.0'
55
gem 'pg', '~> 1.5'
66
gem 'bootsnap', require: false
77
gem 'connection_pool'

frameworks/rails/Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ GEM
137137
psych (5.3.1)
138138
date
139139
stringio
140-
puma (7.2.0)
140+
puma (8.0.1)
141141
nio4r (~> 2.0)
142142
racc (1.8.1)
143143
rack (3.2.6)
@@ -208,7 +208,7 @@ DEPENDENCIES
208208
bootsnap
209209
connection_pool
210210
pg (~> 1.5)
211-
puma (~> 7.2)
211+
puma (~> 8.0)
212212
rails (~> 8.0)
213213

214214
CHECKSUMS
@@ -260,7 +260,7 @@ CHECKSUMS
260260
prettyprint (0.2.0) sha256=2bc9e15581a94742064a3cc8b0fb9d45aae3d03a1baa6ef80922627a0766f193
261261
prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85
262262
psych (5.3.1) sha256=eb7a57cef10c9d70173ff74e739d843ac3b2c019a003de48447b2963d81b1974
263-
puma (7.2.0) sha256=bf8ef4ab514a4e6d4554cb4326b2004eba5036ae05cf765cfe51aba9706a72a8
263+
puma (8.0.1) sha256=7b94e50c07655718c1fb8ae41a11fc06c7d61293208b3aa608ff71a46d3ad37c
264264
racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f
265265
rack (3.2.6) sha256=5ed78e1f73b2e25679bec7d45ee2d4483cc4146eb1be0264fc4d94cb5ef212c2
266266
rack-session (2.1.2) sha256=595434f8c0c3473ae7d7ac56ecda6cc6dfd9d37c0b2b5255330aa1576967ffe8

frameworks/rails/config/application.rb

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,46 @@
33

44
Bundler.require(*Rails.groups)
55

6+
# Catch unknown HTTP methods, routing errors, and mark /upload as binary
7+
class MethodGuard
8+
VALID_METHODS = %w[GET HEAD POST PUT DELETE PATCH OPTIONS TRACE].to_set.freeze
9+
10+
def initialize(app)
11+
@app = app
12+
end
13+
14+
def call(env)
15+
unless VALID_METHODS.include?(env['REQUEST_METHOD'])
16+
return [405, { 'content-type' => 'text/plain' }, ['Method Not Allowed']]
17+
end
18+
# Mark /upload as binary so Rack skips form parameter parsing
19+
if env['PATH_INFO'] == '/upload'
20+
env['CONTENT_TYPE'] = 'application/octet-stream'
21+
end
22+
@app.call(env)
23+
rescue => e
24+
if e.class.name.include?('UnknownHttpMethod') || e.class.name.include?('RoutingError')
25+
[400, { 'content-type' => 'text/plain' }, ['Bad Request']]
26+
else
27+
raise
28+
end
29+
end
30+
end
31+
32+
# Threads marked as IO bound are allowed to go over Puma's max thread limit.
33+
class MarkAsIOBoundThreads
34+
def initialize(app)
35+
@app = app
36+
end
37+
38+
def call(env)
39+
if env['PATH_INFO'].start_with?('/baseline')
40+
env["puma.mark_as_io_bound"].call
41+
end
42+
@app.call(env)
43+
end
44+
end
45+
646
class BenchmarkApp < Rails::Application
747
config.load_defaults Rails::VERSION::STRING.to_f
848
config.eager_load = true
@@ -25,32 +65,8 @@ class BenchmarkApp < Rails::Application
2565

2666
# Add gzip support
2767
config.middleware.insert 0, Rack::Deflater
28-
29-
# Catch unknown HTTP methods, routing errors, and mark /upload as binary
30-
config.middleware.insert 0, Class.new {
31-
VALID_METHODS = %w[GET HEAD POST PUT DELETE PATCH OPTIONS TRACE].to_set.freeze
32-
33-
def initialize(app)
34-
@app = app
35-
end
36-
37-
def call(env)
38-
unless VALID_METHODS.include?(env['REQUEST_METHOD'])
39-
return [405, { 'content-type' => 'text/plain' }, ['Method Not Allowed']]
40-
end
41-
# Mark /upload as binary so Rack skips form parameter parsing
42-
if env['PATH_INFO'] == '/upload'
43-
env['CONTENT_TYPE'] = 'application/octet-stream'
44-
end
45-
@app.call(env)
46-
rescue => e
47-
if e.class.name.include?('UnknownHttpMethod') || e.class.name.include?('RoutingError')
48-
[400, { 'content-type' => 'text/plain' }, ['Bad Request']]
49-
else
50-
raise
51-
end
52-
end
53-
}
68+
config.middleware.insert 0, MethodGuard
69+
config.middleware.insert 0, MarkAsIOBoundThreads
5470

5571
# Silence logging
5672
config.logger = nil

frameworks/rails/config/puma.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
thread_count = ENV.fetch('RAILS_MAX_THREADS', 4).to_i
22
threads thread_count, thread_count
3+
max_io_threads ENV.fetch("MAX_IO_THREADS", 10).to_i
34

45
tls_cert_path = ENV.fetch('TLS_CERT', '/certs/server.crt')
56
tls_key_path = ENV.fetch('TLS_KEY', '/certs/server.key')

0 commit comments

Comments
 (0)