Class: ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper
- Defined in:
- lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb
Overview
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`).
Instance Attribute Summary collapse
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Class Method Summary collapse
-
.pools(refs = nil) ⇒ Object
:nodoc:.
-
.register_pool(pool, frequency) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#initialize(pool, frequency) ⇒ Reaper
constructor
A new instance of Reaper.
- #run ⇒ Object
Constructor Details
#initialize(pool, frequency) ⇒ Reaper
Returns a new instance of Reaper.
36 37 38 39 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb', line 36 def initialize(pool, frequency) @pool = pool @frequency = frequency end |
Instance Attribute Details
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
34 35 36 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb', line 34 def frequency @frequency end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
34 35 36 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb', line 34 def pool @pool end |
Class Method Details
.pools(refs = nil) ⇒ Object
:nodoc:
56 57 58 59 60 61 62 63 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb', line 56 def pools(refs = nil) # :nodoc: refs ||= @mutex.synchronize { @pools.values.flatten(1) } refs.filter_map do |ref| ref.__getobj__ if ref.weakref_alive? rescue WeakRef::RefError end.select(&:maintainable?) end |
.register_pool(pool, frequency) ⇒ Object
:nodoc:
46 47 48 49 50 51 52 53 54 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb', line 46 def register_pool(pool, frequency) # :nodoc: @mutex.synchronize do unless @threads[frequency]&.alive? @threads[frequency] = spawn_thread(frequency) end @pools[frequency] ||= [] @pools[frequency] << WeakRef.new(pool) end end |
Instance Method Details
#run ⇒ Object
110 111 112 113 |
# File 'lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb', line 110 def run return unless frequency && frequency > 0 self.class.register_pool(pool, frequency) end |