Skip to content

Commit

Permalink
bb8: move State into api module
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed May 31, 2024
1 parent 62597aa commit 9fa2061
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
21 changes: 15 additions & 6 deletions bb8/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use async_trait::async_trait;

use crate::inner::PoolInner;
use crate::internals::Conn;
pub use crate::internals::State;

/// A generic connection pool.
pub struct Pool<M>
Expand Down Expand Up @@ -45,11 +44,6 @@ impl<M: ManageConnection> Pool<M> {
Builder::new()
}

/// Returns information about the current state of the pool.
pub fn state(&self) -> State {
self.inner.state()
}

/// Retrieves a connection from the pool.
pub async fn get(&self) -> Result<PooledConnection<'_, M>, RunError<M::Error>> {
self.inner.get().await
Expand All @@ -76,6 +70,21 @@ impl<M: ManageConnection> Pool<M> {
pub async fn dedicated_connection(&self) -> Result<M::Connection, M::Error> {
self.inner.connect().await
}

/// Returns information about the current state of the pool.
pub fn state(&self) -> State {
self.inner.state()
}
}

/// Information about the state of a `Pool`.
#[derive(Debug)]
#[non_exhaustive]
pub struct State {
/// The number of connections currently being managed by the pool.
pub connections: u32,
/// The number of idle connections.
pub idle_connections: u32,
}

/// A builder for a connection pool.
Expand Down
6 changes: 3 additions & 3 deletions bb8/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use futures_util::TryFutureExt;
use tokio::spawn;
use tokio::time::{interval_at, sleep, timeout, Interval};

use crate::api::{Builder, ConnectionState, ManageConnection, PooledConnection, RunError};
use crate::internals::{Approval, ApprovalIter, Conn, SharedPool, State};
use crate::api::{Builder, ConnectionState, ManageConnection, PooledConnection, RunError, State};
use crate::internals::{Approval, ApprovalIter, Conn, SharedPool};

pub(crate) struct PoolInner<M>
where
Expand Down Expand Up @@ -148,7 +148,7 @@ where

/// Returns information about the current state of the pool.
pub(crate) fn state(&self) -> State {
self.inner.internals.lock().state()
(&*self.inner.internals.lock()).into()
}

// Outside of Pool to avoid borrow splitting issues on self
Expand Down
16 changes: 4 additions & 12 deletions bb8/src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Instant;
use crate::{api::QueueStrategy, lock::Mutex};
use tokio::sync::Notify;

use crate::api::{Builder, ManageConnection};
use crate::api::{Builder, ManageConnection, State};
use std::collections::VecDeque;

/// The guts of a `Pool`.
Expand Down Expand Up @@ -153,8 +153,10 @@ where

self.dropped((before - self.conns.len()) as u32, config)
}
}

pub(crate) fn state(&self) -> State {
impl<M: ManageConnection> Into<State> for &PoolInternals<M> {

Check failure on line 158 in bb8/src/internals.rs

View workflow job for this annotation

GitHub Actions / lint

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> State {
State {
connections: self.num_conns,
idle_connections: self.conns.len() as u32,
Expand Down Expand Up @@ -245,13 +247,3 @@ impl<C: Send> From<Conn<C>> for IdleConn<C> {
}
}
}

/// Information about the state of a `Pool`.
#[derive(Debug)]
#[non_exhaustive]
pub struct State {
/// The number of connections currently being managed by the pool.
pub connections: u32,
/// The number of idle connections.
pub idle_connections: u32,
}

0 comments on commit 9fa2061

Please sign in to comment.