fn acquire_or_create_connection(&self) -> Result<Connection> {
        let start_time = Instant::now();
        let mut handle = self.inner.wait_queue.wait_until_at_front()?;

        loop {
            // Try to get the most recent available connection.
            while let Some(conn) = self.inner.connections.write().unwrap().pop() {
                // Close the connection if it's stale.
                if conn.is_stale(self.inner.generation.load(Ordering::SeqCst)) {
                    self.close_connection(conn, ConnectionClosedReason::Stale);
                    continue;
                }

                // Close the connection if it's idle.
                if conn.is_idle(self.inner.max_idle_time) {
                    self.close_connection(conn, ConnectionClosedReason::Idle);
                    continue;
                }

                // Otherwise, return the connection.
                return Ok(conn);
            }

            // Create a new connection if the pool is under max size.
            if self.inner.total_connection_count.load(Ordering::SeqCst) < self.inner.max_pool_size {
                return Ok(self.create_connection(true)?);
            }

            // Check if the pool has a max timeout.
            if let Some(timeout) = self.inner.wait_queue_timeout {
                // Check how long since the checkout process began.
                let time_waiting = Instant::now().duration_since(start_time);

                // If the timeout has been reached, return an error.
                if time_waiting >= timeout {
                    self.emit_event(|handler| {
                        let event = ConnectionCheckoutFailedEvent {
                            address: self.inner.address.clone(),
                            reason: ConnectionCheckoutFailedReason::Timeout,
                        };

                        handler.handle_connection_checkout_failed_event(event);
                    });

                    return Err(ErrorKind::WaitQueueTimeoutError {
                        address: self.inner.address.clone(),
                    }
                    .into());
                }

                // Wait until the either the timeout has been reached or a connection is checked
                // into the pool.
                handle.wait_for_available_connection(Some(timeout - time_waiting))?;
            } else {
                // Wait until a connection has been returned to the pool.
                handle.wait_for_available_connection(None)?;
            }
        }
    }
/// Waits for the thread to reach the front of the wait queue, then attempts to check out a
/// connection.
Waits for the thread to reach the front of the wait queue, then attempts to check out a
connection.
Waits for the thread to reach the front of the wait queue then attempts to check out a connection .
fn acquire_or_create_connection(&self) -> Result<Connection> {
        let start_time = Instant::now();
        let mut handle = self.inner.wait_queue.wait_until_at_front()?;
        loop {
            // Try to get the most recent available connection.
            while let Some(conn) = self.inner.connections.write().unwrap().pop() {
                // Close the connection if it's stale.
                if conn.is_stale(self.inner.generation.load(Ordering::SeqCst)) {
                    self.close_connection(conn, ConnectionClosedReason::Stale);
                    continue;
                }
                // Close the connection if it's idle.
                if conn.is_idle(self.inner.max_idle_time) {
                    self.close_connection(conn, ConnectionClosedReason::Idle);
                    continue;
                }
                // Otherwise, return the connection.
                return Ok(conn);
            }
            // Create a new connection if the pool is under max size.
            if self.inner.total_connection_count.load(Ordering::SeqCst) < self.inner.max_pool_size {
                return Ok(self.create_connection(true)?);
            }
            // Check if the pool has a max timeout.
            if let Some(timeout) = self.inner.wait_queue_timeout {
                // Check how long since the checkout process began.
                let time_waiting = Instant::now().duration_since(start_time);
                // If the timeout has been reached, return an error.
                if time_waiting >= timeout {
                    self.emit_event(|handler| {
                        let event = ConnectionCheckoutFailedEvent {
                            address: self.inner.address.clone(),
                            reason: ConnectionCheckoutFailedReason::Timeout,
                        };
                        handler.handle_connection_checkout_failed_event(event);
                    });
                    return Err(ErrorKind::WaitQueueTimeoutError {
                        address: self.inner.address.clone(),
                    }
                    .into());
                }
                // Wait until the either the timeout has been reached or a connection is checked
                // into the pool.
                handle.wait_for_available_connection(Some(timeout - time_waiting))?;
            } else {
                // Wait until a connection has been returned to the pool.
                handle.wait_for_available_connection(None)?;
            }
        }
    }
fn acquire_or_create_connection ( & self ) -> Result < Connection > { let start_time = Instant :: now ( ) ; let mut handle = self . inner . wait_queue . wait_until_at_front ( ) ? ; loop { // Try to get the most recent available connection. while let Some ( conn ) = self . inner . connections . write ( ) . unwrap ( ) . pop ( ) { // Close the connection if it's stale. if conn . is_stale ( self . inner . generation . load ( Ordering :: SeqCst ) ) { self . close_connection ( conn , ConnectionClosedReason :: Stale ) ; continue ; } // Close the connection if it's idle. if conn . is_idle ( self . inner . max_idle_time ) { self . close_connection ( conn , ConnectionClosedReason :: Idle ) ; continue ; } // Otherwise, return the connection. return Ok ( conn ) ; } // Create a new connection if the pool is under max size. if self . inner . total_connection_count . load ( Ordering :: SeqCst ) < self . inner . max_pool_size { return Ok ( self . create_connection ( true ) ? ) ; } // Check if the pool has a max timeout. if let Some ( timeout ) = self . inner . wait_queue_timeout { // Check how long since the checkout process began. let time_waiting = Instant :: now ( ) . duration_since ( start_time ) ; // If the timeout has been reached, return an error. if time_waiting >= timeout { self . emit_event ( | handler | { let event = ConnectionCheckoutFailedEvent { address : self . inner . address . clone ( ) , reason : ConnectionCheckoutFailedReason :: Timeout , } ; handler . handle_connection_checkout_failed_event ( event ) ; } ) ; return Err ( ErrorKind :: WaitQueueTimeoutError { address : self . inner . address . clone ( ) , } . into ( ) ) ; } // Wait until the either the timeout has been reached or a connection is checked // into the pool. handle . wait_for_available_connection ( Some ( timeout - time_waiting ) ) ? ; } else { // Wait until a connection has been returned to the pool. handle . wait_for_available_connection ( None ) ? ; } } }
Waits for the thread to reach the front of the wait queue, then attempts to check out a
connection.
Waits for the thread to reach the front of the wait queue then attempts to check out a connection .
&self
&self