Skip to content
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

fix(sql): replace Mutex with RwLock to enable concurrent SQL execution #1972

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changes/fix-sql-blocking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
sql: patch
---

Replace `Mutex` with `RwLock` to enable concurrent SQL execution.
16 changes: 7 additions & 9 deletions plugins/sql/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) async fn load<R: Runtime>(
pool.migrate(&migrator).await?;
}

db_instances.0.lock().await.insert(db.clone(), pool);
db_instances.0.write().await.insert(db.clone(), pool);

Ok(db)
}
Expand All @@ -36,7 +36,7 @@ pub(crate) async fn close(
db_instances: State<'_, DbInstances>,
db: Option<String>,
) -> Result<bool, crate::Error> {
let mut instances = db_instances.0.lock().await;
let instances = db_instances.0.read().await;

let pools = if let Some(db) = db {
vec![db]
Expand All @@ -45,9 +45,7 @@ pub(crate) async fn close(
};

for pool in pools {
let db = instances
.get_mut(&pool)
.ok_or(Error::DatabaseNotLoaded(pool))?;
let db = instances.get(&pool).ok_or(Error::DatabaseNotLoaded(pool))?;
db.close().await;
}

Expand All @@ -62,9 +60,9 @@ pub(crate) async fn execute(
query: String,
values: Vec<JsonValue>,
) -> Result<(u64, LastInsertId), crate::Error> {
let mut instances = db_instances.0.lock().await;
let instances = db_instances.0.read().await;

let db = instances.get_mut(&db).ok_or(Error::DatabaseNotLoaded(db))?;
let db = instances.get(&db).ok_or(Error::DatabaseNotLoaded(db))?;
db.execute(query, values).await
}

Expand All @@ -75,8 +73,8 @@ pub(crate) async fn select(
query: String,
values: Vec<JsonValue>,
) -> Result<Vec<IndexMap<String, JsonValue>>, crate::Error> {
let mut instances = db_instances.0.lock().await;
let instances = db_instances.0.read().await;

let db = instances.get_mut(&db).ok_or(Error::DatabaseNotLoaded(db))?;
let db = instances.get(&db).ok_or(Error::DatabaseNotLoaded(db))?;
db.select(query, values).await
}
8 changes: 4 additions & 4 deletions plugins/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ use tauri::{
plugin::{Builder as PluginBuilder, TauriPlugin},
Manager, RunEvent, Runtime,
};
use tokio::sync::Mutex;
use tokio::sync::{Mutex, RwLock};

use std::collections::HashMap;

#[derive(Default)]
pub struct DbInstances(pub Mutex<HashMap<String, DbPool>>);
pub struct DbInstances(pub RwLock<HashMap<String, DbPool>>);

#[derive(Serialize)]
#[serde(untagged)]
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Builder {

tauri::async_runtime::block_on(async move {
let instances = DbInstances::default();
let mut lock = instances.0.lock().await;
let mut lock = instances.0.write().await;

for db in config.preload {
let pool = DbPool::connect(&db, app).await?;
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Builder {
if let RunEvent::Exit = event {
tauri::async_runtime::block_on(async move {
let instances = &*app.state::<DbInstances>();
let instances = instances.0.lock().await;
let instances = instances.0.read().await;
for value in instances.values() {
value.close().await;
}
Expand Down
Loading