WIP pool changes

This commit is contained in:
Austin Bonander
2024-10-18 19:46:36 -07:00
parent 418bcbb80e
commit 8efb358593
13 changed files with 851 additions and 522 deletions

View File

@@ -186,7 +186,7 @@ impl DatabaseError for PgDatabaseError {
self
}
fn is_transient_in_connect_phase(&self) -> bool {
fn is_retryable_connect_error(&self) -> bool {
// https://www.postgresql.org/docs/current/errcodes-appendix.html
[
// too_many_connections

View File

@@ -134,6 +134,11 @@ impl PgConnectOptions {
self
}
/// Identical to [Self::host()], but through a mutable reference.
pub fn set_host(&mut self, host: &str) {
host.clone_into(&mut self.host);
}
/// Sets the port to connect to at the server host.
///
/// The default port for PostgreSQL is `5432`.
@@ -150,6 +155,12 @@ impl PgConnectOptions {
self
}
/// Identical to [`Self::port()`], but through a mutable reference.
pub fn set_port(&mut self, port: u16) -> &mut Self {
self.port = port;
self
}
/// Sets a custom path to a directory containing a unix domain socket,
/// switching the connection method from TCP to the corresponding socket.
///
@@ -176,6 +187,12 @@ impl PgConnectOptions {
self
}
/// Identical to [`Self::username()`], but through a mutable reference.
pub fn set_username(&mut self, username: &str) -> &mut Self {
username.clone_into(&mut self.username);
self
}
/// Sets the password to use if the server demands password authentication.
///
/// # Example
@@ -191,6 +208,12 @@ impl PgConnectOptions {
self
}
/// Identical to [`Self::password()`]. but through a mutable reference.
pub fn set_password(&mut self, password: &str) -> &mut Self {
self.password = Some(password.to_owned());
self
}
/// Sets the database name. Defaults to be the same as the user name.
///
/// # Example

View File

@@ -1,5 +1,4 @@
use std::future::Future;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::OnceLock;
use std::time::Duration;
@@ -101,27 +100,11 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<Postgres>, Error> {
.max_connections(20)
// Immediately close master connections. Tokio's I/O streams don't like hopping runtimes.
.after_release(|_conn, _| Box::pin(async move { Ok(false) }))
.connect_lazy_with(master_opts);
.connect_lazy_with(master_opts.clone());
let master_pool = match once_lock_try_insert_polyfill(&MASTER_POOL, pool) {
Ok(inserted) => inserted,
Err((existing, pool)) => {
// Sanity checks.
assert_eq!(
existing.connect_options().host,
pool.connect_options().host,
"DATABASE_URL changed at runtime, host differs"
);
assert_eq!(
existing.connect_options().database,
pool.connect_options().database,
"DATABASE_URL changed at runtime, database differs"
);
existing
}
};
let master_pool = MASTER_POOL
.try_insert(pool)
.unwrap_or_else(|(existing, _pool)| existing);
let mut conn = master_pool.acquire().await?;
@@ -177,11 +160,7 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<Postgres>, Error> {
// Close connections ASAP if left in the idle queue.
.idle_timeout(Some(Duration::from_secs(1)))
.parent(master_pool.clone()),
connect_opts: master_pool
.connect_options()
.deref()
.clone()
.database(&db_name),
connect_opts: master_opts.database(&db_name),
db_name,
})
}