Struct redis_tang::Pool
source · [−]pub struct Pool<M> where
M: Manager, { /* private fields */ }
Implementations
sourceimpl<M> Pool<M> where
M: Manager,
impl<M> Pool<M> where
M: Manager,
sourcepub async fn get(&'_ self) -> Result<PoolRef<'_, M>, <M as Manager>::Error>
pub async fn get(&'_ self) -> Result<PoolRef<'_, M>, <M as Manager>::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.
sourcepub async fn get_owned(
&'_ self
) -> Result<PoolRefOwned<M>, <M as Manager>::Error>
pub async fn get_owned(
&'_ self
) -> Result<PoolRefOwned<M>, <M as Manager>::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
.
sourcepub async fn run<'a, T, E, F, FF>(&'a self, f: F) -> Result<T, E> where
F: FnOnce(PoolRef<'a, M>) -> FF,
FF: 'a + Future<Output = Result<T, E>> + Send,
E: From<<M as Manager>::Error>,
T: 'static + Send,
pub async fn run<'a, T, E, F, FF>(&'a self, f: F) -> Result<T, E> where
F: FnOnce(PoolRef<'a, M>) -> FF,
FF: 'a + Future<Output = Result<T, E>> + Send,
E: From<<M as Manager>::Error>,
T: 'static + Send,
Run the pool with an async closure.
example:
pool.run(|mut pool_ref| async {
let connection = &mut *pool_ref;
Ok(())
})
sourcepub fn pause(&self)
pub fn pause(&self)
Pause the pool
these functionalities will stop:
- get connection.
Pool<Manager>::get()
would eventually be timed out (IfManager::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.)
sourcepub fn clear(&self)
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.
sourcepub async fn init(&'_ self) -> Result<(), <M as Manager>::Error>
pub async fn init(&'_ self) -> Result<(), <M as Manager>::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(())
}
sourcepub fn set_max_size(&self, size: usize)
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
.
sourcepub fn set_min_idle(&self, size: usize)
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
.
sourcepub fn get_manager(&self) -> &M
pub fn get_manager(&self) -> &M
expose Manager
to public
Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more