Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically resolve rack.multithread/rack.multiprocess #2288

Merged
merged 3 commits into from Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions lib/puma/binder.rb
Expand Up @@ -6,14 +6,15 @@
require 'puma/const'
require 'puma/util'
require 'puma/minissl/context_builder'
require 'puma/configuration'

module Puma
class Binder
include Puma::Const

RACK_VERSION = [1,6].freeze

def initialize(events)
def initialize(events, conf = Configuration.new)
@events = events
@listeners = []
@inherited_fds = {}
Expand All @@ -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'] || "",

Expand Down
2 changes: 1 addition & 1 deletion lib/puma/launcher.rb
Expand Up @@ -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 }

Expand Down
29 changes: 29 additions & 0 deletions test/test_binder.rb
Expand Up @@ -6,6 +6,7 @@
require "puma/binder"
require "puma/puma_http11"
require "puma/events"
require "puma/configuration"

class TestBinderBase < Minitest::Test
include SSLHelper
Expand Down Expand Up @@ -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)
Expand Down