From c1b7b1a18e8e2a0281b1d52e9c0db2b95c78910b Mon Sep 17 00:00:00 2001 From: "chandr-andr (Kiselev Aleksandr)" Date: Wed, 13 Mar 2024 21:55:29 +0100 Subject: [PATCH] init --- src/driver/connection_pool.rs | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/driver/connection_pool.rs b/src/driver/connection_pool.rs index cba6fac6..9baee4fc 100644 --- a/src/driver/connection_pool.rs +++ b/src/driver/connection_pool.rs @@ -12,7 +12,7 @@ use crate::{ use super::{common_options::ConnRecyclingMethod, connection::Connection}; -/// `PSQLPool` for internal use only. +/// `PSQLPool` is for internal use only. /// /// It is not exposed to python. pub struct RustPSQLPool { @@ -24,7 +24,7 @@ pub struct RustPSQLPool { db_name: Option, max_db_pool_size: Option, conn_recycling_method: Option, - db_pool: Arc>>, + db_pool: Option, } impl RustPSQLPool { @@ -50,7 +50,7 @@ impl RustPSQLPool { db_name, max_db_pool_size, conn_recycling_method, - db_pool: Arc::new(tokio::sync::RwLock::new(None)), + db_pool: None, } } } @@ -61,11 +61,8 @@ impl RustPSQLPool { /// # Errors /// May return Err Result if cannot get new connection from the pool. pub async fn inner_connection(&self) -> RustPSQLDriverPyResult { - let db_pool_arc = self.db_pool.clone(); - - let db_pool_guard = db_pool_arc.read().await; - - let db_pool_manager = db_pool_guard + let db_pool_manager = self + .db_pool .as_ref() .ok_or(RustPSQLDriverError::DatabasePoolError( "Database pool is not initialized".into(), @@ -89,11 +86,8 @@ impl RustPSQLPool { querystring: String, parameters: Vec, ) -> RustPSQLDriverPyResult { - let db_pool_arc = self.db_pool.clone(); - - let db_pool_guard = db_pool_arc.read().await; - - let db_pool_manager = db_pool_guard + let db_pool_manager = self + .db_pool .as_ref() .ok_or(RustPSQLDriverError::DatabasePoolError( "Database pool is not initialized".into(), @@ -120,8 +114,7 @@ impl RustPSQLPool { /// # Errors /// May return Err Result if Database pool is already initialized, /// `max_db_pool_size` is less than 2 or it's impossible to build db pool. - pub async fn inner_startup(&self) -> RustPSQLDriverPyResult<()> { - let db_pool_arc = self.db_pool.clone(); + pub fn inner_startup(&mut self) -> RustPSQLDriverPyResult<()> { let dsn = self.dsn.clone(); let password = self.password.clone(); let username = self.username.clone(); @@ -131,8 +124,7 @@ impl RustPSQLPool { let conn_recycling_method = self.conn_recycling_method; let max_db_pool_size = self.max_db_pool_size; - let mut db_pool_guard = db_pool_arc.write().await; - if db_pool_guard.is_some() { + if self.db_pool.is_some() { return Err(RustPSQLDriverError::DatabasePoolError( "Database pool is already initialized".into(), )); @@ -185,7 +177,7 @@ impl RustPSQLPool { db_pool_builder = db_pool_builder.max_size(max_db_pool_size); } - *db_pool_guard = Some(db_pool_builder.build()?); + self.db_pool = Some(db_pool_builder.build()?); Ok(()) } } @@ -220,7 +212,7 @@ impl PSQLPool { db_name, max_db_pool_size, conn_recycling_method, - db_pool: Arc::new(tokio::sync::RwLock::new(None)), + db_pool: None, })), } } @@ -232,8 +224,8 @@ impl PSQLPool { pub fn startup<'a>(&'a self, py: Python<'a>) -> RustPSQLDriverPyResult<&'a PyAny> { let psql_pool_arc = self.rust_psql_pool.clone(); rustengine_future(py, async move { - let db_pool_guard = psql_pool_arc.write().await; - db_pool_guard.inner_startup().await?; + let mut db_pool_guard = psql_pool_arc.write().await; + db_pool_guard.inner_startup()?; Ok(()) }) }