mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
refactor: optimize timer and network management with atomic operations
This commit is contained in:
@@ -2,7 +2,10 @@ use anyhow::Result;
|
||||
use parking_lot::Mutex;
|
||||
use reqwest::{Client, ClientBuilder, Proxy, RequestBuilder, Response};
|
||||
use std::{
|
||||
sync::{Arc, Once},
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc, Once,
|
||||
},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tokio::runtime::{Builder, Runtime};
|
||||
@@ -23,12 +26,12 @@ const POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
/// 网络管理器
|
||||
pub struct NetworkManager {
|
||||
runtime: Arc<Runtime>,
|
||||
self_proxy_client: Arc<Mutex<Option<Client>>>,
|
||||
system_proxy_client: Arc<Mutex<Option<Client>>>,
|
||||
no_proxy_client: Arc<Mutex<Option<Client>>>,
|
||||
self_proxy_client: Mutex<Option<Client>>,
|
||||
system_proxy_client: Mutex<Option<Client>>,
|
||||
no_proxy_client: Mutex<Option<Client>>,
|
||||
init: Once,
|
||||
last_connection_error: Arc<Mutex<Option<(Instant, String)>>>,
|
||||
connection_error_count: Arc<Mutex<usize>>,
|
||||
last_connection_error: Mutex<Option<(Instant, String)>>,
|
||||
connection_error_count: AtomicUsize,
|
||||
}
|
||||
|
||||
// Use singleton_lazy macro to replace lazy_static!
|
||||
@@ -47,12 +50,12 @@ impl NetworkManager {
|
||||
|
||||
NetworkManager {
|
||||
runtime: Arc::new(runtime),
|
||||
self_proxy_client: Arc::new(Mutex::new(None)),
|
||||
system_proxy_client: Arc::new(Mutex::new(None)),
|
||||
no_proxy_client: Arc::new(Mutex::new(None)),
|
||||
self_proxy_client: Mutex::new(None),
|
||||
system_proxy_client: Mutex::new(None),
|
||||
no_proxy_client: Mutex::new(None),
|
||||
init: Once::new(),
|
||||
last_connection_error: Arc::new(Mutex::new(None)),
|
||||
connection_error_count: Arc::new(Mutex::new(0)),
|
||||
last_connection_error: Mutex::new(None),
|
||||
connection_error_count: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,12 +88,11 @@ impl NetworkManager {
|
||||
let mut last_error = self.last_connection_error.lock();
|
||||
*last_error = Some((Instant::now(), error.to_string()));
|
||||
|
||||
let mut error_count = self.connection_error_count.lock();
|
||||
*error_count += 1;
|
||||
self.connection_error_count.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
fn should_reset_clients(&self) -> bool {
|
||||
let error_count = *self.connection_error_count.lock();
|
||||
let error_count = self.connection_error_count.load(Ordering::Relaxed);
|
||||
let last_error = self.last_connection_error.lock();
|
||||
|
||||
if error_count > 5 {
|
||||
@@ -120,10 +122,7 @@ impl NetworkManager {
|
||||
let mut client = self.no_proxy_client.lock();
|
||||
*client = None;
|
||||
}
|
||||
{
|
||||
let mut error_count = self.connection_error_count.lock();
|
||||
*error_count = 0;
|
||||
}
|
||||
self.connection_error_count.store(0, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// 创建带有自定义选项的HTTP请求
|
||||
|
||||
@@ -15,10 +15,7 @@ use parking_lot::{Mutex, RwLock};
|
||||
use percent_encoding::percent_decode_str;
|
||||
use scopeguard;
|
||||
use serde_yaml::Mapping;
|
||||
use std::{
|
||||
sync::Arc,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
use tauri::{AppHandle, Manager};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
@@ -33,7 +30,7 @@ const DEFAULT_WIDTH: u32 = 940;
|
||||
const DEFAULT_HEIGHT: u32 = 700;
|
||||
|
||||
// 添加全局UI准备就绪标志
|
||||
static UI_READY: OnceCell<Arc<RwLock<bool>>> = OnceCell::new();
|
||||
static UI_READY: OnceCell<RwLock<bool>> = OnceCell::new();
|
||||
|
||||
// 窗口创建锁,防止并发创建窗口
|
||||
static WINDOW_CREATING: OnceCell<Mutex<(bool, Instant)>> = OnceCell::new();
|
||||
@@ -63,18 +60,18 @@ impl Default for UiReadyState {
|
||||
}
|
||||
|
||||
// 获取UI就绪状态细节
|
||||
static UI_READY_STATE: OnceCell<Arc<UiReadyState>> = OnceCell::new();
|
||||
static UI_READY_STATE: OnceCell<UiReadyState> = OnceCell::new();
|
||||
|
||||
fn get_window_creating_lock() -> &'static Mutex<(bool, Instant)> {
|
||||
WINDOW_CREATING.get_or_init(|| Mutex::new((false, Instant::now())))
|
||||
}
|
||||
|
||||
fn get_ui_ready() -> &'static Arc<RwLock<bool>> {
|
||||
UI_READY.get_or_init(|| Arc::new(RwLock::new(false)))
|
||||
fn get_ui_ready() -> &'static RwLock<bool> {
|
||||
UI_READY.get_or_init(|| RwLock::new(false))
|
||||
}
|
||||
|
||||
fn get_ui_ready_state() -> &'static Arc<UiReadyState> {
|
||||
UI_READY_STATE.get_or_init(|| Arc::new(UiReadyState::default()))
|
||||
fn get_ui_ready_state() -> &'static UiReadyState {
|
||||
UI_READY_STATE.get_or_init(UiReadyState::default)
|
||||
}
|
||||
|
||||
// 更新UI准备阶段
|
||||
|
||||
Reference in New Issue
Block a user