class Puma::ThreadPool
Internal Docs for A simple thread pool management object.
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 processes it.
Constants
- SHUTDOWN_GRACE_TIME
How long, after raising the
ForceShutdownof 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.
Attributes
Public Class Methods
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.
# File lib/puma/thread_pool.rb, line 40 def initialize(name, options = {}, server: nil, &block) @server = server @not_empty = ConditionVariable.new @not_full = ConditionVariable.new @mutex = Mutex.new @todo = Queue.new @backlog_max = 0 @spawned = 0 @waiting = 0 @name = name @min = Integer(options[:min_threads]) @max = Integer(options[:max_threads]) # Not an 'exposed' option, options[:pool_shutdown_grace_time] is used in CI # to shorten @shutdown_grace_time from SHUTDOWN_GRACE_TIME. Parallel CI # makes stubbing constants difficult. @shutdown_grace_time = Float(options[:pool_shutdown_grace_time] || SHUTDOWN_GRACE_TIME) @block = block @out_of_band = options[:out_of_band] @out_of_band_running = false @out_of_band_condvar = ConditionVariable.new @before_thread_start = options[:before_thread_start] @before_thread_exit = options[:before_thread_exit] @reaping_time = options[:reaping_time] @auto_trim_time = options[:auto_trim_time] @shutdown = false @trim_requested = 0 @out_of_band_pending = false @workers = [] @auto_trim = nil @reaper = nil @mutex.synchronize do @min.times do spawn_thread @not_full.wait(@mutex) end end @force_shutdown = false @shutdown_mutex = Mutex.new end
Public Instance Methods
How many objects have yet to be processed by the pool?
# File lib/puma/thread_pool.rb, line 112 def backlog with_mutex { @todo.size } end
The maximum size of the backlog
# File lib/puma/thread_pool.rb, line 118 def backlog_max with_mutex { @backlog_max } end
@!attribute [r] busy_threads @version 5.0.0
# File lib/puma/thread_pool.rb, line 129 def busy_threads with_mutex { @spawned - @waiting + @todo.size } end
@!attribute [r] pool_capacity
# File lib/puma/thread_pool.rb, line 123 def pool_capacity waiting + (@max - spawned) end
# File lib/puma/thread_pool.rb, line 106 def reset_max with_mutex { @backlog_max = 0 } end
generate stats hash so as not to perform multiple locks @return [Hash] hash containing stat info from ThreadPool
# File lib/puma/thread_pool.rb, line 93 def stats with_mutex do temp = @backlog_max @backlog_max = 0 { backlog: @todo.size, running: @spawned, pool_capacity: @waiting + (@max - @spawned), busy_threads: @spawned - @waiting + @todo.size, backlog_max: temp } end end