diff --git a/History.md b/History.md index 517fb05d06..2bbc15c3e4 100644 --- a/History.md +++ b/History.md @@ -11,6 +11,7 @@ * Faster phased restart and worker timeout (#2220) * Added `state_permission` to config DSL to set state file permissions (#2238) * Added `Puma.stats_hash`, which returns a stats in Hash instead of a JSON string (#2086, #2253) + * `rack.multithread` and `rack.multiprocess` now dynamically resolved by `max_thread` and `workers` respectively (#2288) * Deprecations, Removals and Breaking API Changes * `--control` has been removed. Use `--control-url` (#1487) diff --git a/lib/puma/binder.rb b/lib/puma/binder.rb index 7759c85b66..3f1b571935 100644 --- a/lib/puma/binder.rb +++ b/lib/puma/binder.rb @@ -6,6 +6,7 @@ require 'puma/const' require 'puma/util' require 'puma/minissl/context_builder' +require 'puma/configuration' module Puma class Binder @@ -13,7 +14,7 @@ class Binder RACK_VERSION = [1,6].freeze - def initialize(events) + def initialize(events, conf = Configuration.new) @events = events @listeners = [] @inherited_fds = {} @@ -23,8 +24,8 @@ def initialize(events) @proto_env = { "rack.version".freeze => RACK_VERSION, "rack.errors".freeze => events.stderr, - "rack.multithread".freeze => true, - "rack.multiprocess".freeze => false, + "rack.multithread".freeze => conf.options[:max_threads] > 1, + "rack.multiprocess".freeze => conf.options[:workers] >= 1, "rack.run_once".freeze => false, "SCRIPT_NAME".freeze => ENV['SCRIPT_NAME'] || "", diff --git a/lib/puma/launcher.rb b/lib/puma/launcher.rb index 90646b7a73..3ec065aa5e 100644 --- a/lib/puma/launcher.rb +++ b/lib/puma/launcher.rb @@ -47,7 +47,7 @@ def initialize(conf, launcher_args={}) @original_argv = @argv.dup @config = conf - @binder = Binder.new(@events) + @binder = Binder.new(@events, conf) @binder.create_inherited_fds(ENV).each { |k| ENV.delete k } @binder.create_activated_fds(ENV).each { |k| ENV.delete k } diff --git a/test/test_binder.rb b/test/test_binder.rb index 344cd395e9..9c5bf84800 100644 --- a/test/test_binder.rb +++ b/test/test_binder.rb @@ -6,6 +6,7 @@ require "puma/binder" require "puma/puma_http11" require "puma/events" +require "puma/configuration" class TestBinderBase < Minitest::Test include SSLHelper @@ -295,6 +296,34 @@ def test_socket_activation_unix File.unlink(path) rescue nil # JRuby race? end + def test_rack_multithread_default_configuration + binder = Puma::Binder.new(@events) + + assert binder.proto_env["rack.multithread"] + end + + def test_rack_multithread_custom_configuration + conf = Puma::Configuration.new(max_threads: 1) + + binder = Puma::Binder.new(@events, conf) + + refute binder.proto_env["rack.multithread"] + end + + def test_rack_multiprocess_default_configuration + binder = Puma::Binder.new(@events) + + refute binder.proto_env["rack.multiprocess"] + end + + def test_rack_multiprocess_custom_configuration + conf = Puma::Configuration.new(workers: 1) + + binder = Puma::Binder.new(@events, conf) + + assert binder.proto_env["rack.multiprocess"] + end + private def assert_activates_sockets(path: nil, port: nil, url: nil, sock: nil)