1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Add min:max thread settings

This commit is contained in:
Evan Phoenix 2011-09-27 16:33:17 -05:00
parent 9d377ddd06
commit b2a7d84aa2
4 changed files with 51 additions and 37 deletions

View file

@ -14,6 +14,8 @@ module Puma
@stdout = stdout
@stderr = stderr
@events = Events.new @stdout, @stderr
setup_options
end
@ -28,14 +30,22 @@ module Puma
def setup_options
@options = {
:concurrency => 16
:min_threads => 0,
:max_threads => 16
}
@binds = []
@parser = OptionParser.new do |o|
o.on '-n', '--concurrency INT', "Number of concurrent threads to use" do |arg|
@options[:concurrency] = arg.to_i
o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
min, max = arg.split(":")
if max
@options[:min_threads] = min.to_i
@options[:max_threads] = max.to_i
else
@options[:min_threads] = 0
@options[:max_threads] = arg.to_i
end
end
o.on "-b", "--bind URI", "URI to bind to (tcp:// and unix:// only)" do |arg|
@ -78,12 +88,18 @@ module Puma
@options[:Port] ||= DefaultTCPPort
end
server = Puma::Server.new @app, @options[:concurrency]
min_t = @options[:min_threads]
max_t = @options[:max_threads]
server = Puma::Server.new @app, @events
server.min_threads = min_t
server.max_threads = max_t
log "Puma #{Puma::Const::PUMA_VERSION} starting..."
log "* Min threads: #{min_t}, max threads: #{max_t}"
if @options[:Host]
log "Listening on tcp://#{@options[:Host]}:#{@options[:Port]}"
log "* Listening on tcp://#{@options[:Host]}:#{@options[:Port]}"
server.add_tcp_listener @options[:Host], @options[:Port]
end
@ -91,10 +107,10 @@ module Puma
uri = URI.parse str
case uri.scheme
when "tcp"
log "Listening on #{str}"
log "* Listening on #{str}"
server.add_tcp_listener uri.host, uri.port
when "unix"
log "Listening on #{str}"
log "* Listening on #{str}"
path = "#{uri.host}#{uri.path}"
server.add_unix_listener path

View file

@ -15,14 +15,12 @@ module Puma
include Puma::Const
attr_reader :acceptor
attr_reader :host
attr_reader :port
attr_reader :concurrent
attr_reader :thread
attr_reader :events
attr_accessor :app
attr_reader :events
attr_accessor :min_threads
attr_accessor :max_threads
# Creates a working server on host:port (strange things happen if port
# isn't a Number).
@ -30,26 +28,20 @@ module Puma
# Use HttpServer#run to start the server and HttpServer#acceptor.join to
# join the thread that's processing incoming requests on the socket.
#
# +concurrent+ indicates how many concurrent requests should be run at
# the same time. Any requests over this ammount are queued and handled
# as soon as a thread is available.
#
def initialize(app, concurrent=10, events=Events::DEFAULT)
@concurrent = concurrent
@check, @notify = IO.pipe
@ios = [@check]
@running = true
@thread_pool = ThreadPool.new(0, concurrent) do |client|
process_client(client)
end
def initialize(app, events=Events::DEFAULT)
@app = app
@events = events
@app = app
@check, @notify = IO.pipe
@ios = [@check]
@running = false
@min_threads = 0
@max_threads = 16
@thread = nil
@thread_pool = nil
@proto_env = {
"rack.version".freeze => Rack::VERSION,
@ -80,7 +72,13 @@ module Puma
def run
BasicSocket.do_not_reverse_lookup = true
@acceptor = Thread.new do
@running = true
@thread_pool = ThreadPool.new(@min_threads, @max_threads) do |client|
process_client(client)
end
@thread = Thread.new do
begin
check = @check
sockets = @ios
@ -109,7 +107,7 @@ module Puma
end
end
return @acceptor
return @thread
end
def handle_check
@ -307,7 +305,7 @@ module Puma
# Wait for all outstanding requests to finish.
def graceful_shutdown
@thread_pool.shutdown
@thread_pool.shutdown if @thread_pool
end
# Stops the acceptor thread and then causes the worker threads to finish
@ -315,7 +313,7 @@ module Puma
def stop(sync=false)
@notify << STOP_COMMAND
@acceptor.join if @acceptor && sync
@thread.join if @thread && sync
end
end
end

View file

@ -10,7 +10,7 @@ class TestPumaUnixSocket < Test::Unit::TestCase
Path = "test/puma.sock"
def setup
@server = Puma::Server.new App, 2
@server = Puma::Server.new App
end
def teardown

View file

@ -22,7 +22,7 @@ class WebServerTest < Test::Unit::TestCase
@tester = TestHandler.new
@server = Server.new @tester, 1, Events.strings
@server = Server.new @tester, Events.strings
@server.add_tcp_listener "127.0.0.1", 9998
redirect_test_io do