sfackler/r2d2

Is there a way to drop or close an actual connection when I'm using PooldConnection?

atuk721 opened this issue · 4 comments

Because when I work with the connection of PooldConnection got by Pool.get and face some error that a protocol of the connection became bad state, I'd liket to close the actual connection.

I don't want to use test_on_checkout because it's high impact for performance.

I had similar issue, I hacked it like this:

implement has_broken with boolean holder

fn has_broken(&self, conn: &mut MyConnection) -> bool {
    if self.release_on_return_to_pool.get() {
            return true;
    }
...
pub struct MyConnection {
...
    /// Indicates that we want to release this connection on return to pool (used for gracefull shutdown)
    release_on_return_to_pool: Cell<bool>,
...
impl ProtocolRunnerConnection {
...
    /// Mark connection as "destroy" when return back to pool
    pub fn set_release_on_return_to_pool(&self) {
        self.release_on_return_to_pool.set(true);
    }
...
}

so when I have aquired connection and some error occures, I can decide, if to return connection to pool or to destroy it by calling set_release_on_return_to_pool

@sfackler
if this solution is ok, I could maybe do pull request to your library,
btw. r2d2 is very cool and works perfectly for me

This seems like quite the hack. Is there really no way of closing or returning a connection to the pool explicitly?

I'd expect something like
connection.close() for example— or pool.reclaim(connection)

FWIW I found one source that says connections are returned to the pool as soon as they go out of scope