refactor: replace tokio::task::spawn_blocking with AsyncHandler::spawn_blocking for improved task management

This commit is contained in:
Tunglies
2025-08-22 03:52:30 +08:00
parent e4c243de2d
commit 6d112c387d
6 changed files with 28 additions and 12 deletions

View File

@@ -1,6 +1,8 @@
## v2.4.1
Something Should Be Done
### 🚀 性能优化
- 优化异步程序性能
## v2.4.0

View File

@@ -2,7 +2,9 @@ use super::CmdResult;
use crate::{
config::{Config, IProfiles, PrfItem, PrfOption},
core::{handle, timer::Timer, tray::Tray, CoreManager},
feat, logging, ret_err,
feat, logging,
process::AsyncHandler,
ret_err,
utils::{dirs, help, logging::Type},
wrap_err,
};
@@ -37,7 +39,7 @@ pub async fn get_profiles() -> CmdResult<IProfiles> {
// 策略1: 尝试快速获取latest数据
let latest_result = tokio::time::timeout(
Duration::from_millis(500),
tokio::task::spawn_blocking(move || {
AsyncHandler::spawn_blocking(move || {
let profiles = Config::profiles();
let latest = profiles.latest_ref();
IProfiles {
@@ -64,7 +66,7 @@ pub async fn get_profiles() -> CmdResult<IProfiles> {
// 策略2: 如果快速获取失败尝试获取data()
let data_result = tokio::time::timeout(
Duration::from_secs(2),
tokio::task::spawn_blocking(move || {
AsyncHandler::spawn_blocking(move || {
let profiles = Config::profiles();
let data = profiles.latest_ref();
IProfiles {
@@ -102,7 +104,7 @@ pub async fn get_profiles() -> CmdResult<IProfiles> {
"所有获取配置策略都失败尝试fallback"
);
match tokio::task::spawn_blocking(IProfiles::new).await {
match AsyncHandler::spawn_blocking(IProfiles::new).await {
Ok(profiles) => {
logging!(info, Type::Cmd, true, "使用fallback配置成功");
Ok(profiles)
@@ -372,7 +374,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
match file_read_result {
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)
})
.await;

View File

@@ -1,3 +1,5 @@
#[cfg(any(target_os = "windows", target_os = "linux"))]
use crate::process::AsyncHandler;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use tokio::time::{timeout, Duration};
@@ -74,7 +76,7 @@ impl AsyncProxyQuery {
#[cfg(target_os = "windows")]
async fn get_auto_proxy_impl() -> Result<AsyncAutoproxy> {
// Windows: 从注册表读取PAC配置
tokio::task::spawn_blocking(move || -> Result<AsyncAutoproxy> {
AsyncHandler::spawn_blocking(move || -> Result<AsyncAutoproxy> {
Self::get_pac_config_from_registry()
})
.await?
@@ -258,7 +260,7 @@ impl AsyncProxyQuery {
#[cfg(target_os = "windows")]
async fn get_system_proxy_impl() -> Result<AsyncSysproxy> {
// Windows: 使用注册表直接读取代理设置
tokio::task::spawn_blocking(move || -> Result<AsyncSysproxy> {
AsyncHandler::spawn_blocking(move || -> Result<AsyncSysproxy> {
Self::get_system_proxy_from_registry()
})
.await?

View File

@@ -533,7 +533,7 @@ impl CoreManager {
use winapi::um::winnt::HANDLE;
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();
unsafe {
@@ -628,7 +628,7 @@ impl CoreManager {
use winapi::um::processthreadsapi::{OpenProcess, TerminateProcess};
use winapi::um::winnt::{HANDLE, PROCESS_TERMINATE};
tokio::task::spawn_blocking(move || -> bool {
AsyncHandler::spawn_blocking(move || -> bool {
unsafe {
let process_handle: HANDLE = OpenProcess(PROCESS_TERMINATE, 0, pid);
if process_handle.is_null() {
@@ -703,7 +703,7 @@ impl CoreManager {
use winapi::um::processthreadsapi::OpenProcess;
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 {
let process_handle: HANDLE = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
if process_handle.is_null() {

View File

@@ -1,3 +1,5 @@
#[cfg(target_os = "windows")]
use crate::process::AsyncHandler;
use crate::{logging, utils::logging::Type};
use anyhow::{bail, Context, Result};
use hmac::{Hmac, Mac};
@@ -143,7 +145,7 @@ pub async fn send_ipc_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) {
Ok(name) => name,
Err(e) => {

View File

@@ -11,4 +11,12 @@ impl AsyncHandler {
{
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)
}
}