diff --git a/lib/puma/binder.rb b/lib/puma/binder.rb index 7759c85b..252e5564 100644 --- a/lib/puma/binder.rb +++ b/lib/puma/binder.rb @@ -6,6 +6,7 @@ require 'socket' require 'puma/const' require 'puma/util' require 'puma/minissl/context_builder' +require 'puma/configuration' module Puma class Binder @@ -13,18 +14,19 @@ module Puma RACK_VERSION = [1,6].freeze - def initialize(events) + def initialize(events, conf = Configuration.new) @events = events @listeners = [] @inherited_fds = {} @activated_sockets = {} @unix_paths = [] + @conf = conf @proto_env = { "rack.version".freeze => RACK_VERSION, "rack.errors".freeze => events.stderr, - "rack.multithread".freeze => true, - "rack.multiprocess".freeze => false, + "rack.multithread".freeze => resolve_option(:multithread, @conf), + "rack.multiprocess".freeze => resolve_option(:multiprocess, @conf), "rack.run_once".freeze => false, "SCRIPT_NAME".freeze => ENV['SCRIPT_NAME'] || "", @@ -383,5 +385,14 @@ module Puma def socket_activation_fd(int) int + 3 # 3 is the magic number you add to follow the SA protocol end + + def resolve_option(key, conf) + case key + when :multithread + conf.options[:max_threads] > 1 + when :multiprocess + conf.options[:workers] >= 1 + end + end end end diff --git a/lib/puma/launcher.rb b/lib/puma/launcher.rb index 90646b7a..3ec065aa 100644 --- a/lib/puma/launcher.rb +++ b/lib/puma/launcher.rb @@ -47,7 +47,7 @@ module Puma @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 344cd395..9c5bf848 100644 --- a/test/test_binder.rb +++ b/test/test_binder.rb @@ -6,6 +6,7 @@ require_relative "helpers/ssl" require "puma/binder" require "puma/puma_http11" require "puma/events" +require "puma/configuration" class TestBinderBase < Minitest::Test include SSLHelper @@ -295,6 +296,34 @@ class TestBinder < TestBinderBase 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)