Skip to content

Commit

Permalink
Fix #167: Notify waiters when dropping a bad connection from the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
xortive committed Mar 27, 2024
1 parent 8de2610 commit 5805866
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions bb8/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ where
(_, _) => {
let approvals = locked.dropped(1, &self.inner.statics);
self.spawn_replenishing_approvals(approvals);
self.inner.notify.notify_waiters();
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions bb8/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,3 +831,58 @@ async fn test_customize_connection_acquire() {
let connection_1_or_2 = pool.get().await.unwrap();
assert!(connection_1_or_2.custom_field == 1 || connection_1_or_2.custom_field == 2);
}

#[tokio::test]
async fn test_broken_connections_dont_starve_pool() {
use std::sync::RwLock;
use std::{convert::Infallible, time::Duration};

#[derive(Default)]
struct ConnectionManager {
counter: RwLock<u16>,
}
#[derive(Debug)]
struct Connection;

#[async_trait::async_trait]
impl bb8::ManageConnection for ConnectionManager {
type Connection = Connection;
type Error = Infallible;

async fn connect(&self) -> Result<Self::Connection, Self::Error> {
Ok(Connection)
}

async fn is_valid(&self, _: &mut Self::Connection) -> Result<(), Self::Error> {
Ok(())
}

fn has_broken(&self, _: &mut Self::Connection) -> bool {
let mut counter = self.counter.write().unwrap();
let res = *counter < 5;
*counter += 1;
res
}
}

let pool = bb8::Pool::builder()
.max_size(5)
.connection_timeout(Duration::from_secs(10))
.build(ConnectionManager::default())
.await
.unwrap();

let mut futures = Vec::new();

for _ in 0..10 {
let pool = pool.clone();
futures.push(tokio::spawn(async move {
let conn = pool.get().await.unwrap();
drop(conn);
}));
}

for future in futures {
future.await.unwrap();
}
}

0 comments on commit 5805866

Please sign in to comment.