From 3bcdd076c3cd82b2aa292cec8e65919b2d3dcf59 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 15 Dec 2023 17:23:24 -0800 Subject: [PATCH] Update 'deadpool' to 0.10. Also updates: * 'deadpool-postgres' to 0.12 * 'deadpool-redis' to 0.14 --- contrib/db_pools/lib/Cargo.toml | 17 +++++++---- contrib/db_pools/lib/src/lib.rs | 4 +-- contrib/db_pools/lib/src/pool.rs | 48 ++++++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/contrib/db_pools/lib/Cargo.toml b/contrib/db_pools/lib/Cargo.toml index e8304065..175ef419 100644 --- a/contrib/db_pools/lib/Cargo.toml +++ b/contrib/db_pools/lib/Cargo.toml @@ -23,8 +23,8 @@ sqlx_postgres = ["sqlx", "sqlx/postgres"] sqlx_sqlite = ["sqlx", "sqlx/sqlite"] sqlx_macros = ["sqlx/macros"] # diesel features -diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "diesel", "deadpool"] -diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "diesel", "deadpool"] +diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "diesel", "deadpool_09"] +diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "diesel", "deadpool_09"] # implicit features: mongodb [dependencies.rocket] @@ -36,20 +36,27 @@ default-features = false path = "../codegen" version = "0.1.0" -[dependencies.deadpool] +[dependencies.deadpool_09] +package = "deadpool" version = "0.9" default-features = false features = ["rt_tokio_1", "managed"] optional = true [dependencies.deadpool-postgres] -version = "0.10" +version = "0.12" default-features = false features = ["rt_tokio_1"] optional = true +[dependencies.deadpool] +version = "0.10" +default-features = false +features = ["rt_tokio_1", "managed"] +optional = true + [dependencies.deadpool-redis] -version = "0.12" +version = "0.14" default-features = false features = ["rt_tokio_1"] optional = true diff --git a/contrib/db_pools/lib/src/lib.rs b/contrib/db_pools/lib/src/lib.rs index 6e6b886a..ae5118fb 100644 --- a/contrib/db_pools/lib/src/lib.rs +++ b/contrib/db_pools/lib/src/lib.rs @@ -107,8 +107,8 @@ //! //! | Database | Feature | [`Pool`] Type | [`Connection`] Deref | //! |----------|-----------------------------|-----------------------------|--------------------------------------| -//! | Postgres | `deadpool_postgres` (v0.10) | [`deadpool_postgres::Pool`] | [`deadpool_postgres::ClientWrapper`] | -//! | Redis | `deadpool_redis` (v0.11) | [`deadpool_redis::Pool`] | [`deadpool_redis::Connection`] | +//! | Postgres | `deadpool_postgres` (v0.12) | [`deadpool_postgres::Pool`] | [`deadpool_postgres::ClientWrapper`] | +//! | Redis | `deadpool_redis` (v0.14) | [`deadpool_redis::Pool`] | [`deadpool_redis::Connection`] | //! //! On shutdown, new connections are denied. Shutdown _does not_ wait for //! connections to be returned. diff --git a/contrib/db_pools/lib/src/pool.rs b/contrib/db_pools/lib/src/pool.rs index 694a2064..4e3ef35d 100644 --- a/contrib/db_pools/lib/src/pool.rs +++ b/contrib/db_pools/lib/src/pool.rs @@ -156,9 +156,7 @@ pub trait Pool: Sized + Send + Sync + 'static { mod deadpool_postgres { use deadpool::{managed::{Manager, Pool, PoolError, Object, BuildError}, Runtime}; use super::{Duration, Error, Config, Figment}; - - #[cfg(any(feature = "diesel_postgres", feature = "diesel_mysql"))] - use diesel_async::pooled_connection::AsyncDieselConnectionManager; + use rocket::Either; pub trait DeadManager: Manager + Sized + Send + Sync + 'static { fn new(config: &Config) -> Result; @@ -178,6 +176,50 @@ mod deadpool_postgres { } } + #[rocket::async_trait] + impl>> crate::Pool for Pool + where M::Type: Send, C: Send + Sync + 'static, M::Error: std::error::Error + { + type Error = Error, PoolError>; + + type Connection = C; + + async fn init(figment: &Figment) -> Result { + let config: Config = figment.extract()?; + let manager = M::new(&config).map_err(|e| Error::Init(Either::Left(e)))?; + + Pool::builder(manager) + .max_size(config.max_connections) + .wait_timeout(Some(Duration::from_secs(config.connect_timeout))) + .create_timeout(Some(Duration::from_secs(config.connect_timeout))) + .recycle_timeout(config.idle_timeout.map(Duration::from_secs)) + .runtime(Runtime::Tokio1) + .build() + .map_err(|e| Error::Init(Either::Right(e))) + } + + async fn get(&self) -> Result { + self.get().await.map_err(Error::Get) + } + + async fn close(&self) { + >::close(self) + } + } +} + +// TODO: Remove when new release of diesel-async with deadpool 0.10 is out. +#[cfg(all(feature = "deadpool_09", any(feature = "diesel_postgres", feature = "diesel_mysql")))] +mod deadpool_old { + use deadpool_09::{managed::{Manager, Pool, PoolError, Object, BuildError}, Runtime}; + use diesel_async::pooled_connection::AsyncDieselConnectionManager; + + use super::{Duration, Error, Config, Figment}; + + pub trait DeadManager: Manager + Sized + Send + Sync + 'static { + fn new(config: &Config) -> Result; + } + #[cfg(feature = "diesel_postgres")] impl DeadManager for AsyncDieselConnectionManager { fn new(config: &Config) -> Result {