[][src]Struct tang_rs::Pool

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

Implementations

impl<M: Manager> Pool<M>[src]

#[must_use = "futures do nothing unless you `.await` or poll them"]pub async fn get<'_, '_>(&'_ self) -> Result<PoolRef<'_, M>, M::Error>[src]

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.

#[must_use = "futures do nothing unless you `.await` or poll them"]pub async fn get_owned<'_>(&'_ self) -> Result<PoolRefOwned<M>, M::Error>[src]

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.

#[must_use = "futures do nothing unless you `.await` or poll them"]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, 
[src]

Run the pool with an async closure.

example:

This example is not tested
pool.run(|mut pool_ref| async {
    let connection = &mut *pool_ref;
    Ok(())
})

pub fn pause(&self)[src]

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.)

pub fn resume(&self)[src]

restart the pool.

pub fn running(&self) -> bool[src]

check if the pool is running.

pub fn clear(&self)[src]

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.

#[must_use = "futures do nothing unless you `.await` or poll them"]pub async fn init<'_>(&'_ self) -> Result<(), M::Error>[src]

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:

This example is not tested
#[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(())
}

pub fn set_max_size(&self, size: usize)[src]

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.

pub fn set_min_idle(&self, size: usize)[src]

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.

pub fn get_manager(&self) -> &M[src]

expose Manager to public

pub fn state(&self) -> State[src]

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

Trait Implementations

impl<M: Manager> Clone for Pool<M>[src]

impl<M: Manager> Debug for Pool<M>[src]

impl<M: Manager> Drop for Pool<M>[src]

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.