Skip to content

Commit

Permalink
Merge pull request #2288 from FTLam11/2174-add-cli-options-for-multi
Browse files Browse the repository at this point in the history
Dynamically resolve rack.multithread/rack.multiprocess
  • Loading branch information
nateberkopec committed Jun 2, 2020
2 parents f9ddd58 + 321b3fb commit dab5ff1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
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

0 comments on commit dab5ff1

Please sign in to comment.