refactor: clean up imports and remove unused initialization in NetworkManager

This commit is contained in:
Tunglies
2025-09-02 23:48:27 +08:00
parent 40f87c834d
commit b989aeb7b0
3 changed files with 12 additions and 23 deletions

View File

@@ -6,6 +6,11 @@ use kode_bridge::{
}; };
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use crate::{
logging, singleton_with_logging,
utils::{dirs::ipc_path, logging::Type},
};
// 定义用于URL路径的编码集合只编码真正必要的字符 // 定义用于URL路径的编码集合只编码真正必要的字符
const URL_PATH_ENCODE_SET: &AsciiSet = &CONTROLS const URL_PATH_ENCODE_SET: &AsciiSet = &CONTROLS
.add(b' ') // 空格 .add(b' ') // 空格
@@ -15,8 +20,6 @@ const URL_PATH_ENCODE_SET: &AsciiSet = &CONTROLS
.add(b'&') // 和号 .add(b'&') // 和号
.add(b'%'); // 百分号 .add(b'%'); // 百分号
use crate::{logging, singleton_with_logging, utils::dirs::ipc_path};
// Helper function to create AnyError from string // Helper function to create AnyError from string
fn create_error(msg: impl Into<String>) -> AnyError { fn create_error(msg: impl Into<String>) -> AnyError {
Box::new(std::io::Error::other(msg.into())) Box::new(std::io::Error::other(msg.into()))
@@ -29,13 +32,7 @@ pub struct IpcManager {
impl IpcManager { impl IpcManager {
fn new() -> Self { fn new() -> Self {
let ipc_path_buf = ipc_path().unwrap_or_else(|e| { let ipc_path_buf = ipc_path().unwrap_or_else(|e| {
logging!( logging!(error, Type::Ipc, true, "Failed to get IPC path: {}", e);
error,
crate::utils::logging::Type::Ipc,
true,
"Failed to get IPC path: {}",
e
);
std::path::PathBuf::from("/tmp/clash-verge-ipc") // fallback path std::path::PathBuf::from("/tmp/clash-verge-ipc") // fallback path
}); });
let ipc_path = ipc_path_buf.to_str().unwrap_or_default(); let ipc_path = ipc_path_buf.to_str().unwrap_or_default();
@@ -48,9 +45,9 @@ impl IpcManager {
max_requests_per_second: Some(64.0), max_requests_per_second: Some(64.0),
..Default::default() ..Default::default()
}; };
Self { #[allow(clippy::unwrap_used)]
client: IpcHttpClient::with_config(ipc_path, config).unwrap(), let client = IpcHttpClient::with_config(ipc_path, config).unwrap();
} Self { client }
} }
} }

View File

@@ -8,7 +8,6 @@ use isahc::{
}, },
}; };
use isahc::{config::SslOption, HttpClient}; use isahc::{config::SslOption, HttpClient};
use std::sync::Once;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use sysproxy::Sysproxy; use sysproxy::Sysproxy;
use tokio::sync::Mutex; use tokio::sync::Mutex;
@@ -56,7 +55,6 @@ pub struct NetworkManager {
self_proxy_client: Mutex<Option<HttpClient>>, self_proxy_client: Mutex<Option<HttpClient>>,
system_proxy_client: Mutex<Option<HttpClient>>, system_proxy_client: Mutex<Option<HttpClient>>,
no_proxy_client: Mutex<Option<HttpClient>>, no_proxy_client: Mutex<Option<HttpClient>>,
init: Once,
last_connection_error: Mutex<Option<(Instant, String)>>, last_connection_error: Mutex<Option<(Instant, String)>>,
connection_error_count: Mutex<usize>, connection_error_count: Mutex<usize>,
} }
@@ -67,16 +65,11 @@ impl NetworkManager {
self_proxy_client: Mutex::new(None), self_proxy_client: Mutex::new(None),
system_proxy_client: Mutex::new(None), system_proxy_client: Mutex::new(None),
no_proxy_client: Mutex::new(None), no_proxy_client: Mutex::new(None),
init: Once::new(),
last_connection_error: Mutex::new(None), last_connection_error: Mutex::new(None),
connection_error_count: Mutex::new(0), connection_error_count: Mutex::new(0),
} }
} }
pub fn init(&self) {
self.init.call_once(|| {});
}
async fn record_connection_error(&self, error: &str) { async fn record_connection_error(&self, error: &str) {
let mut last_error = self.last_connection_error.lock().await; let mut last_error = self.last_connection_error.lock().await;
*last_error = Some((Instant::now(), error.to_string())); *last_error = Some((Instant::now(), error.to_string()));

View File

@@ -7,7 +7,7 @@ use crate::{
logging, logging_error, logging, logging_error,
module::lightweight::auto_lightweight_mode_init, module::lightweight::auto_lightweight_mode_init,
process::AsyncHandler, process::AsyncHandler,
utils::{init, logging::Type, network::NetworkManager, resolve::window::create_window, server}, utils::{init, logging::Type, resolve::window::create_window, server},
}; };
pub mod dns; pub mod dns;
@@ -22,9 +22,8 @@ pub fn resolve_setup_handle(app_handle: AppHandle) {
pub fn resolve_setup_sync() { pub fn resolve_setup_sync() {
AsyncHandler::spawn(|| async { AsyncHandler::spawn(|| async {
AsyncHandler::spawn_blocking(|| init_scheme()); AsyncHandler::spawn_blocking(init_scheme);
AsyncHandler::spawn_blocking(|| init_embed_server()); AsyncHandler::spawn_blocking(init_embed_server);
AsyncHandler::spawn_blocking(|| NetworkManager::new().init());
}); });
} }