Skip to content

Clear active connections after initialization - #28057

Merged
matthewd merged 1 commit into
rails:masterfrom
eugeneius:clear_active_connections
Apr 3, 2017
Merged

matthewd merged 1 commit into
rails:masterfrom
eugeneius:clear_active_connections

Conversation

@eugeneius

Copy link
Copy Markdown
Member

Any connections that were checked out during initialization should be checked back in before the first request is processed, for two reasons:

  • Returning the connection to the pool allows it to be health checked before it's used again. If the connection dies before the first request arrives, the health check will replace it with a new one.

  • If the thread that initialized Rails is not the same thread that will be performing work, checking in the connection will allow it to be reused instead of being stuck to the initialization thread forever.

The second use case was the reason I wrote this patch - we run a lot of Sidekiq processes at @intercom, and deploying this reduced the peak open connection count on our database by 13%.

Any connections that were checked out during initialization should be
checked back in before the first request is processed, for two reasons:

 - Returning the connection to the pool allows it to be health checked
   before it's used again. If the connection dies before the first
   request arrives, the health check will replace it with a new one.

 - If the thread that initialized Rails is not the same thread that will
   be performing work, checking in the connection will allow it to be
   reused instead of being stuck to the initialization thread forever.
@rails-bot

Copy link
Copy Markdown

r? @schneems

(@rails-bot has picked a reviewer for you, use r? to override)

@matthewd
matthewd merged commit 3ecffab into rails:master Apr 3, 2017
mriddle added a commit to zendesk/active_record_host_pool that referenced this pull request Aug 21, 2019
Unless told otherwhise, when this proxy class intercepts methods, it will create
a connection if one does not already exist.

https://github.com/zendesk/active_record_host_pool/blob/v0.11.0/lib/active_record_host_pool/pool_proxy.rb#L107-L108

In Rails 5.2.0, they added the following functionality which caused issues with
code mentioned above
https://github.com/rails/rails/blob/v5.2.3/activerecord/lib/active_record/railtie.rb#L180-L195

Here's the associated PRs
rails/rails#28057
rails/rails#31221

This patch overrides the methods, retrieves the connection if it's active and
calls super without creating a new one.

This resolves an issue for applications upgrading to Rails 5.2.3 whereby
connections would be created and thrown away during Rails.application.initialize!
mriddle added a commit to zendesk/active_record_host_pool that referenced this pull request Aug 22, 2019
Unless told otherwhise, when this proxy class intercepts methods, it will create
a connection if one does not already exist.

https://github.com/zendesk/active_record_host_pool/blob/v0.11.0/lib/active_record_host_pool/pool_proxy.rb#L107-L108

In Rails 5.2.0, they added the following functionality which caused issues with
code mentioned above
https://github.com/rails/rails/blob/v5.2.3/activerecord/lib/active_record/railtie.rb#L180-L195

Here's the associated PRs
rails/rails#28057
rails/rails#31221

This patch overrides the methods, retrieves the connection if it's active and
calls super without creating a new one.

This resolves an issue for applications upgrading to Rails 5.2.3 whereby
connections would be created and thrown away during Rails.application.initialize!
mriddle added a commit to zendesk/active_record_host_pool that referenced this pull request Aug 22, 2019
Unless told otherwhise, when this proxy class intercepts methods, it will create
a connection if one does not already exist.

https://github.com/zendesk/active_record_host_pool/blob/v0.11.0/lib/active_record_host_pool/pool_proxy.rb#L107-L108

In Rails 5.2.0, they added the following functionality which caused issues with
code mentioned above
https://github.com/rails/rails/blob/v5.2.3/activerecord/lib/active_record/railtie.rb#L180-L195

Here's the associated PRs
rails/rails#28057
rails/rails#31221

This patch overrides the methods, retrieves the connection if it's active and
calls super without creating a new one.

This resolves an issue for applications upgrading to Rails 5.2.3 whereby
connections would be created and thrown away during Rails.application.initialize!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants