Skip to content

Commit

Permalink
Add new get wait time statistic
Browse files Browse the repository at this point in the history
The new field `get_waited_time` for the `Statistics` type allows
users to know the accumulated time for successive calls to the `get`
method while waiting for a free connection.
  • Loading branch information
pfreixes committed Jun 10, 2024
1 parent 276235a commit 647d015
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bb8/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct Statistics {
pub get_waited: u64,
/// Total gets performed that timed out while waiting for a connection.
pub get_timed_out: u64,
/// Total time accumulated waiting for a connection.
pub get_waited_time: Duration,
}

/// A builder for a connection pool.
Expand Down
6 changes: 6 additions & 0 deletions bb8/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ where

pub(crate) async fn get(&self) -> Result<PooledConnection<'_, M>, RunError<M::Error>> {
let mut kind = StatsKind::Direct;
let mut wait_time_start = None;

let future = async {
loop {
Expand All @@ -98,6 +99,7 @@ where
let mut conn = match conn {
Some(conn) => PooledConnection::new(self, conn),
None => {
wait_time_start = Some(Instant::now());
kind = StatsKind::Waited;
self.inner.notify.notified().await;
continue;
Expand Down Expand Up @@ -128,6 +130,10 @@ where
};

self.inner.statistics.record(kind);
if let Some(wait_time_start) = wait_time_start {
let wait_time = Instant::now() - wait_time_start;
self.inner.statistics.record_get(wait_time);
}
result
}

Expand Down
11 changes: 10 additions & 1 deletion bb8/src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::min;
use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;
use std::time::{Duration, Instant};

use tokio::sync::Notify;

Expand Down Expand Up @@ -255,6 +255,7 @@ pub(crate) struct AtomicStatistics {
pub(crate) get_direct: AtomicU64,
pub(crate) get_waited: AtomicU64,
pub(crate) get_timed_out: AtomicU64,
pub(crate) get_waited_time_micros: AtomicU64,
}

impl AtomicStatistics {
Expand All @@ -265,6 +266,11 @@ impl AtomicStatistics {
StatsKind::TimedOut => self.get_timed_out.fetch_add(1, Ordering::SeqCst),
};
}

pub(crate) fn record_get(&self, wait_time: Duration) {
self.get_waited_time_micros
.fetch_add(wait_time.as_micros() as u64, Ordering::SeqCst);
}
}

impl From<&AtomicStatistics> for Statistics {
Expand All @@ -273,6 +279,9 @@ impl From<&AtomicStatistics> for Statistics {
get_direct: item.get_direct.load(Ordering::SeqCst),
get_waited: item.get_waited.load(Ordering::SeqCst),
get_timed_out: item.get_timed_out.load(Ordering::SeqCst),
get_waited_time: Duration::from_micros(
item.get_waited_time_micros.load(Ordering::SeqCst),
),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions bb8/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,4 +924,5 @@ async fn test_state_get_contention() {
let statistics = pool.state().statistics;
assert_eq!(statistics.get_direct, 1);
assert_eq!(statistics.get_waited, 1);
assert!(statistics.get_waited_time > Duration::from_micros(0));
}

0 comments on commit 647d015

Please sign in to comment.