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

View file

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

View file

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

View file

@ -22,7 +22,7 @@ class WebServerTest < Test::Unit::TestCase
@tester = TestHandler.new @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 @server.add_tcp_listener "127.0.0.1", 9998
redirect_test_io do redirect_test_io do