Skip to Content Skip to Search

class ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper

Active Record Connection Pool Reaper

The reaper is a singleton that exists in the background of the process and is responsible for general maintenance of all the connection pools.

It will reclaim connections that are leased to now-dead threads, ensuring that a bad thread can’t leak a pool slot forever. By definition, this involves touching currently-leased connections, but that is safe because the owning thread is known to be dead.

Beyond that, it manages the health of available / unleased connections:

* retiring connections that have been idle[1] for too long
* creating occasional activity on inactive[1] connections
* keeping the pool prepopulated up to its minimum size
* proactively connecting to the target database from any pooled
  connections that had lazily deferred that step
* resetting or replacing connections that are known to be broken

[1]: “idle” and “inactive” here distinguish between connections that have not been requested by the application in a while (idle) and those that have not spoken to their remote server in a while (inactive). The former is a desirable opportunity to reduce our connection count (‘idle_timeout`); the latter is a risk that the server or a firewall may drop a connection we still anticipate using (avoided by `keepalive`).

Attributes

[R] frequency
[R] pool

Public class methods

Source code GitHub
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb, line 36
def initialize(pool, frequency)
  @pool      = pool
  @frequency = frequency
end

Public instance methods

Source code GitHub
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb, line 94
def run
  return unless frequency && frequency > 0
  self.class.register_pool(pool, frequency)
end

Definition files