Add reap functionality#187
Conversation
|
I’m wondering if the default idle_seconds value should be something more like 60. 0 is very aggressive. |
0 seconds is on the aggresive, setting to a more sane value of 60 seconds to prevent connection reaping/creation thrash.
Good call, 60 seconds is a more sane value such that this method can be called and not cause thrash of opening/closing connections. |
|
Did you document how the user can implement reaping? |
Updated the README with information on using I could also add a section on implementing a reaper thread 🤔... Is that what you are referring to or is what I added above enough. |
|
Yeah, I meant with a Thread. Your code example isn't quite realistic yet, I'd change it to put the reap call in a Thread. |
|
Sorry for the delay: I've added a new section about creating your own reaper thread to the README: |
|
This is great, nice work. I’ll play with it later and push out a new release in a few weeks. |
| conn, _last_checked_out = @que.shift | ||
| reap_block.call(conn) |
There was a problem hiding this comment.
Doesn't this also need to decrement @created?
|
Excited for this, thank you! |
|
2.5.0 is now out with reaping functionality. |
* Add idle and reap methods to connection pool * Change ConnectionPool#reap idle_seconds default value from 0 to 60 0 seconds is on the aggresive, setting to a more sane value of 60 seconds to prevent connection reaping/creation thrash. * Update README with information on new ConnectionPool methods #reap and #idle * add section to README about creating a reaper thread
There have been several discussions/attempts at adding a idle connection reaping functionality to this gem: #125 #127 #126
I and many others would find value in adding this functionality however I am in agreement with the previous discussions that the key to adding this
reapfunctionality is to add it in a way that keeps the simplicity of this gem.In order to accomplish this, I've added two new public methods to
ConnectionPool(as well as matching methods forTimedStack):#idle- Returns how many connections that are created and available in the pool. This is different thanavailablebecauseavailableincludes connections that haven't been created yet.#reap(idle_seconds = 0, &block)- Removes connections that have been idle for longer thanidle_secondsand yields each connection to the block passed to the method in order to close/clean up the connection. By default,idle_secondsis 0, which means#reapwill remove all connections that are on the stack.In order to implement the
#reapmethod in connection pool, I've changed how theTimedStackqueue stores connections in the queue to also include the current time when pushed back onto the stack.