Skip to content

Commit

Permalink
Merge pull request #249 from alexrudy/feature/client/pool-token-impro…
Browse files Browse the repository at this point in the history
…vements

Use the token to indicate whether a connection is re-used.
  • Loading branch information
alexrudy authored Dec 29, 2024
2 parents f390e40 + c28808c commit 93cbb91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/client/pool/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,26 +688,26 @@ where
pool.push(token, reused, poolref.clone());
return Pooled {
connection: Some(connection),
is_reused: true,
token,
token: Token::zero(),
pool: PoolRef::none(),
};
} else {
return Pooled {
connection: Some(connection),
is_reused: false,
token,
pool: poolref.clone(),
};
}
}

// No pool or lock was available, so we can't add the connection to the pool.
//
// Returning the original poolref + token means that if this was temporary,
// and we can grab the pool later, we will do so.
Pooled {
connection: Some(connection),
is_reused: false,
token,
pool: PoolRef::none(),
pool: poolref.clone(),
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/client/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub enum UriError {
#[derive(Debug)]
pub(crate) struct Pool<C: PoolableConnection, K: Key> {
inner: Arc<Mutex<PoolInner<C>>>,

// NOTE: The token map is stored on the Pool, not the PoolInner so that the generic K argument doesn't
// propogate into the pool inner and reference, only the connection type propogates.
keys: Arc<Mutex<TokenMap<K>>>,
}

Expand Down Expand Up @@ -330,8 +333,7 @@ where
trace!("re-usable connection will be sent to waiter");
let pooled = Pooled {
connection: Some(conn),
is_reused: true,
token,
token: Token::zero(),
pool: pool_ref.clone(),
};

Expand All @@ -346,7 +348,6 @@ where
);
let pooled = Pooled {
connection: Some(connection),
is_reused: false,
token,
pool: pool_ref.clone(),
};
Expand Down Expand Up @@ -461,7 +462,6 @@ where
C: PoolableConnection,
{
connection: Option<C>,
is_reused: bool,
token: Token,
pool: PoolRef<C>,
}
Expand All @@ -473,6 +473,12 @@ where
fn take(mut self) -> Option<C> {
self.connection.take()
}

/// Checks if this connection is being re-used
/// by the pool (implying that it is multiplexed)
pub fn is_reused(&self) -> bool {
self.token.is_zero()
}
}

impl<C> fmt::Debug for Pooled<C>
Expand Down Expand Up @@ -514,7 +520,7 @@ where
{
fn drop(&mut self) {
if let Some(connection) = self.connection.take() {
if connection.is_open() && !self.is_reused {
if connection.is_open() && !self.token.is_zero() {
if let Some(mut pool) = self.pool.lock() {
trace!("open connection returned to pool");
pool.push(self.token, connection, self.pool.clone());
Expand Down

0 comments on commit 93cbb91

Please sign in to comment.