I'm running into issues with Sidekiq workers.
ActiveRecord::ConnectionTimeoutError: could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)
I am following recommendations about using ActiveRecord::ConnectionTimeoutError
and a suitably large connection pool.
I want to find out if I'm exhausting the connection pool. I'm logging size
and connections.length
from ActiveRecord::Base.connection_pool
, but they stay at a constant size = 100 connections.length = 5. Which suggests that this isn't a resource leak issue.
My MySQL server is configured to allow up to 400 concurrent connections.
My Job ended up looking like this:
class MyJob < ActiveJob::Base
queue_as :default
rescue_from StandardError do |exception|
# clear connections on exception. Not sure if this is a good idea or not.
ActiveRecord::Base.clear_active_connections!
end
def perform()
logger.info "size"
logger.info ActiveRecord::Base.connection_pool.instance_eval { @size }
logger.info "connections"
logger.info ActiveRecord::Base.connection_pool.instance_eval { @connections }.length
# Ensure connections come from connection pool.
ActiveRecord::Base.connection_pool.with_connection do |conn|
# do stuff
end
end
end
Is this the right way to diagnose what's causing this, whether it's resource starvation or leakage? Are there other techniques I can use to work out why this is happening?