mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-01 10:40:14 +08:00
refactor(backend): share single-owner drivers for background tasks (#8759)
* docs(backend): define background task ownership * refactor(backend): give background operations single task owners * test(backend): remove actor migration regression cases * docs(changelog): drop backend actor migration entry * refactor(backend): extract workspace task drivers * fix(timer): omit profile identifier from panic log * docs(backend): define workspace crate boundaries * refactor(lightweight): use the task countdown driver * refactor(lightweight): name countdown callbacks * refactor(task): simplify driver startup and coalescing requests * refactor(core): bind service handoff to the sidecar generation * refactor(tun): coalesce background run state reconciliation * refactor(core): own sidecar exit resources in one session * refactor(core): remove automatic Windows service handoff * refactor(task): simplify coalescing job signals * refactor(runstate): keep operation status with service state * refactor(dns): coalesce background override synchronization * refactor(sysproxy): own the local guard under its operation lock * fix(tray): read menu settings at publication * fix(dns): retain script lock across caller cancellation * refactor(profiles): own selected node restoration in one task * refactor(timer): own subscription schedules in the scheduler * test(timer): remove the added application test * fix(service): renew provider sync retry budget on refresh
This commit is contained in:
@@ -212,22 +212,9 @@ impl Tray {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let verge = Config::verge().await.latest_arc();
|
||||
let system_proxy = verge.enable_system_proxy.unwrap_or(false);
|
||||
let tun_mode = verge.enable_tun_mode.unwrap_or(false);
|
||||
let mode = {
|
||||
Config::clash()
|
||||
.await
|
||||
.latest_arc()
|
||||
.0
|
||||
.get("mode")
|
||||
.map_or("rule", |val| val.as_str().unwrap_or("rule"))
|
||||
.to_owned()
|
||||
};
|
||||
let is_lightweight_mode = is_in_lightweight_mode();
|
||||
|
||||
let verge = Config::verge().await;
|
||||
let fetch_proxy_groups =
|
||||
include_proxy_groups && verge.tray_proxy_groups_display_mode.as_deref() != Some("disable");
|
||||
include_proxy_groups && verge.latest_arc().tray_proxy_groups_display_mode.as_deref() != Some("disable");
|
||||
|
||||
// TODO: should update tray menu again when it was timeout error
|
||||
let (proxy_nodes_data, runtime_proxy_groups_order) = if fetch_proxy_groups {
|
||||
@@ -257,29 +244,28 @@ impl Tray {
|
||||
(None, None)
|
||||
};
|
||||
|
||||
let verge = if fetch_proxy_groups {
|
||||
Config::verge().await.latest_arc()
|
||||
} else {
|
||||
verge
|
||||
};
|
||||
let profiles = Config::profiles().await.latest_arc();
|
||||
let clash = Config::clash().await;
|
||||
let profiles = Config::profiles().await;
|
||||
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
let dispatch_handle = app_handle.clone();
|
||||
app_handle
|
||||
.run_on_main_thread(move || {
|
||||
let _ = tx.send((|| -> Result<()> {
|
||||
let verge = verge.latest_arc();
|
||||
let clash = clash.latest_arc();
|
||||
let mode = clash.0.get("mode").and_then(|mode| mode.as_str()).unwrap_or("rule");
|
||||
let menu = build_tray_menu(
|
||||
&dispatch_handle,
|
||||
TrayMenuOptions {
|
||||
mode: Some(mode.as_str()),
|
||||
system_proxy_enabled: system_proxy,
|
||||
tun_mode_enabled: tun_mode,
|
||||
is_lightweight_mode,
|
||||
mode: Some(mode),
|
||||
system_proxy_enabled: verge.enable_system_proxy.unwrap_or(false),
|
||||
tun_mode_enabled: verge.enable_tun_mode.unwrap_or(false),
|
||||
is_lightweight_mode: is_in_lightweight_mode(),
|
||||
include_proxy_groups,
|
||||
},
|
||||
&verge,
|
||||
&profiles,
|
||||
&profiles.latest_arc(),
|
||||
runtime_proxy_groups_order,
|
||||
proxy_nodes_data,
|
||||
)?;
|
||||
|
||||
@@ -2,28 +2,18 @@ use crate::core::handle;
|
||||
use crate::process::AsyncHandler;
|
||||
use crate::utils::{connections_stream, tray_speed};
|
||||
use crate::{Type, logging};
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
use std::time::Duration;
|
||||
use tauri::async_runtime::JoinHandle;
|
||||
use tauri_plugin_mihomo::models::WsConnectionId;
|
||||
use tokio::sync::watch;
|
||||
|
||||
use connections_stream::{MihomoWsEventStream, StreamConsumeState, TrafficSpeedEvent};
|
||||
|
||||
const TRAY_SPEED_RETRY_DELAY: Duration = Duration::from_secs(1);
|
||||
const TRAY_SPEED_STALE_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Default)]
|
||||
pub struct TraySpeedController {
|
||||
speed_task: Arc<Mutex<Option<JoinHandle<()>>>>,
|
||||
speed_connection_id: Arc<Mutex<Option<WsConnectionId>>>,
|
||||
}
|
||||
|
||||
impl Default for TraySpeedController {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
speed_task: Arc::new(Mutex::new(None)),
|
||||
speed_connection_id: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
enabled: OnceLock<watch::Sender<bool>>,
|
||||
}
|
||||
|
||||
impl TraySpeedController {
|
||||
@@ -32,104 +22,102 @@ impl TraySpeedController {
|
||||
}
|
||||
|
||||
pub fn update_task(&self, enable_tray_speed: bool) {
|
||||
if enable_tray_speed {
|
||||
self.start_task();
|
||||
} else {
|
||||
self.stop_task();
|
||||
let enabled = self.enabled.get_or_init(|| {
|
||||
let (enabled, receiver) = watch::channel(false);
|
||||
AsyncHandler::spawn(move || Self::run(receiver));
|
||||
enabled
|
||||
});
|
||||
enabled.send_replace(enable_tray_speed);
|
||||
}
|
||||
|
||||
async fn run(mut enabled: watch::Receiver<bool>) {
|
||||
while enabled.changed().await.is_ok() {
|
||||
let should_run = *enabled.borrow_and_update();
|
||||
if should_run {
|
||||
Self::run_enabled(&mut enabled).await;
|
||||
} else {
|
||||
Self::clear_tray_speed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn start_task(&self) {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
return;
|
||||
}
|
||||
async fn run_enabled(enabled: &mut watch::Receiver<bool>) {
|
||||
while !handle::Handle::global().is_exiting() {
|
||||
if !Self::has_main_tray() {
|
||||
logging!(warn, Type::Tray, "托盘不可用,停止托盘速率任务");
|
||||
return;
|
||||
}
|
||||
|
||||
if !Self::has_main_tray() {
|
||||
logging!(warn, Type::Tray, "托盘不可用,跳过启动托盘速率任务");
|
||||
return;
|
||||
}
|
||||
|
||||
let mut guard = self.speed_task.lock();
|
||||
if guard.as_ref().is_some_and(|task| !task.inner().is_finished()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let speed_connection_id = Arc::clone(&self.speed_connection_id);
|
||||
let task = AsyncHandler::spawn(move || async move {
|
||||
loop {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
break;
|
||||
}
|
||||
|
||||
if !Self::has_main_tray() {
|
||||
logging!(warn, Type::Tray, "托盘已不可用,停止托盘速率任务");
|
||||
break;
|
||||
}
|
||||
|
||||
let stream_connect_result = connections_stream::connect_traffic_stream().await;
|
||||
let mut speed_stream = match stream_connect_result {
|
||||
Ok(stream) => stream,
|
||||
Err(err) => {
|
||||
logging!(debug, Type::Tray, "托盘速率流连接失败,稍后重试: {err}");
|
||||
Self::apply_tray_speed(0, 0);
|
||||
tokio::time::sleep(TRAY_SPEED_RETRY_DELAY).await;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
Self::set_speed_connection_id(&speed_connection_id, Some(speed_stream.connection_id));
|
||||
|
||||
loop {
|
||||
let next_state = speed_stream
|
||||
.next_event(TRAY_SPEED_STALE_TIMEOUT, || handle::Handle::global().is_exiting())
|
||||
.await;
|
||||
|
||||
match next_state {
|
||||
connections_stream::StreamConsumeState::Event(speed_event) => {
|
||||
Self::apply_tray_speed(speed_event.up, speed_event.down);
|
||||
}
|
||||
connections_stream::StreamConsumeState::Stale => {
|
||||
logging!(debug, Type::Tray, "托盘速率流长时间未收到有效数据,触发重连");
|
||||
Self::apply_tray_speed(0, 0);
|
||||
break;
|
||||
}
|
||||
connections_stream::StreamConsumeState::Closed
|
||||
| connections_stream::StreamConsumeState::ExitRequested => {
|
||||
break;
|
||||
}
|
||||
// Keep the registered stream outside the cancellable future until it is disconnected.
|
||||
let mut stream = None;
|
||||
let disabled = tokio::select! {
|
||||
biased;
|
||||
_ = Self::wait_until_disabled(enabled) => true,
|
||||
_ = Self::read_speed(&mut stream) => false,
|
||||
};
|
||||
if disabled {
|
||||
Self::clear_tray_speed();
|
||||
}
|
||||
if let Some(stream) = stream {
|
||||
let disconnect = connections_stream::disconnect_connection(stream.connection_id);
|
||||
tokio::pin!(disconnect);
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = Self::wait_until_disabled(enabled), if !disabled => {
|
||||
Self::clear_tray_speed();
|
||||
disconnect.await;
|
||||
return;
|
||||
}
|
||||
_ = &mut disconnect => {}
|
||||
}
|
||||
}
|
||||
if disabled || handle::Handle::global().is_exiting() || !Self::has_main_tray() {
|
||||
return;
|
||||
}
|
||||
|
||||
Self::disconnect_speed_connection(&speed_connection_id).await;
|
||||
|
||||
if handle::Handle::global().is_exiting() || !Self::has_main_tray() {
|
||||
break;
|
||||
}
|
||||
|
||||
// `Stale` already reset the display; this covers remote close.
|
||||
if *enabled.borrow() {
|
||||
Self::apply_tray_speed(0, 0);
|
||||
tokio::time::sleep(TRAY_SPEED_RETRY_DELAY).await;
|
||||
}
|
||||
|
||||
Self::set_speed_connection_id(&speed_connection_id, None);
|
||||
});
|
||||
|
||||
*guard = Some(task);
|
||||
if tokio::time::timeout(TRAY_SPEED_RETRY_DELAY, Self::wait_until_disabled(enabled))
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
Self::clear_tray_speed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn stop_task(&self) {
|
||||
let task = self.speed_task.lock().take();
|
||||
let speed_connection_id = Arc::clone(&self.speed_connection_id);
|
||||
async fn wait_until_disabled(enabled: &mut watch::Receiver<bool>) {
|
||||
let _ = enabled.wait_for(|enabled| !enabled).await;
|
||||
}
|
||||
|
||||
AsyncHandler::spawn(move || async move {
|
||||
// Await abort before disconnect so the task cannot consume and lose the connection ID.
|
||||
if let Some(task) = task {
|
||||
task.abort();
|
||||
let _ = task.await;
|
||||
async fn read_speed(stream: &mut Option<MihomoWsEventStream<TrafficSpeedEvent>>) {
|
||||
let speed_stream = match connections_stream::connect_traffic_stream().await {
|
||||
Ok(connected) => stream.insert(connected),
|
||||
Err(err) => {
|
||||
logging!(debug, Type::Tray, "托盘速率流连接失败,稍后重试: {err}");
|
||||
return;
|
||||
}
|
||||
Self::disconnect_speed_connection(&speed_connection_id).await;
|
||||
});
|
||||
};
|
||||
|
||||
loop {
|
||||
match speed_stream
|
||||
.next_event(TRAY_SPEED_STALE_TIMEOUT, || handle::Handle::global().is_exiting())
|
||||
.await
|
||||
{
|
||||
StreamConsumeState::Event(event) => Self::apply_tray_speed(event.up, event.down),
|
||||
StreamConsumeState::Stale => {
|
||||
logging!(debug, Type::Tray, "托盘速率流长时间未收到有效数据,触发重连");
|
||||
Self::apply_tray_speed(0, 0);
|
||||
return;
|
||||
}
|
||||
StreamConsumeState::Closed | StreamConsumeState::ExitRequested => return,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_tray_speed() {
|
||||
let app_handle = handle::Handle::app_handle();
|
||||
if let Some(tray) = app_handle.tray_by_id(super::TRAY_ID) {
|
||||
let result = tray.with_inner_tray_icon(|inner| {
|
||||
@@ -147,23 +135,6 @@ impl TraySpeedController {
|
||||
handle::Handle::app_handle().tray_by_id(super::TRAY_ID).is_some()
|
||||
}
|
||||
|
||||
fn set_speed_connection_id(
|
||||
speed_connection_id: &Arc<Mutex<Option<WsConnectionId>>>,
|
||||
connection_id: Option<WsConnectionId>,
|
||||
) {
|
||||
*speed_connection_id.lock() = connection_id;
|
||||
}
|
||||
|
||||
fn take_speed_connection_id(speed_connection_id: &Arc<Mutex<Option<WsConnectionId>>>) -> Option<WsConnectionId> {
|
||||
speed_connection_id.lock().take()
|
||||
}
|
||||
|
||||
async fn disconnect_speed_connection(speed_connection_id: &Arc<Mutex<Option<WsConnectionId>>>) {
|
||||
if let Some(connection_id) = Self::take_speed_connection_id(speed_connection_id) {
|
||||
connections_stream::disconnect_connection(connection_id).await;
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_tray_speed(up: u64, down: u64) {
|
||||
let app_handle = handle::Handle::app_handle();
|
||||
if let Some(tray) = app_handle.tray_by_id(super::TRAY_ID) {
|
||||
|
||||
Reference in New Issue
Block a user