perf: spawn clean task (#5595)

* perf: spawn task

* perf: spawn task to save draft date

* perf: store proxy client

* chore: update

* perf: reduce handle quit on macOS

* chore: clippy

* docs: update Changelog.md

* chore: update
This commit is contained in:
oomeow
2025-11-28 21:12:34 +08:00
committed by GitHub
parent 683667f8ae
commit 6456c597ed
6 changed files with 114 additions and 113 deletions

View File

@@ -7,6 +7,8 @@ use crate::{
};
use anyhow::Result;
use clash_verge_logging::{Type, logging, logging_error};
#[cfg(not(target_os = "windows"))]
use parking_lot::Mutex;
use parking_lot::RwLock;
use scopeguard::defer;
use smartstring::alias::String;
@@ -23,6 +25,10 @@ use tauri_plugin_autostart::ManagerExt as _;
pub struct Sysopt {
update_sysproxy: AtomicBool,
reset_sysproxy: AtomicBool,
#[cfg(not(target_os = "windows"))]
sysproxy: Arc<Mutex<Sysproxy>>,
#[cfg(not(target_os = "windows"))]
autoproxy: Arc<Mutex<Autoproxy>>,
guard: Arc<RwLock<GuardMonitor>>,
}
@@ -31,6 +37,10 @@ impl Default for Sysopt {
Self {
update_sysproxy: AtomicBool::new(false),
reset_sysproxy: AtomicBool::new(false),
#[cfg(not(target_os = "windows"))]
sysproxy: Arc::new(Mutex::new(Sysproxy::default())),
#[cfg(not(target_os = "windows"))]
autoproxy: Arc::new(Mutex::new(Autoproxy::default())),
guard: Arc::new(RwLock::new(GuardMonitor::new(
GuardType::None,
Duration::from_secs(30),
@@ -185,23 +195,25 @@ impl Sysopt {
#[cfg(not(target_os = "windows"))]
{
let mut sys = Sysproxy {
enable: false,
host: proxy_host.clone().into(),
port,
bypass: get_bypass().await.into(),
};
let mut auto = Autoproxy {
enable: false,
url: format!("http://{proxy_host}:{pac_port}/commands/pac"),
};
// 先 await, 避免持有锁导致的 Send 问题
let bypass = get_bypass().await;
let mut sys = self.sysproxy.lock();
sys.enable = false;
sys.host = proxy_host.clone().into();
sys.port = port;
sys.bypass = bypass.into();
let mut auto = self.autoproxy.lock();
auto.enable = false;
auto.url = format!("http://{proxy_host}:{pac_port}/commands/pac");
if !sys_enable {
sys.set_system_proxy()?;
auto.set_auto_proxy()?;
self.access_guard()
.write()
.set_guard_type(GuardType::Sysproxy(sys));
.set_guard_type(GuardType::Sysproxy(sys.clone()));
return Ok(());
}
@@ -212,7 +224,7 @@ impl Sysopt {
auto.set_auto_proxy()?;
self.access_guard()
.write()
.set_guard_type(GuardType::Autoproxy(auto));
.set_guard_type(GuardType::Autoproxy(auto.clone()));
return Ok(());
}
@@ -221,9 +233,11 @@ impl Sysopt {
sys.enable = true;
auto.set_auto_proxy()?;
sys.set_system_proxy()?;
drop(auto);
self.access_guard()
.write()
.set_guard_type(GuardType::Sysproxy(sys));
.set_guard_type(GuardType::Sysproxy(sys.clone()));
drop(sys);
return Ok(());
}
}
@@ -282,32 +296,13 @@ impl Sysopt {
//直接关闭所有代理
#[cfg(not(target_os = "windows"))]
{
let mut sysproxy: Sysproxy = match Sysproxy::get_system_proxy() {
Ok(sp) => sp,
Err(e) => {
logging!(
warn,
Type::Core,
"Warning: 重置代理时获取系统代理配置失败: {e}, 使用默认配置"
);
Sysproxy::default()
}
};
let mut autoproxy = match Autoproxy::get_auto_proxy() {
Ok(ap) => ap,
Err(e) => {
logging!(
warn,
Type::Core,
"Warning: 重置代理时获取自动代理配置失败: {e}, 使用默认配置"
);
Autoproxy::default()
}
};
let mut sysproxy = self.sysproxy.lock();
sysproxy.enable = false;
sysproxy.set_system_proxy()?;
drop(sysproxy);
let mut autoproxy = self.autoproxy.lock();
autoproxy.enable = false;
autoproxy.set_auto_proxy()?;
sysproxy.set_system_proxy()?;
}
#[cfg(target_os = "windows")]