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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
r? @schneems (@rails-bot has picked a reviewer for you, use r? to override) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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%.