mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
refactor: replace tokio::task::spawn_blocking with AsyncHandler::spawn_blocking for improved task management
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
## v2.4.1
|
## v2.4.1
|
||||||
|
|
||||||
Something Should Be Done
|
### 🚀 性能优化
|
||||||
|
|
||||||
|
- 优化异步程序性能
|
||||||
|
|
||||||
## v2.4.0
|
## v2.4.0
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ use super::CmdResult;
|
|||||||
use crate::{
|
use crate::{
|
||||||
config::{Config, IProfiles, PrfItem, PrfOption},
|
config::{Config, IProfiles, PrfItem, PrfOption},
|
||||||
core::{handle, timer::Timer, tray::Tray, CoreManager},
|
core::{handle, timer::Timer, tray::Tray, CoreManager},
|
||||||
feat, logging, ret_err,
|
feat, logging,
|
||||||
|
process::AsyncHandler,
|
||||||
|
ret_err,
|
||||||
utils::{dirs, help, logging::Type},
|
utils::{dirs, help, logging::Type},
|
||||||
wrap_err,
|
wrap_err,
|
||||||
};
|
};
|
||||||
@@ -37,7 +39,7 @@ pub async fn get_profiles() -> CmdResult<IProfiles> {
|
|||||||
// 策略1: 尝试快速获取latest数据
|
// 策略1: 尝试快速获取latest数据
|
||||||
let latest_result = tokio::time::timeout(
|
let latest_result = tokio::time::timeout(
|
||||||
Duration::from_millis(500),
|
Duration::from_millis(500),
|
||||||
tokio::task::spawn_blocking(move || {
|
AsyncHandler::spawn_blocking(move || {
|
||||||
let profiles = Config::profiles();
|
let profiles = Config::profiles();
|
||||||
let latest = profiles.latest_ref();
|
let latest = profiles.latest_ref();
|
||||||
IProfiles {
|
IProfiles {
|
||||||
@@ -64,7 +66,7 @@ pub async fn get_profiles() -> CmdResult<IProfiles> {
|
|||||||
// 策略2: 如果快速获取失败,尝试获取data()
|
// 策略2: 如果快速获取失败,尝试获取data()
|
||||||
let data_result = tokio::time::timeout(
|
let data_result = tokio::time::timeout(
|
||||||
Duration::from_secs(2),
|
Duration::from_secs(2),
|
||||||
tokio::task::spawn_blocking(move || {
|
AsyncHandler::spawn_blocking(move || {
|
||||||
let profiles = Config::profiles();
|
let profiles = Config::profiles();
|
||||||
let data = profiles.latest_ref();
|
let data = profiles.latest_ref();
|
||||||
IProfiles {
|
IProfiles {
|
||||||
@@ -102,7 +104,7 @@ pub async fn get_profiles() -> CmdResult<IProfiles> {
|
|||||||
"所有获取配置策略都失败,尝试fallback"
|
"所有获取配置策略都失败,尝试fallback"
|
||||||
);
|
);
|
||||||
|
|
||||||
match tokio::task::spawn_blocking(IProfiles::new).await {
|
match AsyncHandler::spawn_blocking(IProfiles::new).await {
|
||||||
Ok(profiles) => {
|
Ok(profiles) => {
|
||||||
logging!(info, Type::Cmd, true, "使用fallback配置成功");
|
logging!(info, Type::Cmd, true, "使用fallback配置成功");
|
||||||
Ok(profiles)
|
Ok(profiles)
|
||||||
@@ -372,7 +374,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
|||||||
|
|
||||||
match file_read_result {
|
match file_read_result {
|
||||||
Ok(Ok(content)) => {
|
Ok(Ok(content)) => {
|
||||||
let yaml_parse_result = tokio::task::spawn_blocking(move || {
|
let yaml_parse_result = AsyncHandler::spawn_blocking(move || {
|
||||||
serde_yaml::from_str::<serde_yaml::Value>(&content)
|
serde_yaml::from_str::<serde_yaml::Value>(&content)
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#[cfg(any(target_os = "windows", target_os = "linux"))]
|
||||||
|
use crate::process::AsyncHandler;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::time::{timeout, Duration};
|
use tokio::time::{timeout, Duration};
|
||||||
@@ -74,7 +76,7 @@ impl AsyncProxyQuery {
|
|||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
async fn get_auto_proxy_impl() -> Result<AsyncAutoproxy> {
|
async fn get_auto_proxy_impl() -> Result<AsyncAutoproxy> {
|
||||||
// Windows: 从注册表读取PAC配置
|
// Windows: 从注册表读取PAC配置
|
||||||
tokio::task::spawn_blocking(move || -> Result<AsyncAutoproxy> {
|
AsyncHandler::spawn_blocking(move || -> Result<AsyncAutoproxy> {
|
||||||
Self::get_pac_config_from_registry()
|
Self::get_pac_config_from_registry()
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
@@ -258,7 +260,7 @@ impl AsyncProxyQuery {
|
|||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
async fn get_system_proxy_impl() -> Result<AsyncSysproxy> {
|
async fn get_system_proxy_impl() -> Result<AsyncSysproxy> {
|
||||||
// Windows: 使用注册表直接读取代理设置
|
// Windows: 使用注册表直接读取代理设置
|
||||||
tokio::task::spawn_blocking(move || -> Result<AsyncSysproxy> {
|
AsyncHandler::spawn_blocking(move || -> Result<AsyncSysproxy> {
|
||||||
Self::get_system_proxy_from_registry()
|
Self::get_system_proxy_from_registry()
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
|
|||||||
@@ -533,7 +533,7 @@ impl CoreManager {
|
|||||||
use winapi::um::winnt::HANDLE;
|
use winapi::um::winnt::HANDLE;
|
||||||
|
|
||||||
let process_name_clone = process_name.clone();
|
let process_name_clone = process_name.clone();
|
||||||
let pids = tokio::task::spawn_blocking(move || -> Result<Vec<u32>> {
|
let pids = AsyncHandler::spawn_blocking(move || -> Result<Vec<u32>> {
|
||||||
let mut pids = Vec::new();
|
let mut pids = Vec::new();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -628,7 +628,7 @@ impl CoreManager {
|
|||||||
use winapi::um::processthreadsapi::{OpenProcess, TerminateProcess};
|
use winapi::um::processthreadsapi::{OpenProcess, TerminateProcess};
|
||||||
use winapi::um::winnt::{HANDLE, PROCESS_TERMINATE};
|
use winapi::um::winnt::{HANDLE, PROCESS_TERMINATE};
|
||||||
|
|
||||||
tokio::task::spawn_blocking(move || -> bool {
|
AsyncHandler::spawn_blocking(move || -> bool {
|
||||||
unsafe {
|
unsafe {
|
||||||
let process_handle: HANDLE = OpenProcess(PROCESS_TERMINATE, 0, pid);
|
let process_handle: HANDLE = OpenProcess(PROCESS_TERMINATE, 0, pid);
|
||||||
if process_handle.is_null() {
|
if process_handle.is_null() {
|
||||||
@@ -703,7 +703,7 @@ impl CoreManager {
|
|||||||
use winapi::um::processthreadsapi::OpenProcess;
|
use winapi::um::processthreadsapi::OpenProcess;
|
||||||
use winapi::um::winnt::{HANDLE, PROCESS_QUERY_INFORMATION};
|
use winapi::um::winnt::{HANDLE, PROCESS_QUERY_INFORMATION};
|
||||||
|
|
||||||
let result = tokio::task::spawn_blocking(move || -> Result<bool> {
|
let result = AsyncHandler::spawn_blocking(move || -> Result<bool> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let process_handle: HANDLE = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
|
let process_handle: HANDLE = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
|
||||||
if process_handle.is_null() {
|
if process_handle.is_null() {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
use crate::process::AsyncHandler;
|
||||||
use crate::{logging, utils::logging::Type};
|
use crate::{logging, utils::logging::Type};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use hmac::{Hmac, Mac};
|
use hmac::{Hmac, Mac};
|
||||||
@@ -143,7 +145,7 @@ pub async fn send_ipc_request(
|
|||||||
|
|
||||||
let request_json = serde_json::to_string(&request)?;
|
let request_json = serde_json::to_string(&request)?;
|
||||||
|
|
||||||
let result = tokio::task::spawn_blocking(move || -> Result<IpcResponse> {
|
let result = AsyncHandler::spawn_blocking(move || -> Result<IpcResponse> {
|
||||||
let c_pipe_name = match CString::new(IPC_SOCKET_NAME) {
|
let c_pipe_name = match CString::new(IPC_SOCKET_NAME) {
|
||||||
Ok(name) => name,
|
Ok(name) => name,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
@@ -11,4 +11,12 @@ impl AsyncHandler {
|
|||||||
{
|
{
|
||||||
async_runtime::spawn(f())
|
async_runtime::spawn(f())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn spawn_blocking<T, F>(f: F) -> JoinHandle<T>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> T + Send + 'static,
|
||||||
|
T: Send + 'static,
|
||||||
|
{
|
||||||
|
async_runtime::spawn_blocking(f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user