Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 21 additions & 3 deletions bb8/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,17 @@ pub struct State {
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct Statistics {
/// Total gets performed that did not have to wait for a connection.
/// Total gets started.
///
/// This counter is incremented before the `get` operation starts waiting,
/// so that it's possible to monitor the size of the queue by computing the
/// difference with the other `get_*` statistics.
pub get_started: u64,
/// Total gets completed that did not have to wait for a connection.
pub get_direct: u64,
/// Total gets performed that had to wait for a connection available.
/// Total gets completed that had to wait for a connection available.
pub get_waited: u64,
/// Total gets performed that timed out while waiting for a connection.
/// Total gets completed that timed out while waiting for a connection.
pub get_timed_out: u64,
/// Total time accumulated waiting for a connection.
pub get_wait_time: Duration,
Expand All @@ -123,6 +129,18 @@ pub struct Statistics {
pub connections_closed_idle_timeout: u64,
}

impl Statistics {
/// Total pending gets waiting for a connection.
pub fn pending_gets(&self) -> u64 {
self.get_started - self.completed_gets()
}

/// Total gets completed.
pub fn completed_gets(&self) -> u64 {
self.get_direct + self.get_waited + self.get_timed_out
}
}

/// A builder for a connection pool.
#[derive(Debug)]
pub struct Builder<M: ManageConnection> {
Expand Down
2 changes: 2 additions & 0 deletions bb8/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ where
}
};

self.inner.statistics.record_get_started();

let result = match timeout(self.inner.statics.connection_timeout, future).await {
Ok(result) => result,
_ => {
Expand Down
6 changes: 6 additions & 0 deletions bb8/src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ impl<C: Send> From<Conn<C>> for IdleConn<C> {

#[derive(Default)]
pub(crate) struct AtomicStatistics {
pub(crate) get_started: AtomicU64,
pub(crate) get_direct: AtomicU64,
pub(crate) get_waited: AtomicU64,
pub(crate) get_timed_out: AtomicU64,
Expand All @@ -332,6 +333,10 @@ pub(crate) struct AtomicStatistics {
}

impl AtomicStatistics {
pub(crate) fn record_get_started(&self) {
self.get_started.fetch_add(1, Ordering::Relaxed);
}

pub(crate) fn record_get(&self, kind: StatsGetKind, wait_time_start: Option<Instant>) {
match kind {
StatsGetKind::Direct => self.get_direct.fetch_add(1, Ordering::Relaxed),
Expand Down Expand Up @@ -370,6 +375,7 @@ impl AtomicStatistics {
impl From<&AtomicStatistics> for Statistics {
fn from(item: &AtomicStatistics) -> Self {
Self {
get_started: item.get_started.load(Ordering::Relaxed),
get_direct: item.get_direct.load(Ordering::Relaxed),
get_waited: item.get_waited.load(Ordering::Relaxed),
get_timed_out: item.get_timed_out.load(Ordering::Relaxed),
Expand Down