/// Tries to instantly get a connection from the pool.
///
/// # Returns
///
/// An error if there is either a dead connection, there are no connections
/// in the pool or the healthcheck failed.
/// If successful if will return a [`ConnectionGuard`].
///
Tries to instantly get a connection from the pool.
Tries to instantly get a connection from the pool .
pub async fn try_acquire(&self) -> Result<ConnectionGuard, CachemError> {
        // Make sure that there is no dead connection
        if self.has_dead_con.load(Ordering::SeqCst) {
            log::error!("Dead connection");
            return Err(CachemError::ConnectionPoolError(ConnectionPoolError::NoConnectionAvailable));
        }
        // Before locking the connections mutex, check if there are connections
        // available, if not return an error
        if self.available.load(Ordering::SeqCst) == 0 {
            log::warn!("No connection available");
            return Err(CachemError::ConnectionPoolError(ConnectionPoolError::NoConnectionAvailable));
        }
        // Required, removing this will cause some problems regarding Send and await
        let con = { self.connections.lock().unwrap() }.pop_front();
        self.available.fetch_sub(1, Ordering::SeqCst);
        if let Some(mut con) = con {
            if con.is_healthy().await {
                Ok(ConnectionGuard::new(self.clone(), con))
            } else {
                // Connection is dead, set the flag
                self.has_dead_con.store(true, Ordering::Relaxed);
                Err(CachemError::ConnectionPoolError(ConnectionPoolError::CannotConnect))
            }
        } else {
            Err(CachemError::ConnectionPoolError(ConnectionPoolError::NoConnectionAvailable))
        }
    }
pub async fn try_acquire ( & self ) -> Result < ConnectionGuard , CachemError > { // Make sure that there is no dead connection if self . has_dead_con . load ( Ordering :: SeqCst ) { log :: error ! ( " " ) ; return Err ( CachemError :: ConnectionPoolError ( ConnectionPoolError :: NoConnectionAvailable ) ) ; } // Before locking the connections mutex, check if there are connections // available, if not return an error if self . available . load ( Ordering :: SeqCst ) == 0 { log :: warn ! ( " " ) ; return Err ( CachemError :: ConnectionPoolError ( ConnectionPoolError :: NoConnectionAvailable ) ) ; } // Required, removing this will cause some problems regarding Send and await let con = { self . connections . lock ( ) . unwrap ( ) } . pop_front ( ) ; self . available . fetch_sub ( 1 , Ordering :: SeqCst ) ; if let Some ( mut con ) = con { if con . is_healthy ( ) . await { Ok ( ConnectionGuard :: new ( self . clone ( ) , con ) ) } else { // Connection is dead, set the flag self . has_dead_con . store ( true , Ordering :: Relaxed ) ; Err ( CachemError :: ConnectionPoolError ( ConnectionPoolError :: CannotConnect ) ) } } else { Err ( CachemError :: ConnectionPoolError ( ConnectionPoolError :: NoConnectionAvailable ) ) } }
Tries to instantly get a connection from the pool.
Tries to instantly get a connection from the pool .
An error if there is either a dead connection, there are no connections
in the pool or the healthcheck failed.
If successful if will return a [`ConnectionGuard`].
An error if there is either a dead connection there are no connections in the pool or the healthcheck failed . If successful if will return a [ ` ConnectionGuard ` ] .
&self