mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Minor refactor on Thread pool (#1088)
* Move shutdown grace time constant to ThreadPool. SHUTDOWN_GRACE_TIME is the only constant (from const.rb) used by ThreadPool. It's better to move the constant than require all const.rb. * Fix minor typo. * Don't need to check if timeout is zero to immediately shutdown. This removes the duplicated code and add test for forced shutdowns.
This commit is contained in:
parent
13ac97f026
commit
64f930dfb7
3 changed files with 27 additions and 17 deletions
|
@ -113,11 +113,6 @@ module Puma
|
|||
# sending data back
|
||||
WRITE_TIMEOUT = 10
|
||||
|
||||
# How long, after raising the ForceShutdown of a thread during
|
||||
# forced shutdown mode, to wait for the thread to try and finish
|
||||
# up it's work before leaving the thread to die on the vine.
|
||||
SHUTDOWN_GRACE_TIME = 5 # seconds
|
||||
|
||||
# The original URI requested by the client.
|
||||
REQUEST_URI= 'REQUEST_URI'.freeze
|
||||
REQUEST_PATH = 'REQUEST_PATH'.freeze
|
||||
|
|
|
@ -4,10 +4,14 @@ module Puma
|
|||
# A simple thread pool management object.
|
||||
#
|
||||
class ThreadPool
|
||||
|
||||
class ForceShutdown < RuntimeError
|
||||
end
|
||||
|
||||
# How long, after raising the ForceShutdown of a thread during
|
||||
# forced shutdown mode, to wait for the thread to try and finish
|
||||
# up its work before leaving the thread to die on the vine.
|
||||
SHUTDOWN_GRACE_TIME = 5 # seconds
|
||||
|
||||
# Maintain a minimum of +min+ and maximum of +max+ threads
|
||||
# in the pool.
|
||||
#
|
||||
|
@ -259,18 +263,12 @@ module Puma
|
|||
@workers.dup
|
||||
end
|
||||
|
||||
case timeout
|
||||
when -1
|
||||
if timeout == -1
|
||||
# Wait for threads to finish without force shutdown.
|
||||
threads.each(&:join)
|
||||
when 0
|
||||
threads.each do |t|
|
||||
t.raise ForceShutdown
|
||||
end
|
||||
|
||||
threads.each do |t|
|
||||
t.join Const::SHUTDOWN_GRACE_TIME
|
||||
end
|
||||
else
|
||||
# Wait for threads to finish after n attempts (+timeout+).
|
||||
# If threads are still running, it will forcefully kill them.
|
||||
timeout.times do
|
||||
threads.delete_if do |t|
|
||||
t.join 1
|
||||
|
@ -288,7 +286,7 @@ module Puma
|
|||
end
|
||||
|
||||
threads.each do |t|
|
||||
t.join Const::SHUTDOWN_GRACE_TIME
|
||||
t.join SHUTDOWN_GRACE_TIME
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -230,4 +230,21 @@ class TestThreadPool < Test::Unit::TestCase
|
|||
|
||||
assert_equal 0, pool.spawned
|
||||
end
|
||||
|
||||
def test_force_shutdown_immediately
|
||||
pool = new_pool(1, 1) {
|
||||
begin
|
||||
sleep 1
|
||||
rescue Puma::ThreadPool::ForceShutdown
|
||||
end
|
||||
}
|
||||
|
||||
pool << 1
|
||||
|
||||
sleep 0.1
|
||||
|
||||
pool.shutdown(0)
|
||||
|
||||
assert_equal 0, pool.spawned
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue