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

bb8: move State into api module #200

Merged
merged 1 commit into from
May 31, 2024
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
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
17 changes: 5 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,11 @@ where

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

pub(crate) fn state(&self) -> State {
#[allow(clippy::from_over_into)] // Keep this more private with the internal type
impl<M: ManageConnection> Into<State> for &PoolInternals<M> {
fn into(self) -> State {
State {
connections: self.num_conns,
idle_connections: self.conns.len() as u32,
Expand Down Expand Up @@ -245,13 +248,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,
}
Loading