diff --git a/Cargo.lock b/Cargo.lock index aecb92ad0..38aab54ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1242,6 +1242,19 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" +[[package]] +name = "ease-off" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20e90ae5e739d99dc0406f9a4e2307a999625e2414d2ecc4fbb4ded8a3945f77" +dependencies = [ + "async-io", + "pin-project", + "rand", + "thiserror 1.0.69", + "tokio", +] + [[package]] name = "either" version = "1.15.0" @@ -3544,10 +3557,10 @@ dependencies = [ "chrono", "crc", "crossbeam-queue", + "ease-off", "either", "event-listener 5.4.0", "futures-core", - "futures-intrusive", "futures-io", "futures-util", "hashbrown 0.16.0", @@ -3560,6 +3573,7 @@ dependencies = [ "memchr", "native-tls", "percent-encoding", + "pin-project-lite", "rust_decimal", "rustls", "rustls-native-certs", diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index ff416ed2f..00b1a6406 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -289,13 +289,13 @@ pub trait DatabaseError: 'static + Send + Sync + StdError { /// For example, the Postgres driver overrides this to return `true` for the following error codes: /// /// * `53300 too_many_connections`: returned when the maximum connections are exceeded - /// on the server. Assumed to be the result of a temporary overcommit - /// (e.g. an extra application replica being spun up to replace one that is going down). + /// on the server. Assumed to be the result of a temporary overcommit + /// (e.g. an extra application replica being spun up to replace one that is going down). /// * This error being consistently logged or returned is a likely indicator of a misconfiguration; /// the sum of [`PoolOptions::max_connections`] for all replicas should not exceed /// the maximum connections allowed by the server. /// * `57P03 cannot_connect_now`: returned when the database server is still starting up - /// and the tcop component is not ready to accept connections yet. + /// and the tcop component is not ready to accept connections yet. fn is_retryable_connect_error(&self) -> bool { false } diff --git a/sqlx-core/src/pool/connect.rs b/sqlx-core/src/pool/connect.rs index 187dab929..f1f7ce7d4 100644 --- a/sqlx-core/src/pool/connect.rs +++ b/sqlx-core/src/pool/connect.rs @@ -1,21 +1,18 @@ use crate::connection::{ConnectOptions, Connection}; use crate::database::Database; -use crate::pool::connection::{Floating, Live}; +use crate::pool::connection::Floating; use crate::pool::inner::PoolInner; use crate::pool::PoolConnection; use crate::rt::JoinHandle; use crate::Error; use ease_off::EaseOff; -use event_listener::{Event, EventListener}; +use event_listener::Event; use std::fmt::{Display, Formatter}; use std::future::Future; -use std::pin::Pin; use std::ptr; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::{Arc, RwLock}; -use std::task::{Context, Poll}; -use std::time::{Duration, Instant}; -use tracing::Instrument; +use std::sync::Arc; +use std::time::Instant; use std::io; @@ -74,7 +71,7 @@ use std::io; /// `set_connect_options` and `get_connect_options` were removed in 0.9.0 because they complicated /// the pool internals. They can be reimplemented by capturing a mutex, or similar, in the callback. /// -/// This example uses Postgres and [`tokio::sync::Mutex`] but may be adapted to any driver +/// This example uses Postgres and [`tokio::sync::RwLock`] but may be adapted to any driver /// or `async-std`, respectively. /// /// ```rust,no_run @@ -197,11 +194,11 @@ pub trait PoolConnector: Send + Sync + 'static { /// /// * [`io::ErrorKind::ConnectionRefused`] /// * Database errors for which - /// [`is_retryable_connect_error`][crate::error::DatabaseError::is_retryable_connect_error] - /// returns `true`. + /// [`is_retryable_connect_error`][crate::error::DatabaseError::is_retryable_connect_error] + /// returns `true`. /// * [`Error::PoolConnector`] with `retryable: true`. - /// This error kind is not returned internally and is designed to allow this method to return - /// arbitrary error types not otherwise supported. + /// This error kind is not returned internally and is designed to allow this method to return + /// arbitrary error types not otherwise supported. /// /// Manual implementations of this method may also use the signature: /// ```rust,ignore @@ -363,7 +360,7 @@ impl ConnectionCounter { // Check that `self` can increase size first before we check the parent. let acquired = self.acquire_permit_self(pool).await; - if let Some(parent) = &pool.options.parent_pool { + if let Some(parent) = pool.parent() { let (_, permit) = parent.0.counter.acquire_permit_self(&parent.0).await; // consume the parent permit diff --git a/sqlx-core/src/pool/connection.rs b/sqlx-core/src/pool/connection.rs index f4d6e765c..76e4e24b0 100644 --- a/sqlx-core/src/pool/connection.rs +++ b/sqlx-core/src/pool/connection.rs @@ -4,8 +4,6 @@ use std::ops::{Deref, DerefMut}; use std::sync::Arc; use std::time::{Duration, Instant}; -use crate::sync::AsyncSemaphoreReleaser; - use crate::connection::Connection; use crate::database::Database; use crate::error::Error; diff --git a/sqlx-core/src/pool/inner.rs b/sqlx-core/src/pool/inner.rs index a700f9fa4..da9b0add3 100644 --- a/sqlx-core/src/pool/inner.rs +++ b/sqlx-core/src/pool/inner.rs @@ -91,7 +91,7 @@ impl PoolInner { } } - fn parent(&self) -> Option<&Pool> { + pub(super) fn parent(&self) -> Option<&Pool> { self.options.parent_pool.as_ref() } diff --git a/sqlx-core/src/pool/mod.rs b/sqlx-core/src/pool/mod.rs index 0caa1161c..978f101da 100644 --- a/sqlx-core/src/pool/mod.rs +++ b/sqlx-core/src/pool/mod.rs @@ -59,7 +59,6 @@ use std::future::Future; use std::pin::{pin, Pin}; use std::sync::Arc; use std::task::{ready, Context, Poll}; -use std::time::{Duration, Instant}; use event_listener::EventListener; use futures_core::FusedFuture; @@ -628,15 +627,6 @@ impl FusedFuture for CloseEvent { } } -/// get the time between the deadline and now and use that as our timeout -/// -/// returns `Error::PoolTimedOut` if the deadline is in the past -fn deadline_as_timeout(deadline: Instant) -> Result { - deadline - .checked_duration_since(Instant::now()) - .ok_or(Error::PoolTimedOut) -} - #[test] #[allow(dead_code)] fn assert_pool_traits() { diff --git a/sqlx-core/src/rt/mod.rs b/sqlx-core/src/rt/mod.rs index 1da096d93..0044139f5 100644 --- a/sqlx-core/src/rt/mod.rs +++ b/sqlx-core/src/rt/mod.rs @@ -71,7 +71,7 @@ pub async fn timeout_at(deadline: Instant, f: F) -> Result