Skip to content

Commit b19a848

Browse files
authored
Merge pull request #17 from qaspen-python/feature/change_db_pool
Removed Arc<RwLock> from Connection Pool
2 parents 9ae0eec + c1b7b1a commit b19a848

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/driver/connection_pool.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212

1313
use super::{common_options::ConnRecyclingMethod, connection::Connection};
1414

15-
/// `PSQLPool` for internal use only.
15+
/// `PSQLPool` is for internal use only.
1616
///
1717
/// It is not exposed to python.
1818
pub struct RustPSQLPool {
@@ -24,7 +24,7 @@ pub struct RustPSQLPool {
2424
db_name: Option<String>,
2525
max_db_pool_size: Option<usize>,
2626
conn_recycling_method: Option<ConnRecyclingMethod>,
27-
db_pool: Arc<tokio::sync::RwLock<Option<Pool>>>,
27+
db_pool: Option<Pool>,
2828
}
2929

3030
impl RustPSQLPool {
@@ -50,7 +50,7 @@ impl RustPSQLPool {
5050
db_name,
5151
max_db_pool_size,
5252
conn_recycling_method,
53-
db_pool: Arc::new(tokio::sync::RwLock::new(None)),
53+
db_pool: None,
5454
}
5555
}
5656
}
@@ -61,11 +61,8 @@ impl RustPSQLPool {
6161
/// # Errors
6262
/// May return Err Result if cannot get new connection from the pool.
6363
pub async fn inner_connection(&self) -> RustPSQLDriverPyResult<Connection> {
64-
let db_pool_arc = self.db_pool.clone();
65-
66-
let db_pool_guard = db_pool_arc.read().await;
67-
68-
let db_pool_manager = db_pool_guard
64+
let db_pool_manager = self
65+
.db_pool
6966
.as_ref()
7067
.ok_or(RustPSQLDriverError::DatabasePoolError(
7168
"Database pool is not initialized".into(),
@@ -89,11 +86,8 @@ impl RustPSQLPool {
8986
querystring: String,
9087
parameters: Vec<PythonDTO>,
9188
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
92-
let db_pool_arc = self.db_pool.clone();
93-
94-
let db_pool_guard = db_pool_arc.read().await;
95-
96-
let db_pool_manager = db_pool_guard
89+
let db_pool_manager = self
90+
.db_pool
9791
.as_ref()
9892
.ok_or(RustPSQLDriverError::DatabasePoolError(
9993
"Database pool is not initialized".into(),
@@ -120,8 +114,7 @@ impl RustPSQLPool {
120114
/// # Errors
121115
/// May return Err Result if Database pool is already initialized,
122116
/// `max_db_pool_size` is less than 2 or it's impossible to build db pool.
123-
pub async fn inner_startup(&self) -> RustPSQLDriverPyResult<()> {
124-
let db_pool_arc = self.db_pool.clone();
117+
pub fn inner_startup(&mut self) -> RustPSQLDriverPyResult<()> {
125118
let dsn = self.dsn.clone();
126119
let password = self.password.clone();
127120
let username = self.username.clone();
@@ -131,8 +124,7 @@ impl RustPSQLPool {
131124
let conn_recycling_method = self.conn_recycling_method;
132125
let max_db_pool_size = self.max_db_pool_size;
133126

134-
let mut db_pool_guard = db_pool_arc.write().await;
135-
if db_pool_guard.is_some() {
127+
if self.db_pool.is_some() {
136128
return Err(RustPSQLDriverError::DatabasePoolError(
137129
"Database pool is already initialized".into(),
138130
));
@@ -185,7 +177,7 @@ impl RustPSQLPool {
185177
db_pool_builder = db_pool_builder.max_size(max_db_pool_size);
186178
}
187179

188-
*db_pool_guard = Some(db_pool_builder.build()?);
180+
self.db_pool = Some(db_pool_builder.build()?);
189181
Ok(())
190182
}
191183
}
@@ -220,7 +212,7 @@ impl PSQLPool {
220212
db_name,
221213
max_db_pool_size,
222214
conn_recycling_method,
223-
db_pool: Arc::new(tokio::sync::RwLock::new(None)),
215+
db_pool: None,
224216
})),
225217
}
226218
}
@@ -232,8 +224,8 @@ impl PSQLPool {
232224
pub fn startup<'a>(&'a self, py: Python<'a>) -> RustPSQLDriverPyResult<&'a PyAny> {
233225
let psql_pool_arc = self.rust_psql_pool.clone();
234226
rustengine_future(py, async move {
235-
let db_pool_guard = psql_pool_arc.write().await;
236-
db_pool_guard.inner_startup().await?;
227+
let mut db_pool_guard = psql_pool_arc.write().await;
228+
db_pool_guard.inner_startup()?;
237229
Ok(())
238230
})
239231
}

0 commit comments

Comments
 (0)