Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear active connections after initialization #28057

Merged
merged 1 commit into from Apr 3, 2017

Conversation

eugeneius
Copy link
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

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.

None yet

5 participants