sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.51k stars 82 forks source link

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

Open atuk721 opened 3 years ago

atuk721 commented 3 years ago

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.

bkontur commented 3 years ago

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

bkontur commented 3 years ago

@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

thomasmost commented 2 years ago

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)

thomasmost commented 2 years ago

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