mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
refactor: replace RwLock with AtomicBool for is_exiting and emergency_mode in Handle and NotificationSystem
This commit is contained in:
@@ -1,27 +1,33 @@
|
||||
use crate::{APP_HANDLE, constants::timing, singleton};
|
||||
use parking_lot::RwLock;
|
||||
use smartstring::alias::String;
|
||||
use std::{sync::Arc, thread};
|
||||
use std::{
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
},
|
||||
thread,
|
||||
};
|
||||
use tauri::{AppHandle, Manager, WebviewWindow};
|
||||
use tauri_plugin_mihomo::{Mihomo, MihomoExt};
|
||||
use tokio::sync::RwLockReadGuard;
|
||||
|
||||
use super::notification::{ErrorMessage, FrontendEvent, NotificationSystem};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct Handle {
|
||||
is_exiting: Arc<RwLock<bool>>,
|
||||
is_exiting: AtomicBool,
|
||||
startup_errors: Arc<RwLock<Vec<ErrorMessage>>>,
|
||||
startup_completed: Arc<RwLock<bool>>,
|
||||
startup_completed: AtomicBool,
|
||||
pub(crate) notification_system: Arc<RwLock<Option<NotificationSystem>>>,
|
||||
}
|
||||
|
||||
impl Default for Handle {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
is_exiting: Arc::new(RwLock::new(false)),
|
||||
is_exiting: AtomicBool::new(false),
|
||||
startup_errors: Arc::new(RwLock::new(Vec::new())),
|
||||
startup_completed: Arc::new(RwLock::new(false)),
|
||||
startup_completed: AtomicBool::new(false),
|
||||
notification_system: Arc::new(RwLock::new(Some(NotificationSystem::new()))),
|
||||
}
|
||||
}
|
||||
@@ -108,7 +114,7 @@ impl Handle {
|
||||
let status_str = status.into();
|
||||
let msg_str = msg.into();
|
||||
|
||||
if !*handle.startup_completed.read() {
|
||||
if !handle.startup_completed.load(Ordering::Acquire) {
|
||||
handle.startup_errors.write().push(ErrorMessage {
|
||||
status: status_str,
|
||||
message: msg_str,
|
||||
@@ -139,7 +145,7 @@ impl Handle {
|
||||
}
|
||||
|
||||
pub fn mark_startup_completed(&self) {
|
||||
*self.startup_completed.write() = true;
|
||||
self.startup_completed.store(true, Ordering::Release);
|
||||
self.send_startup_errors();
|
||||
}
|
||||
|
||||
@@ -182,7 +188,7 @@ impl Handle {
|
||||
}
|
||||
|
||||
pub fn set_is_exiting(&self) {
|
||||
*self.is_exiting.write() = true;
|
||||
self.is_exiting.store(true, Ordering::Release);
|
||||
|
||||
let mut system_opt = self.notification_system.write();
|
||||
if let Some(system) = system_opt.as_mut() {
|
||||
@@ -191,7 +197,7 @@ impl Handle {
|
||||
}
|
||||
|
||||
pub fn is_exiting(&self) -> bool {
|
||||
*self.is_exiting.read()
|
||||
self.is_exiting.load(Ordering::Acquire)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ use parking_lot::RwLock;
|
||||
use smartstring::alias::String;
|
||||
use std::{
|
||||
sync::{
|
||||
atomic::{AtomicU64, Ordering},
|
||||
atomic::{AtomicBool, AtomicU64, Ordering},
|
||||
mpsc,
|
||||
},
|
||||
thread,
|
||||
@@ -47,7 +47,7 @@ pub struct NotificationSystem {
|
||||
worker_handle: Option<thread::JoinHandle<()>>,
|
||||
pub(super) is_running: bool,
|
||||
stats: EventStats,
|
||||
emergency_mode: RwLock<bool>,
|
||||
emergency_mode: AtomicBool,
|
||||
}
|
||||
|
||||
impl Default for NotificationSystem {
|
||||
@@ -63,7 +63,7 @@ impl NotificationSystem {
|
||||
worker_handle: None,
|
||||
is_running: false,
|
||||
stats: EventStats::default(),
|
||||
emergency_mode: RwLock::new(false),
|
||||
emergency_mode: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ impl NotificationSystem {
|
||||
}
|
||||
|
||||
fn should_skip_event(&self, event: &FrontendEvent) -> bool {
|
||||
let is_emergency = *self.emergency_mode.read();
|
||||
let is_emergency = self.emergency_mode.load(Ordering::Acquire);
|
||||
matches!(
|
||||
(is_emergency, event),
|
||||
(true, FrontendEvent::NoticeMessage { status, .. }) if status == "info"
|
||||
@@ -184,14 +184,14 @@ impl NotificationSystem {
|
||||
*self.stats.last_error_time.write() = Some(Instant::now());
|
||||
|
||||
let errors = self.stats.total_errors.load(Ordering::Relaxed);
|
||||
if errors > retry::EVENT_EMIT_THRESHOLD && !*self.emergency_mode.read() {
|
||||
if errors > retry::EVENT_EMIT_THRESHOLD && !self.emergency_mode.load(Ordering::Acquire) {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Frontend,
|
||||
"Entering emergency mode after {} errors",
|
||||
errors
|
||||
);
|
||||
*self.emergency_mode.write() = true;
|
||||
self.emergency_mode.store(true, Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user