From 6ebdaaa7ff11743951f62284720f7a3582d13c36 Mon Sep 17 00:00:00 2001 From: "Christine." Date: Wed, 20 May 2026 14:21:24 +0800 Subject: [PATCH] fix: switch node slowly on Windows (#7052) --- Cargo.lock | 47 +++++++++++++++++++++-- src-tauri/Cargo.toml | 2 +- src-tauri/src/core/tray/speed_task.rs | 14 +++---- src-tauri/src/lib.rs | 10 +++++ src-tauri/src/utils/connections_stream.rs | 6 +-- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b10a40976..2069ee539 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1665,6 +1665,19 @@ dependencies = [ "once_cell", ] +[[package]] +name = "crossbeam" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + [[package]] name = "crossbeam-channel" version = "0.5.15" @@ -1693,6 +1706,15 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-queue" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.21" @@ -3560,7 +3582,7 @@ dependencies = [ "tokio", "tower-service", "tracing", - "windows-registry", + "windows-registry 0.6.1", ] [[package]] @@ -7785,7 +7807,7 @@ dependencies = [ "thiserror 2.0.18", "tracing", "url", - "windows-registry", + "windows-registry 0.5.3", "windows-result 0.3.4", ] @@ -7899,12 +7921,18 @@ dependencies = [ [[package]] name = "tauri-plugin-mihomo" -version = "0.1.9" -source = "git+https://github.com/clash-verge-rev/tauri-plugin-mihomo#a185fe1a8802367d0b3d50d402cb823759d2c57e" +version = "0.1.5" +source = "git+https://github.com/clash-verge-rev/tauri-plugin-mihomo?branch=revert#1cc80bc0fbe1245315617f4cecd93710a152325b" dependencies = [ + "base64 0.22.1", + "bytes", + "crossbeam", "futures-util", "http 1.4.0", + "http-body-util", "httparse", + "hyper 1.9.0", + "libc", "log", "pin-project", "rand 0.9.4", @@ -9803,6 +9831,17 @@ dependencies = [ "windows-strings 0.4.2", ] +[[package]] +name = "windows-registry" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" +dependencies = [ + "windows-link 0.2.1", + "windows-result 0.4.1", + "windows-strings 0.5.1", +] + [[package]] name = "windows-result" version = "0.3.4" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index c3bd14f0d..ee0ae1bae 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -95,7 +95,7 @@ backon = { version = "1.6.0", features = ["tokio-sleep"] } tauri-plugin-http = "2.5.7" console-subscriber = { version = "0.5.0", optional = true } tauri-plugin-devtools = { version = "2.0.1" } -tauri-plugin-mihomo = { git = "https://github.com/clash-verge-rev/tauri-plugin-mihomo" } +tauri-plugin-mihomo = { git = "https://github.com/clash-verge-rev/tauri-plugin-mihomo", branch = "revert" } clash_verge_logger = { git = "https://github.com/clash-verge-rev/clash-verge-logger" } async-trait = "0.1.89" clash_verge_service_ipc = { version = "2.3.0", features = [ diff --git a/src-tauri/src/core/tray/speed_task.rs b/src-tauri/src/core/tray/speed_task.rs index 1923f92e1..62be15c96 100644 --- a/src-tauri/src/core/tray/speed_task.rs +++ b/src-tauri/src/core/tray/speed_task.rs @@ -6,7 +6,7 @@ use parking_lot::Mutex; use std::sync::Arc; use std::time::Duration; use tauri::async_runtime::JoinHandle; -use tauri_plugin_mihomo::models::WebSocketConnectionId; +use tauri_plugin_mihomo::models::ConnectionId; /// 托盘速率流异常后的重连间隔。 const TRAY_SPEED_RETRY_DELAY: Duration = Duration::from_secs(1); @@ -19,7 +19,7 @@ const TRAY_SPEED_STALE_TIMEOUT: Duration = Duration::from_secs(5); #[derive(Clone)] pub struct TraySpeedController { speed_task: Arc>>>, - speed_connection_id: Arc>>, + speed_connection_id: Arc>>, } impl Default for TraySpeedController { @@ -162,19 +162,17 @@ impl TraySpeedController { } fn set_speed_connection_id( - speed_connection_id: &Arc>>, - connection_id: Option, + speed_connection_id: &Arc>>, + connection_id: Option, ) { *speed_connection_id.lock() = connection_id; } - fn take_speed_connection_id( - speed_connection_id: &Arc>>, - ) -> Option { + fn take_speed_connection_id(speed_connection_id: &Arc>>) -> Option { speed_connection_id.lock().take() } - async fn disconnect_speed_connection(speed_connection_id: &Arc>>) { + async fn disconnect_speed_connection(speed_connection_id: &Arc>>) { if let Some(connection_id) = Self::take_speed_connection_id(speed_connection_id) { connections_stream::disconnect_connection(connection_id).await; } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e38a6ed2a..17f585cde 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -24,6 +24,7 @@ use tauri::{AppHandle, Manager as _}; #[cfg(target_os = "macos")] use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_deep_link::DeepLinkExt as _; +use tauri_plugin_mihomo::RejectPolicy; pub static APP_HANDLE: OnceCell = OnceCell::new(); /// Application initialization helper functions @@ -58,6 +59,15 @@ mod app_init { tauri_plugin_mihomo::Builder::new() .protocol(tauri_plugin_mihomo::models::Protocol::LocalSocket) .socket_path(crate::config::IClashTemp::guard_external_controller_ipc()) + .pool_config( + tauri_plugin_mihomo::IpcPoolConfigBuilder::new() + .min_connections(3) + .max_connections(32) + .idle_timeout(std::time::Duration::from_secs(60)) + .health_check_interval(std::time::Duration::from_secs(60)) + .reject_policy(RejectPolicy::Wait) + .build(), + ) .build(), ); diff --git a/src-tauri/src/utils/connections_stream.rs b/src-tauri/src/utils/connections_stream.rs index 8e6f8e864..412517c9d 100644 --- a/src-tauri/src/utils/connections_stream.rs +++ b/src-tauri/src/utils/connections_stream.rs @@ -3,7 +3,7 @@ use anyhow::Result; use serde::Deserialize; use serde_json::Value; use std::time::Duration; -use tauri_plugin_mihomo::models::{WebSocketConnectionId, WebSocketMessage}; +use tauri_plugin_mihomo::models::{ConnectionId, WebSocketMessage}; use tokio::sync::mpsc; use tokio::time::Instant; @@ -39,7 +39,7 @@ enum InternalWsEvent { /// Mihomo WebSocket 订阅句柄(通用事件流)。 pub struct MihomoWsEventStream { /// 当前订阅连接 ID,用于主动断开。 - pub connection_id: WebSocketConnectionId, + pub connection_id: ConnectionId, /// 当前订阅消息接收器。 receiver: mpsc::Receiver>, /// 最近一次收到有效事件的时间戳。 @@ -162,7 +162,7 @@ impl MihomoWsEventStream { /// /// # Arguments /// * `connection_id` - 目标连接 ID -pub async fn disconnect_connection(connection_id: WebSocketConnectionId) { +pub async fn disconnect_connection(connection_id: ConnectionId) { if let Err(err) = handle::Handle::mihomo() .await .disconnect(connection_id, Some(MIHOMO_WS_STREAM_CLOSE_CODE))