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 proceses it.

Constants

SHUTDOWN_GRACE_TIME

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.

Attributes

clean_thread_locals[RW]
spawned[R]
trim_requested[R]
waiting[R]

Public Class Methods

clean_thread_locals() click to toggle source
# File lib/puma/thread_pool.rb, line 66
def self.clean_thread_locals
  Thread.current.keys.each do |key| # rubocop: disable Performance/HashEachMethods
    Thread.current[key] = nil unless key == :__recursive_key__
  end
end
new(min, max, *extra, &block) click to toggle source

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 32
def initialize(min, max, *extra, &block)
  @not_empty = ConditionVariable.new
  @not_full = ConditionVariable.new
  @mutex = Mutex.new

  @todo = []

  @spawned = 0
  @waiting = 0

  @min = Integer(min)
  @max = Integer(max)
  @block = block
  @extra = extra

  @shutdown = false

  @trim_requested = 0

  @workers = []

  @auto_trim = nil
  @reaper = nil

  @mutex.synchronize do
    @min.times { spawn_thread }
  end

  @clean_thread_locals = false
end

Public Instance Methods

backlog() click to toggle source

How many objects have yet to be processed by the pool?

# File lib/puma/thread_pool.rb, line 74
def backlog
  @mutex.synchronize { @todo.size }
end
pool_capacity() click to toggle source
# File lib/puma/thread_pool.rb, line 78
def pool_capacity
  waiting + (@max - spawned)
end