2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-09-20 01:16:31 -04:00
|
|
|
require 'thread'
|
|
|
|
|
2011-09-22 22:24:43 -04:00
|
|
|
module Puma
|
2018-05-01 16:46:53 -04:00
|
|
|
# Internal Docs for A simple thread pool management object.
|
2011-12-01 18:23:14 -05:00
|
|
|
#
|
2018-05-01 16:46:53 -04:00
|
|
|
# Each Puma "worker" has a thread pool to process requests.
|
|
|
|
#
|
|
|
|
# First a connection to a client is made in `Puma::Server`. It is wrapped in a
|
|
|
|
# `Puma::Client` instance and then passed to the `Puma::Reactor` to ensure
|
|
|
|
# the whole request is buffered into memory. Once the request is ready, it is passed into
|
|
|
|
# a thread pool via the `Puma::ThreadPool#<<` operator where it is stored in a `@todo` array.
|
|
|
|
#
|
|
|
|
# Each thread in the pool has an internal loop where it pulls a request from the `@todo` array
|
|
|
|
# and proceses it.
|
2011-09-20 01:16:31 -04:00
|
|
|
class ThreadPool
|
2016-04-07 13:25:10 -04:00
|
|
|
class ForceShutdown < RuntimeError
|
|
|
|
end
|
|
|
|
|
2016-11-20 13:36:47 -05:00
|
|
|
# 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
|
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# Maintain a minimum of +min+ and maximum of +max+ threads
|
|
|
|
# in the pool.
|
|
|
|
#
|
|
|
|
# The block passed is the work that will be performed in each
|
|
|
|
# thread.
|
|
|
|
#
|
2013-07-03 10:25:48 -04:00
|
|
|
def initialize(min, max, *extra, &block)
|
2015-01-20 07:20:39 -05:00
|
|
|
@not_empty = ConditionVariable.new
|
|
|
|
@not_full = ConditionVariable.new
|
2011-09-20 01:16:31 -04:00
|
|
|
@mutex = Mutex.new
|
|
|
|
|
2012-02-07 12:49:23 -05:00
|
|
|
@todo = []
|
|
|
|
|
2011-09-20 01:16:31 -04:00
|
|
|
@spawned = 0
|
2012-02-07 12:49:23 -05:00
|
|
|
@waiting = 0
|
|
|
|
|
2013-07-16 20:07:59 -04:00
|
|
|
@min = Integer(min)
|
|
|
|
@max = Integer(max)
|
2013-07-03 10:25:48 -04:00
|
|
|
@block = block
|
2012-08-11 18:09:09 -04:00
|
|
|
@extra = extra
|
2011-09-20 01:16:31 -04:00
|
|
|
|
2012-02-07 12:49:23 -05:00
|
|
|
@shutdown = false
|
|
|
|
|
2011-09-20 01:16:31 -04:00
|
|
|
@trim_requested = 0
|
|
|
|
|
|
|
|
@workers = []
|
|
|
|
|
2011-12-05 13:07:01 -05:00
|
|
|
@auto_trim = nil
|
2015-05-19 10:14:30 -04:00
|
|
|
@reaper = nil
|
2011-12-05 13:07:01 -05:00
|
|
|
|
2012-02-07 12:49:23 -05:00
|
|
|
@mutex.synchronize do
|
2013-07-16 20:07:59 -04:00
|
|
|
@min.times { spawn_thread }
|
2012-02-07 12:49:23 -05:00
|
|
|
end
|
2014-11-26 02:18:30 -05:00
|
|
|
|
|
|
|
@clean_thread_locals = false
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
|
|
|
|
2018-05-04 13:54:05 -04:00
|
|
|
attr_reader :spawned, :trim_requested, :waiting
|
2014-11-26 02:18:30 -05:00
|
|
|
attr_accessor :clean_thread_locals
|
2011-09-20 01:16:31 -04:00
|
|
|
|
2016-02-25 16:35:47 -05:00
|
|
|
def self.clean_thread_locals
|
2017-11-30 10:52:40 -05:00
|
|
|
Thread.current.keys.each do |key| # rubocop: disable Performance/HashEachMethods
|
2016-02-25 16:35:47 -05:00
|
|
|
Thread.current[key] = nil unless key == :__recursive_key__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# How many objects have yet to be processed by the pool?
|
|
|
|
#
|
2011-09-20 01:16:31 -04:00
|
|
|
def backlog
|
2012-02-07 12:49:23 -05:00
|
|
|
@mutex.synchronize { @todo.size }
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
|
|
|
|
2018-05-04 14:02:44 -04:00
|
|
|
def pool_capacity
|
|
|
|
waiting + (@max - spawned)
|
|
|
|
end
|
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# :nodoc:
|
2012-02-07 12:49:23 -05:00
|
|
|
#
|
|
|
|
# Must be called with @mutex held!
|
|
|
|
#
|
2011-09-20 01:16:31 -04:00
|
|
|
def spawn_thread
|
2012-02-07 12:49:23 -05:00
|
|
|
@spawned += 1
|
2011-09-20 01:16:31 -04:00
|
|
|
|
2017-08-02 20:48:40 -04:00
|
|
|
th = Thread.new(@spawned) do |spawned|
|
2016-04-28 10:28:44 -04:00
|
|
|
# Thread name is new in Ruby 2.3
|
2017-08-02 20:48:40 -04:00
|
|
|
Thread.current.name = 'puma %03i' % spawned if Thread.current.respond_to?(:name=)
|
2012-07-05 15:05:29 -04:00
|
|
|
todo = @todo
|
2011-09-20 01:16:31 -04:00
|
|
|
block = @block
|
2012-07-05 15:05:29 -04:00
|
|
|
mutex = @mutex
|
2015-01-20 07:20:39 -05:00
|
|
|
not_empty = @not_empty
|
|
|
|
not_full = @not_full
|
2011-09-20 01:16:31 -04:00
|
|
|
|
2012-08-11 18:09:09 -04:00
|
|
|
extra = @extra.map { |i| i.new }
|
|
|
|
|
2011-09-20 01:16:31 -04:00
|
|
|
while true
|
2012-02-07 12:49:23 -05:00
|
|
|
work = nil
|
|
|
|
|
|
|
|
continue = true
|
|
|
|
|
2012-07-05 15:05:29 -04:00
|
|
|
mutex.synchronize do
|
2012-02-07 12:49:23 -05:00
|
|
|
while todo.empty?
|
|
|
|
if @trim_requested > 0
|
|
|
|
@trim_requested -= 1
|
|
|
|
continue = false
|
2016-02-08 15:47:54 -05:00
|
|
|
not_full.signal
|
2012-02-07 12:49:23 -05:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if @shutdown
|
|
|
|
continue = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
@waiting += 1
|
2015-01-20 07:20:39 -05:00
|
|
|
not_full.signal
|
|
|
|
not_empty.wait mutex
|
2015-01-20 08:23:17 -05:00
|
|
|
@waiting -= 1
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
|
|
|
|
2014-01-25 15:43:30 -05:00
|
|
|
work = todo.shift if continue
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
2012-02-07 12:49:23 -05:00
|
|
|
|
|
|
|
break unless continue
|
|
|
|
|
2014-11-26 02:18:30 -05:00
|
|
|
if @clean_thread_locals
|
2016-02-25 16:35:47 -05:00
|
|
|
ThreadPool.clean_thread_locals
|
2014-10-30 20:52:37 -04:00
|
|
|
end
|
|
|
|
|
2015-06-10 14:52:09 -04:00
|
|
|
begin
|
|
|
|
block.call(work, *extra)
|
2016-07-19 00:26:05 -04:00
|
|
|
rescue Exception => e
|
|
|
|
STDERR.puts "Error reached top of thread-pool: #{e.message} (#{e.class})"
|
2015-06-10 14:52:09 -04:00
|
|
|
end
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
|
|
|
|
2012-07-05 15:05:29 -04:00
|
|
|
mutex.synchronize do
|
2011-09-20 01:16:31 -04:00
|
|
|
@spawned -= 1
|
|
|
|
@workers.delete th
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-07 12:49:23 -05:00
|
|
|
@workers << th
|
2011-09-20 01:16:31 -04:00
|
|
|
|
|
|
|
th
|
|
|
|
end
|
|
|
|
|
2012-02-07 12:49:23 -05:00
|
|
|
private :spawn_thread
|
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# Add +work+ to the todo list for a Thread to pickup and process.
|
2011-09-20 01:16:31 -04:00
|
|
|
def <<(work)
|
2012-02-07 12:49:23 -05:00
|
|
|
@mutex.synchronize do
|
|
|
|
if @shutdown
|
|
|
|
raise "Unable to add work while shutting down"
|
|
|
|
end
|
2011-09-20 01:16:31 -04:00
|
|
|
|
2012-02-07 12:49:23 -05:00
|
|
|
@todo << work
|
|
|
|
|
2014-08-20 00:26:30 -04:00
|
|
|
if @waiting < @todo.size and @spawned < @max
|
2012-02-07 12:49:23 -05:00
|
|
|
spawn_thread
|
|
|
|
end
|
|
|
|
|
2015-01-20 07:20:39 -05:00
|
|
|
@not_empty.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-01 16:46:53 -04:00
|
|
|
# This method is used by `Puma::Server` to let the server know when
|
|
|
|
# the thread pool can pull more requests from the socket and
|
|
|
|
# pass to the reactor.
|
|
|
|
#
|
|
|
|
# The general idea is that the thread pool can only work on a fixed
|
|
|
|
# number of requests at the same time. If it is already processing that
|
|
|
|
# number of requests then it is at capacity. If another Puma process has
|
|
|
|
# spare capacity, then the request can be left on the socket so the other
|
|
|
|
# worker can pick it up and process it.
|
|
|
|
#
|
|
|
|
# For example: if there are 5 threads, but only 4 working on
|
|
|
|
# requests, this method will not wait and the `Puma::Server`
|
|
|
|
# can pull a request right away.
|
|
|
|
#
|
|
|
|
# If there are 5 threads and all 5 of them are busy, then it will
|
|
|
|
# pause here, and wait until the `not_full` condition variable is
|
|
|
|
# signaled, usually this indicates that a request has been processed.
|
|
|
|
#
|
|
|
|
# It's important to note that even though the server might accept another
|
|
|
|
# request, it might not be added to the `@todo` array right away.
|
|
|
|
# For example if a slow client has only sent a header, but not a body
|
|
|
|
# then the `@todo` array would stay the same size as the reactor works
|
|
|
|
# to try to buffer the request. In tha scenario the next call to this
|
|
|
|
# method would not block and another request would be added into the reactor
|
|
|
|
# by the server. This would continue until a fully bufferend request
|
|
|
|
# makes it through the reactor and can then be processed by the thread pool.
|
2018-09-13 14:49:09 -04:00
|
|
|
#
|
|
|
|
# Returns the current number of busy threads, or +nil+ if shutting down.
|
|
|
|
#
|
2015-01-20 07:20:39 -05:00
|
|
|
def wait_until_not_full
|
|
|
|
@mutex.synchronize do
|
2017-04-28 13:42:09 -04:00
|
|
|
while true
|
|
|
|
return if @shutdown
|
|
|
|
|
|
|
|
# If we can still spin up new threads and there
|
2018-04-17 15:00:02 -04:00
|
|
|
# is work queued that cannot be handled by waiting
|
|
|
|
# threads, then accept more work until we would
|
2017-04-28 13:42:09 -04:00
|
|
|
# spin up the max number of threads.
|
2018-09-13 14:49:09 -04:00
|
|
|
busy_threads = @spawned - @waiting + @todo.size
|
|
|
|
return busy_threads if @max > busy_threads
|
2017-04-28 13:42:09 -04:00
|
|
|
|
2015-01-20 07:20:39 -05:00
|
|
|
@not_full.wait @mutex
|
|
|
|
end
|
2012-02-07 12:49:23 -05:00
|
|
|
end
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# If too many threads are in the pool, tell one to finish go ahead
|
2012-02-07 12:49:23 -05:00
|
|
|
# and exit. If +force+ is true, then a trim request is requested
|
|
|
|
# even if all threads are being utilized.
|
2011-12-01 18:23:14 -05:00
|
|
|
#
|
2012-02-07 12:49:23 -05:00
|
|
|
def trim(force=false)
|
2011-09-20 01:16:31 -04:00
|
|
|
@mutex.synchronize do
|
2012-02-07 12:49:23 -05:00
|
|
|
if (force or @waiting > 0) and @spawned - @trim_requested > @min
|
2011-09-20 01:16:31 -04:00
|
|
|
@trim_requested += 1
|
2015-01-20 07:20:39 -05:00
|
|
|
@not_empty.signal
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-19 10:14:30 -04:00
|
|
|
# If there are dead threads in the pool make them go away while decreasing
|
2015-10-27 07:48:30 -04:00
|
|
|
# spawned counter so that new healthy threads could be created again.
|
2015-05-19 10:14:30 -04:00
|
|
|
def reap
|
|
|
|
@mutex.synchronize do
|
|
|
|
dead_workers = @workers.reject(&:alive?)
|
|
|
|
|
|
|
|
dead_workers.each do |worker|
|
|
|
|
worker.kill
|
|
|
|
@spawned -= 1
|
|
|
|
end
|
|
|
|
|
2016-09-08 13:01:49 -04:00
|
|
|
@workers.delete_if do |w|
|
|
|
|
dead_workers.include?(w)
|
|
|
|
end
|
2015-05-19 10:14:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-05 13:07:01 -05:00
|
|
|
class AutoTrim
|
|
|
|
def initialize(pool, timeout)
|
|
|
|
@pool = pool
|
|
|
|
@timeout = timeout
|
|
|
|
@running = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def start!
|
|
|
|
@running = true
|
|
|
|
|
|
|
|
@thread = Thread.new do
|
|
|
|
while @running
|
|
|
|
@pool.trim
|
|
|
|
sleep @timeout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
@running = false
|
|
|
|
@thread.wakeup
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-05 19:41:46 -04:00
|
|
|
def auto_trim!(timeout=30)
|
2011-12-05 13:07:01 -05:00
|
|
|
@auto_trim = AutoTrim.new(self, timeout)
|
|
|
|
@auto_trim.start!
|
|
|
|
end
|
|
|
|
|
2015-05-19 10:14:30 -04:00
|
|
|
class Reaper
|
|
|
|
def initialize(pool, timeout)
|
|
|
|
@pool = pool
|
|
|
|
@timeout = timeout
|
|
|
|
@running = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def start!
|
|
|
|
@running = true
|
|
|
|
|
|
|
|
@thread = Thread.new do
|
|
|
|
while @running
|
|
|
|
@pool.reap
|
|
|
|
sleep @timeout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
@running = false
|
|
|
|
@thread.wakeup
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_reap!(timeout=5)
|
|
|
|
@reaper = Reaper.new(self, timeout)
|
|
|
|
@reaper.start!
|
|
|
|
end
|
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# Tell all threads in the pool to exit and wait for them to finish.
|
|
|
|
#
|
2016-04-07 13:25:10 -04:00
|
|
|
def shutdown(timeout=-1)
|
2015-10-27 07:48:16 -04:00
|
|
|
threads = @mutex.synchronize do
|
2012-02-07 12:49:23 -05:00
|
|
|
@shutdown = true
|
2015-01-20 07:20:39 -05:00
|
|
|
@not_empty.broadcast
|
|
|
|
@not_full.broadcast
|
2011-12-05 13:07:01 -05:00
|
|
|
|
2012-02-07 12:49:23 -05:00
|
|
|
@auto_trim.stop if @auto_trim
|
2015-05-19 10:14:30 -04:00
|
|
|
@reaper.stop if @reaper
|
2015-10-27 07:48:16 -04:00
|
|
|
# dup workers so that we join them all safely
|
|
|
|
@workers.dup
|
2011-09-20 01:16:31 -04:00
|
|
|
end
|
|
|
|
|
2016-11-20 13:36:47 -05:00
|
|
|
if timeout == -1
|
|
|
|
# Wait for threads to finish without force shutdown.
|
2016-04-07 13:25:10 -04:00
|
|
|
threads.each(&:join)
|
|
|
|
else
|
2016-11-20 13:36:47 -05:00
|
|
|
# Wait for threads to finish after n attempts (+timeout+).
|
|
|
|
# If threads are still running, it will forcefully kill them.
|
2016-04-07 13:25:10 -04:00
|
|
|
timeout.times do
|
|
|
|
threads.delete_if do |t|
|
|
|
|
t.join 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if threads.empty?
|
|
|
|
break
|
|
|
|
else
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
threads.each do |t|
|
|
|
|
t.raise ForceShutdown
|
|
|
|
end
|
|
|
|
|
|
|
|
threads.each do |t|
|
2016-11-20 13:36:47 -05:00
|
|
|
t.join SHUTDOWN_GRACE_TIME
|
2016-04-07 13:25:10 -04:00
|
|
|
end
|
|
|
|
end
|
2011-09-20 01:16:31 -04:00
|
|
|
|
|
|
|
@spawned = 0
|
|
|
|
@workers = []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|