refactor: streamline error handling and resource management in various modules

This commit is contained in:
Tunglies
2025-11-06 10:18:20 +08:00
parent 671ac2ebed
commit 69a706b438
13 changed files with 66 additions and 50 deletions

View File

@@ -109,8 +109,7 @@ impl Handle {
let msg_str = msg.into();
if !*handle.startup_completed.read() {
let mut errors = handle.startup_errors.write();
errors.push(ErrorMessage {
handle.startup_errors.write().push(ErrorMessage {
status: status_str,
message: msg_str,
});

View File

@@ -68,7 +68,8 @@ impl CoreManager {
#[cfg(target_os = "windows")]
self.wait_for_service_if_needed().await;
let mode = match SERVICE_MANAGER.lock().await.current() {
let value = SERVICE_MANAGER.lock().await.current();
let mode = match value {
ServiceStatus::Ready => RunningMode::Service,
_ => RunningMode::Sidecar,
};

View File

@@ -62,8 +62,12 @@ impl CoreManager {
| tauri_plugin_shell::process::CommandEvent::Stderr(line) => {
let mut now = DeferredNow::default();
let message = CompactString::from(String::from_utf8_lossy(&line).as_ref());
let w = shared_writer.lock().await;
write_sidecar_log(w, &mut now, Level::Error, &message);
write_sidecar_log(
shared_writer.lock().await,
&mut now,
Level::Error,
&message,
);
CLASH_LOGGER.append_log(message).await;
}
tauri_plugin_shell::process::CommandEvent::Terminated(term) => {
@@ -75,8 +79,12 @@ impl CoreManager {
} else {
CompactString::from("Process terminated")
};
let w = shared_writer.lock().await;
write_sidecar_log(w, &mut now, Level::Info, &message);
write_sidecar_log(
shared_writer.lock().await,
&mut now,
Level::Info,
&message,
);
CLASH_LOGGER.clear_logs().await;
break;
}

View File

@@ -102,10 +102,13 @@ impl NotificationSystem {
}
}
// Clippy 似乎对 parking lot 的 RwLock 有误报,这里禁用相关警告
#[allow(clippy::significant_drop_tightening)]
fn process_event(handle: &super::handle::Handle, event: FrontendEvent) {
let system_guard = handle.notification_system.read();
let Some(system) = system_guard.as_ref() else {
return;
let binding = handle.notification_system.read();
let system = match binding.as_ref() {
Some(s) => s,
None => return,
};
if system.should_skip_event(&event) {

View File

@@ -154,7 +154,6 @@ impl Timer {
/// 每 3 秒更新系统托盘菜单,总共执行 3 次
pub fn add_update_tray_menu_task(&self) -> Result<()> {
let tid = self.timer_count.fetch_add(1, Ordering::SeqCst);
let delay_timer = self.delay_timer.write();
let task = TaskBuilder::default()
.set_task_id(tid)
.set_maximum_parallel_runnable_num(1)
@@ -164,7 +163,8 @@ impl Timer {
crate::core::tray::Tray::global().update_menu().await
})
.context("failed to create update tray menu timer task")?;
delay_timer
self.delay_timer
.write()
.add_task(task)
.context("failed to add update tray menu timer task")?;
Ok(())
@@ -193,14 +193,12 @@ impl Timer {
// Perform sync operations while holding locks
{
let mut timer_map = self.timer_map.write();
let delay_timer = self.delay_timer.write();
for (uid, diff) in diff_map {
match diff {
DiffFlag::Del(tid) => {
timer_map.remove(&uid);
if let Err(e) = delay_timer.remove_task(tid) {
self.timer_map.write().remove(&uid);
let value = self.delay_timer.write().remove_task(tid);
if let Err(e) = value {
logging!(
warn,
Type::Timer,
@@ -220,12 +218,13 @@ impl Timer {
last_run: chrono::Local::now().timestamp(),
};
timer_map.insert(uid.clone(), task);
self.timer_map.write().insert(uid.clone(), task);
operations_to_add.push((uid, tid, interval));
}
DiffFlag::Mod(tid, interval) => {
// Remove old task first
if let Err(e) = delay_timer.remove_task(tid) {
let value = self.delay_timer.write().remove_task(tid);
if let Err(e) = value {
logging!(
warn,
Type::Timer,
@@ -243,7 +242,7 @@ impl Timer {
last_run: chrono::Local::now().timestamp(),
};
timer_map.insert(uid.clone(), task);
self.timer_map.write().insert(uid.clone(), task);
operations_to_add.push((uid, tid, interval));
}
}

View File

@@ -56,18 +56,17 @@ fn get_tray_click_debounce() -> &'static Mutex<Instant> {
fn should_handle_tray_click() -> bool {
let debounce_lock = get_tray_click_debounce();
let mut last_click = debounce_lock.lock();
let now = Instant::now();
if now.duration_since(*last_click) >= Duration::from_millis(TRAY_CLICK_DEBOUNCE_MS) {
*last_click = now;
if now.duration_since(*debounce_lock.lock()) >= Duration::from_millis(TRAY_CLICK_DEBOUNCE_MS) {
*debounce_lock.lock() = now;
true
} else {
logging!(
debug,
Type::Tray,
"托盘点击被防抖机制忽略,距离上次点击 {}ms",
now.duration_since(*last_click).as_millis()
now.duration_since(*debounce_lock.lock()).as_millis()
);
false
}