feat: add optional clearing of ActiveRecord connections after each job#622
feat: add optional clearing of ActiveRecord connections after each job#622rafael-pissardo wants to merge 8 commits into
Conversation
8ec97b0 to
407f3e0
Compare
|
Thanks @rafael-pissardo! I know some of our tests are flaky, but this one seems to be a real failure. Please take a look. |
a1fb62c to
2e1fc3f
Compare
|
@p-schlickmann thanks for your comment. Specs fixed. 👍 |
|
@rosa WDYT? |
|
Hey @rafael-pissardo, thanks for working on this. I haven't had time to review Solid Queue PRs in a while, or work on some features myself, but I'm hoping I'll have a block of a few days dedicated just to this in a couple of weeks. I'll look into it then 🤞🏻 |
|
@rafael-pissardo I have a question: is it possible to do this with an |
|
@rosa Good question! An app could clear connections via Active Job callbacks, but it wouldn't achieve the same effect here for two reasons:
That's why the clear sits in the Pool's Happy to adjust the approach if you'd prefer something different! |
Summary
This pull request introduces an optional mechanism to release Active Record database connections at the end of every job execution processed by Solid Queue workers.
SolidQueue.clear_connections_after_job(default: false).ActiveRecord::Base.clear_active_connections!in theensureblock of each thread after a job completes.Motivation
Under normal operation, each worker thread keeps its Active Record connection open for the lifetime of the process.
In environments with:
these unused connections can account for a significant share of RSS:
By explicitly clearing idle connections, memory that would otherwise remain tied up in driver buffers and Ruby objects can be reclaimed by the garbage collector and, on modern Ruby/glibc, returned to the OS.
Expected Impact
false).Trade-offs & Considerations
How to Enable
Testing
PoolConnectionClearTestto assert thatActiveRecord::Base.clear_active_connections!is invoked when the flag is enabled.PoolConnectionClearToggleTestto ensure it is not invoked when the flag is disabled.