Revert "refactor: Replace std::sync::Mutex with parking_lot::Mutex for improved performance and consistency in multiple files" (#3990)

This commit is contained in:
Dyna
2025-07-05 12:17:02 +08:00
committed by GitHub
parent 6d192233d1
commit 667844aa12
3 changed files with 22 additions and 25 deletions

View File

@@ -8,8 +8,7 @@ pub fn use_script(
name: String, name: String,
) -> Result<(Mapping, Vec<(String, String)>)> { ) -> Result<(Mapping, Vec<(String, String)>)> {
use boa_engine::{native_function::NativeFunction, Context, JsValue, Source}; use boa_engine::{native_function::NativeFunction, Context, JsValue, Source};
use parking_lot::Mutex; use std::sync::{Arc, Mutex};
use std::sync::Arc;
let mut context = Context::default(); let mut context = Context::default();
let outputs = Arc::new(Mutex::new(vec![])); let outputs = Arc::new(Mutex::new(vec![]));
@@ -25,7 +24,7 @@ pub fn use_script(
let level = level.to_std_string().unwrap(); let level = level.to_std_string().unwrap();
let data = args.get(1).unwrap().to_string(context)?; let data = args.get(1).unwrap().to_string(context)?;
let data = data.to_std_string().unwrap(); let data = data.to_std_string().unwrap();
let mut out = copy_outputs.lock(); let mut out = copy_outputs.lock().unwrap();
out.push((level, data)); out.push((level, data));
Ok(JsValue::undefined()) Ok(JsValue::undefined())
}, },
@@ -68,7 +67,7 @@ pub fn use_script(
// 直接解析JSON结果,不做其他解析 // 直接解析JSON结果,不做其他解析
let res: Result<Mapping, Error> = parse_json_safely(&result); let res: Result<Mapping, Error> = parse_json_safely(&result);
let mut out = outputs.lock(); let mut out = outputs.lock().unwrap();
match res { match res {
Ok(config) => Ok((use_lowercase(config), out.to_vec())), Ok(config) => Ok((use_lowercase(config), out.to_vec())),
Err(err) => { Err(err) => {

View File

@@ -13,8 +13,7 @@ use crate::{
utils::{resolve, resolve::resolve_scheme, server}, utils::{resolve, resolve::resolve_scheme, server},
}; };
use config::Config; use config::Config;
use parking_lot::Mutex; use std::sync::{Mutex, Once};
use std::sync::Once;
use tauri::AppHandle; use tauri::AppHandle;
use tauri::Manager; use tauri::Manager;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@@ -42,14 +41,14 @@ impl AppHandleManager {
/// Initialize the app handle manager with an app handle. /// Initialize the app handle manager with an app handle.
pub fn init(&self, handle: AppHandle) { pub fn init(&self, handle: AppHandle) {
self.init.call_once(|| { self.init.call_once(|| {
let mut app_handle = self.inner.lock(); let mut app_handle = self.inner.lock().unwrap();
*app_handle = Some(handle); *app_handle = Some(handle);
}); });
} }
/// Get the app handle if it has been initialized. /// Get the app handle if it has been initialized.
pub fn get(&self) -> Option<AppHandle> { pub fn get(&self) -> Option<AppHandle> {
self.inner.lock().clone() self.inner.lock().unwrap().clone()
} }
/// Get the app handle, panics if it hasn't been initialized. /// Get the app handle, panics if it hasn't been initialized.
@@ -60,7 +59,7 @@ impl AppHandleManager {
pub fn set_activation_policy_regular(&self) { pub fn set_activation_policy_regular(&self) {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
let app_handle = self.inner.lock(); let app_handle = self.inner.lock().unwrap();
let app_handle = app_handle.as_ref().unwrap(); let app_handle = app_handle.as_ref().unwrap();
let _ = app_handle.set_activation_policy(tauri::ActivationPolicy::Regular); let _ = app_handle.set_activation_policy(tauri::ActivationPolicy::Regular);
} }
@@ -69,7 +68,7 @@ impl AppHandleManager {
pub fn set_activation_policy_accessory(&self) { pub fn set_activation_policy_accessory(&self) {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
let app_handle = self.inner.lock(); let app_handle = self.inner.lock().unwrap();
let app_handle = app_handle.as_ref().unwrap(); let app_handle = app_handle.as_ref().unwrap();
let _ = app_handle.set_activation_policy(tauri::ActivationPolicy::Accessory); let _ = app_handle.set_activation_policy(tauri::ActivationPolicy::Accessory);
} }
@@ -78,7 +77,7 @@ impl AppHandleManager {
pub fn set_activation_policy_prohibited(&self) { pub fn set_activation_policy_prohibited(&self) {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
let app_handle = self.inner.lock(); let app_handle = self.inner.lock().unwrap();
let app_handle = app_handle.as_ref().unwrap(); let app_handle = app_handle.as_ref().unwrap();
let _ = app_handle.set_activation_policy(tauri::ActivationPolicy::Prohibited); let _ = app_handle.set_activation_policy(tauri::ActivationPolicy::Prohibited);
} }

View File

@@ -1,9 +1,8 @@
use anyhow::Result; use anyhow::Result;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use parking_lot::Mutex;
use reqwest::{Client, ClientBuilder, Proxy, RequestBuilder, Response}; use reqwest::{Client, ClientBuilder, Proxy, RequestBuilder, Response};
use std::{ use std::{
sync::{Arc, Once}, sync::{Arc, Mutex, Once},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use tokio::runtime::{Builder, Runtime}; use tokio::runtime::{Builder, Runtime};
@@ -79,7 +78,7 @@ impl NetworkManager {
.build() .build()
.expect("Failed to build no_proxy client"); .expect("Failed to build no_proxy client");
let mut no_proxy_guard = NETWORK_MANAGER.no_proxy_client.lock(); let mut no_proxy_guard = NETWORK_MANAGER.no_proxy_client.lock().unwrap();
*no_proxy_guard = Some(no_proxy_client); *no_proxy_guard = Some(no_proxy_client);
logging!(info, Type::Network, true, "网络管理器初始化完成"); logging!(info, Type::Network, true, "网络管理器初始化完成");
@@ -88,16 +87,16 @@ impl NetworkManager {
} }
fn record_connection_error(&self, error: &str) { fn record_connection_error(&self, error: &str) {
let mut last_error = self.last_connection_error.lock(); let mut last_error = self.last_connection_error.lock().unwrap();
*last_error = Some((Instant::now(), error.to_string())); *last_error = Some((Instant::now(), error.to_string()));
let mut error_count = self.connection_error_count.lock(); let mut error_count = self.connection_error_count.lock().unwrap();
*error_count += 1; *error_count += 1;
} }
fn should_reset_clients(&self) -> bool { fn should_reset_clients(&self) -> bool {
let error_count = *self.connection_error_count.lock(); let error_count = *self.connection_error_count.lock().unwrap();
let last_error = self.last_connection_error.lock(); let last_error = self.last_connection_error.lock().unwrap();
if error_count > 5 { if error_count > 5 {
return true; return true;
@@ -115,19 +114,19 @@ impl NetworkManager {
pub fn reset_clients(&self) { pub fn reset_clients(&self) {
logging!(info, Type::Network, true, "正在重置所有HTTP客户端"); logging!(info, Type::Network, true, "正在重置所有HTTP客户端");
{ {
let mut client = self.self_proxy_client.lock(); let mut client = self.self_proxy_client.lock().unwrap();
*client = None; *client = None;
} }
{ {
let mut client = self.system_proxy_client.lock(); let mut client = self.system_proxy_client.lock().unwrap();
*client = None; *client = None;
} }
{ {
let mut client = self.no_proxy_client.lock(); let mut client = self.no_proxy_client.lock().unwrap();
*client = None; *client = None;
} }
{ {
let mut error_count = self.connection_error_count.lock(); let mut error_count = self.connection_error_count.lock().unwrap();
*error_count = 0; *error_count = 0;
} }
} }
@@ -138,7 +137,7 @@ impl NetworkManager {
self.reset_clients(); self.reset_clients();
} }
let mut client_guard = self.self_proxy_client.lock(); let mut client_guard = self.self_proxy_client.lock().unwrap();
if client_guard.is_none() { if client_guard.is_none() {
let port = Config::verge() let port = Config::verge()
@@ -186,7 +185,7 @@ impl NetworkManager {
self.reset_clients(); self.reset_clients();
} }
let mut client_guard = self.system_proxy_client.lock(); let mut client_guard = self.system_proxy_client.lock().unwrap();
if client_guard.is_none() { if client_guard.is_none() {
use sysproxy::Sysproxy; use sysproxy::Sysproxy;
@@ -233,7 +232,7 @@ impl NetworkManager {
pub fn get_client(&self, proxy_type: ProxyType) -> Client { pub fn get_client(&self, proxy_type: ProxyType) -> Client {
match proxy_type { match proxy_type {
ProxyType::NoProxy => { ProxyType::NoProxy => {
let client_guard = self.no_proxy_client.lock(); let client_guard = self.no_proxy_client.lock().unwrap();
client_guard.as_ref().unwrap().clone() client_guard.as_ref().unwrap().clone()
} }
ProxyType::SelfProxy => self.get_or_create_self_proxy_client(), ProxyType::SelfProxy => self.get_or_create_self_proxy_client(),