One would expect the before and after checkout callback would wrap checkout or the undlying
code that creates a new adapter and assigns it to a pool or assigns an existing one from the pool.
The before and after checkout callback actually wraps verify.
Note, the behavior is the same in 5.1.x, 5.2.x, 6.0.x, 6.1.x but the code snippets come from: https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
Therefore, there is no way to instrument diagnostic code BEFORE the actual checkout occurs.
def checkout(checkout_timeout = @checkout_timeout)
checkout_and_verify(acquire_connection(checkout_timeout))
enddef checkout_and_verify(c)
c._run_checkout_callbacks do
c.verify!
end
c
rescue
remove c
c.disconnect!
raise
enddef acquire_connection(checkout_timeout)
# NOTE: we rely on <tt>@available.poll</tt> and +try_to_checkout_new_connection+ to
# +conn.lease+ the returned connection (and to do this in a +synchronized+
# section). This is not the cleanest implementation, as ideally we would
# <tt>synchronize { conn.lease }</tt> in this method, but by leaving it to <tt>@available.poll</tt>
# and +try_to_checkout_new_connection+ we can piggyback on +synchronize+ sections
# of the said methods and avoid an additional +synchronize+ overhead.
if conn = @available.poll || try_to_checkout_new_connection
conn
else
reap
@available.poll(checkout_timeout)
end
end
def try_to_checkout_new_connection
# first in synchronized section check if establishing new conns is allowed
# and increment @now_connecting, to prevent overstepping this pool's @size
# constraint
do_checkout = synchronize do
if @threads_blocking_new_connections.zero? && (@connections.size + @now_connecting) < @size
@now_connecting += 1
end
end
if do_checkout
begin
# if successfully incremented @now_connecting establish new connection
# outside of synchronized section
conn = checkout_new_connection
ensure
synchronize do
if conn
adopt_connection(conn)
# returned conn needs to be already leased
conn.lease
end
@now_connecting -= 1
end
end
end
end