Skip to content

Removed Arc<RwLock> from Connection Pool #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/driver/connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,7 +24,7 @@ pub struct RustPSQLPool {
db_name: Option<String>,
max_db_pool_size: Option<usize>,
conn_recycling_method: Option<ConnRecyclingMethod>,
db_pool: Arc<tokio::sync::RwLock<Option<Pool>>>,
db_pool: Option<Pool>,
}

impl RustPSQLPool {
Expand All @@ -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,
}
}
}
Expand All @@ -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<Connection> {
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(),
Expand All @@ -89,11 +86,8 @@ impl RustPSQLPool {
querystring: String,
parameters: Vec<PythonDTO>,
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
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(),
Expand All @@ -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();
Expand All @@ -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(),
));
Expand Down Expand Up @@ -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(())
}
}
Expand Down Expand Up @@ -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,
})),
}
}
Expand All @@ -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(())
})
}
Expand Down