Struct Pool

Source
pub struct Pool<M: Manager> { /* private fields */ }

Implementations§

Source§

impl<M: Manager> Pool<M>

Source

pub async fn get(&self) -> Result<PoolRef<'_, M>, M::Error>

Return a PoolRef contains reference of SharedManagedPool<Manager> and an Option<Manager::Connection>.

The PoolRef should be dropped asap when you finish the use of it. Hold it in scope would prevent the connection from pushed back to pool.

Source

pub async fn get_owned(&self) -> Result<PoolRefOwned<M>, M::Error>

Return a PoolRefOwned contains a weak smart pointer of SharedManagedPool<Manager> and an Option<Manager::Connection>.

You can move PoolRefOwned to async blocks and across await point. But the performance is considerably worse than Pool::get.

Source

pub async fn run<'a, T, E, F, FF>(&'a self, f: F) -> Result<T, E>
where F: FnOnce(PoolRef<'a, M>) -> FF, FF: Future<Output = Result<T, E>> + Send + 'a, E: From<M::Error>, T: Send + 'static,

Run the pool with an async closure.

§example:
pool.run(|mut pool_ref| async {
    let connection = &mut *pool_ref;
    Ok(())
})
Source

pub fn pause(&self)

Pause the pool

these functionalities will stop:

  • get connection. Pool<Manager>::get() would eventually be timed out (If Manager::timeout is manually implemented with proper timeout function. *. Otherwise it will stuck forever in executor unless you cancel the future).
  • spawn of new connection.
  • default scheduled works (They would skip at least one iteration if the schedule time come across with the time period the pool is paused).
  • put back connection. (connection will be dropped instead.)
Source

pub fn resume(&self)

restart the pool.

Source

pub fn running(&self) -> bool

check if the pool is running.

Source

pub fn clear(&self)

Clear the pool.

All pending connections will also be destroyed.

Spawned count is not reset to 0 so new connections can’t fill the pool until all outgoing PoolRef are dropped.

All PoolRef' and 'PoolRefOwned outside of pool before the clear happen would be destroyed when trying to return it’s connection to pool.

Source

pub async fn init(&self) -> Result<(), M::Error>

manually initialize pool. this is usually called when the Pool is built with build_uninitialized This is useful when you want to make an empty Pool and initialize it later.

§example:
#[macro_use]
extern crate lazy_static;

use tokio_postgres_tang::{Pool, PostgresManager, Builder};
use tokio_postgres::NoTls;

lazy_static! {
   static ref POOL: Pool<PostgresManager<NoTls>> = Builder::new()
        .always_check(false)
        .idle_timeout(None)
        .max_lifetime(None)
        .min_idle(24)
        .max_size(24)
        .build_uninitialized(
            PostgresManager::new_from_stringlike("postgres://postgres:123@localhost/test", NoTls)
                .expect("can't make postgres manager")
        );
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    POOL.init().await.expect("Failed to initialize postgres pool");
    Ok(())
}
Source

pub fn set_max_size(&self, size: usize)

Change the max size of pool. This operation could result in some reallocation of PoolInner and impact the performance. (Pool<Manager>::clear() will recalibrate the pool with a capacity of current max/min pool size)

No actual check is used for new max_size. Be ware not to pass a size smaller than min_idle.

Source

pub fn set_min_idle(&self, size: usize)

Change the min idle size of pool. (Pool<Manager>::clear() will recalibrate the pool with a capacity of current max/min pool size)

No actual check is used for new min_idle. Be ware not to pass a size bigger than max_size.

Source

pub fn get_manager(&self) -> &M

expose Manager to public

Source

pub fn state(&self) -> State

Return a state of the pool inner. This call will block the thread and wait for lock.

Trait Implementations§

Source§

impl<M: Manager> Clone for Pool<M>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Manager> Debug for Pool<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M: Manager> Drop for Pool<M>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<M> Freeze for Pool<M>

§

impl<M> !RefUnwindSafe for Pool<M>

§

impl<M> Send for Pool<M>

§

impl<M> Sync for Pool<M>

§

impl<M> Unpin for Pool<M>

§

impl<M> !UnwindSafe for Pool<M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.