mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 10:10:30 +08:00
Run heavy read commands on the blocking pool
This commit is contained in:
@@ -6,3 +6,21 @@ pub mod profile;
|
||||
pub mod repo;
|
||||
pub mod startup;
|
||||
pub mod thread;
|
||||
|
||||
use radicle_types::AppState;
|
||||
use radicle_types::error::Error;
|
||||
|
||||
/// Run a blocking, synchronous port method on the async runtime's blocking
|
||||
/// pool instead of a worker thread. The port methods do blocking libgit2/COB
|
||||
/// work; running them inline on an `async` command would pin a runtime worker
|
||||
/// for the call's full duration and let a slow read (e.g. listing refs or
|
||||
/// jobs) stall every other command queued behind it. Cloning `AppState` is
|
||||
/// cheap — it only clones the `Profile` (paths and keys, not open handles).
|
||||
pub(crate) async fn blocking<T, F>(ctx: tauri::State<'_, AppState>, f: F) -> Result<T, Error>
|
||||
where
|
||||
F: FnOnce(AppState) -> Result<T, Error> + Send + 'static,
|
||||
T: Send + 'static,
|
||||
{
|
||||
let state = (*ctx).clone();
|
||||
tauri::async_runtime::spawn_blocking(move || f(state)).await?
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@ use radicle_types::error::Error;
|
||||
use radicle_types::traits::job::Jobs;
|
||||
|
||||
use crate::AppState;
|
||||
use crate::commands::blocking;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn list_jobs(
|
||||
ctx: tauri::State<AppState>,
|
||||
pub async fn list_jobs(
|
||||
ctx: tauri::State<'_, AppState>,
|
||||
rid: RepoId,
|
||||
sha: git::Oid,
|
||||
) -> Result<Vec<job::Job>, Error> {
|
||||
ctx.list_jobs(rid, sha)
|
||||
blocking(ctx, move |ctx| ctx.list_jobs(rid, sha)).await
|
||||
}
|
||||
|
||||
@@ -6,25 +6,26 @@ use radicle_types::error::Error;
|
||||
use radicle_types::traits::repo::{Repo, Show};
|
||||
|
||||
use crate::AppState;
|
||||
use crate::commands::blocking;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_repos(
|
||||
ctx: tauri::State<'_, AppState>,
|
||||
show: Show,
|
||||
) -> Result<Vec<types::repo::RepoInfo>, Error> {
|
||||
ctx.list_repos(show)
|
||||
blocking(ctx, move |ctx| ctx.list_repos(show)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_repos_summary(
|
||||
ctx: tauri::State<'_, AppState>,
|
||||
) -> Result<Vec<types::repo::RepoSummary>, Error> {
|
||||
ctx.list_repos_summary()
|
||||
blocking(ctx, |ctx| ctx.list_repos_summary()).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn repo_count(ctx: tauri::State<'_, AppState>) -> Result<types::repo::RepoCount, Error> {
|
||||
ctx.repo_count()
|
||||
blocking(ctx, |ctx| ctx.repo_count()).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -32,7 +33,7 @@ pub async fn list_repo_refs(
|
||||
ctx: tauri::State<'_, AppState>,
|
||||
rid: RepoId,
|
||||
) -> Result<types::repo::RepoRefs, Error> {
|
||||
ctx.list_repo_refs(rid)
|
||||
blocking(ctx, move |ctx| ctx.list_repo_refs(rid)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -40,7 +41,7 @@ pub async fn repo_by_id(
|
||||
ctx: tauri::State<'_, AppState>,
|
||||
rid: RepoId,
|
||||
) -> Result<types::repo::RepoInfo, Error> {
|
||||
ctx.repo_by_id(rid)
|
||||
blocking(ctx, move |ctx| ctx.repo_by_id(rid)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -51,7 +52,7 @@ pub async fn repo_readme(
|
||||
peer: Option<NodeId>,
|
||||
revision: Option<String>,
|
||||
) -> Result<Option<types::repo::Readme>, Error> {
|
||||
ctx.repo_readme(rid, sha, peer, revision)
|
||||
blocking(ctx, move |ctx| ctx.repo_readme(rid, sha, peer, revision)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -63,7 +64,10 @@ pub async fn repo_tree(
|
||||
peer: Option<NodeId>,
|
||||
revision: Option<String>,
|
||||
) -> Result<types::source::tree::Tree, Error> {
|
||||
ctx.repo_tree(rid, path, sha, peer, revision)
|
||||
blocking(ctx, move |ctx| {
|
||||
ctx.repo_tree(rid, path, sha, peer, revision)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -73,7 +77,7 @@ pub async fn repo_blob(
|
||||
path: std::path::PathBuf,
|
||||
sha: Option<git::Oid>,
|
||||
) -> Result<types::source::blob::Blob, Error> {
|
||||
ctx.repo_blob(rid, path, sha)
|
||||
blocking(ctx, move |ctx| ctx.repo_blob(rid, path, sha)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -83,7 +87,7 @@ pub async fn diff_stats(
|
||||
base: git::Oid,
|
||||
head: git::Oid,
|
||||
) -> Result<types::diff::Stats, Error> {
|
||||
ctx.diff_stats(rid, base, head)
|
||||
blocking(ctx, move |ctx| ctx.diff_stats(rid, base, head)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -93,7 +97,7 @@ pub async fn list_commits(
|
||||
base: String,
|
||||
head: String,
|
||||
) -> Result<Vec<types::repo::Commit>, Error> {
|
||||
ctx.list_commits(rid, base, head)
|
||||
blocking(ctx, move |ctx| ctx.list_commits(rid, base, head)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -104,7 +108,10 @@ pub async fn get_commit_diff(
|
||||
unified: Option<u32>,
|
||||
highlight: Option<bool>,
|
||||
) -> Result<types::diff::Diff, Error> {
|
||||
ctx.get_commit_diff(rid, sha, unified, highlight)
|
||||
blocking(ctx, move |ctx| {
|
||||
ctx.get_commit_diff(rid, sha, unified, highlight)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -117,7 +124,10 @@ pub async fn list_repo_commits(
|
||||
skip: Option<usize>,
|
||||
take: Option<usize>,
|
||||
) -> Result<types::cobs::PaginatedQuery<Vec<types::repo::Commit>>, Error> {
|
||||
ctx.list_repo_commits(rid, head, peer, revision, skip, take)
|
||||
blocking(ctx, move |ctx| {
|
||||
ctx.list_repo_commits(rid, head, peer, revision, skip, take)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -126,7 +136,7 @@ pub async fn repo_commit_count(
|
||||
rid: RepoId,
|
||||
head: git::Oid,
|
||||
) -> Result<usize, Error> {
|
||||
ctx.repo_commit_count(rid, head)
|
||||
blocking(ctx, move |ctx| ctx.repo_commit_count(rid, head)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -137,7 +147,7 @@ pub async fn repo_commit(
|
||||
peer: Option<NodeId>,
|
||||
revision: Option<String>,
|
||||
) -> Result<types::repo::Commit, Error> {
|
||||
ctx.repo_commit(rid, sha, peer, revision)
|
||||
blocking(ctx, move |ctx| ctx.repo_commit(rid, sha, peer, revision)).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
|
||||
Reference in New Issue
Block a user