Skip to content

Commit

Permalink
fix(sql): replace Mutex with RwLock to enable concurrent SQL execution (
Browse files Browse the repository at this point in the history
  • Loading branch information
cijiugechu authored Oct 26, 2024
1 parent 77149dc commit 4341d7f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
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

0 comments on commit 4341d7f

Please sign in to comment.