refactor(backend): phase 5 - optimize concurrency with RwLock and async IO

Replace Mutex with RwLock for AppState.config to enable concurrent reads,
improving performance for tray menu building and query operations that
previously blocked each other unnecessarily.

Key changes:
- Migrate AppState.config from Mutex<MultiAppConfig> to RwLock<MultiAppConfig>
- Distinguish read-only operations (read()) from mutations (write()) across
  all command handlers and service layers
- Offload blocking file I/O in import/export commands to spawn_blocking threads,
  minimizing lock hold time and preventing main thread blocking
- Extract load_config_for_import() to separate I/O logic from state updates
- Update all integration tests to use RwLock semantics

Performance impact:
- Concurrent reads: Multiple threads can now query config simultaneously
  (tray menu, provider list, MCP config)
- Reduced contention: Write locks only acquired during actual mutations
- Non-blocking I/O: Config import/export no longer freezes UI thread

All existing tests pass with new locking semantics.
This commit is contained in:
Jason
2025-10-28 12:23:44 +08:00
parent 7e27f88154
commit 7b1a68ee4e
9 changed files with 107 additions and 80 deletions

View File

@@ -45,7 +45,7 @@ fn create_tray_menu(
app: &tauri::AppHandle,
app_state: &AppState,
) -> Result<Menu<tauri::Wry>, AppError> {
let config = app_state.config.lock().map_err(AppError::from)?;
let config = app_state.config.read().map_err(AppError::from)?;
let mut menu_builder = MenuBuilder::new(app);
@@ -433,7 +433,7 @@ pub fn run() {
// 首次启动迁移:扫描副本文件,合并到 config.json并归档副本旧 config.json 先归档
{
let mut config_guard = app_state.config.lock().unwrap();
let mut config_guard = app_state.config.write().unwrap();
let migrated = migration::migrate_copies_into_config(&mut config_guard)?;
if migrated {
log::info!("已将副本文件导入到 config.json并完成归档");