Skip to content

Commit

Permalink
Add connection pool to connection timeout errors
Browse files Browse the repository at this point in the history
Along the same lines as rails#48295,
in multi-database applications it's helpful to have access to the pool
when debugging these timeouts. For example, we might send the connection
class and role along to an exception tracking system.

ConnectionTimeout was already set up to take a connection_pool, so this
commit passes it along as needed where we raise the error.
  • Loading branch information
composerinteralia committed Nov 7, 2023
1 parent 488a7ce commit 846bf52
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def checkout_for_exclusive_access(checkout_timeout)

msg << " (#{thread_report.join(', ')})" if thread_report.any?

raise ExclusiveConnectionTimeoutError, msg
raise ExclusiveConnectionTimeoutError.new(msg, connection_pool: self)
end

def with_new_connections_blocked
Expand Down Expand Up @@ -670,6 +670,8 @@ def acquire_connection(checkout_timeout)
reap
@available.poll(checkout_timeout)
end
rescue ConnectionTimeoutError => ex
raise ex.set_pool(self)
end

#--
Expand Down
6 changes: 4 additions & 2 deletions activerecord/test/cases/connection_pool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ def test_full_pool_exception
@pool.checkout_timeout = 0.001 # no need to delay test suite by waiting the whole full default timeout
@pool.size.times { assert @pool.checkout }

assert_raises(ConnectionTimeoutError) do
error = assert_raises(ConnectionTimeoutError) do
@pool.checkout
end
assert_equal @pool, error.connection_pool
end

def test_full_pool_blocks
Expand Down Expand Up @@ -615,9 +616,10 @@ def test_non_bang_disconnect_and_clear_reloadable_connections_throw_exception_if
@pool.checkout_timeout = 0.001 # no need to delay test suite by waiting the whole full default timeout
[:disconnect, :clear_reloadable_connections].each do |group_action_method|
@pool.with_connection do |connection|
assert_raises(ExclusiveConnectionTimeoutError) do
error = assert_raises(ExclusiveConnectionTimeoutError) do
new_thread { @pool.public_send(group_action_method) }.join
end
assert_equal @pool, error.connection_pool
end
end
ensure
Expand Down

0 comments on commit 846bf52

Please sign in to comment.