Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new get wait time statistic #203

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
djc marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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);
}
djc marked this conversation as resolved.
Show resolved Hide resolved
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));
}
Loading