33
44Bundler . 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+
646class 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
0 commit comments