mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
refactor: streamline config handling and logging mechanisms
This commit is contained in:
@@ -10,7 +10,6 @@ use anyhow::{Result, anyhow};
|
||||
use smartstring::alias::String;
|
||||
use std::{path::PathBuf, time::Instant};
|
||||
use tauri_plugin_mihomo::Error as MihomoError;
|
||||
use tokio::time::sleep;
|
||||
|
||||
impl CoreManager {
|
||||
pub async fn use_default_config(&self, error_key: &str, error_msg: &str) -> Result<()> {
|
||||
@@ -37,25 +36,25 @@ impl CoreManager {
|
||||
return Ok((true, String::new()));
|
||||
}
|
||||
|
||||
if !self.should_update_config()? {
|
||||
if !self.should_update_config() {
|
||||
return Ok((true, String::new()));
|
||||
}
|
||||
|
||||
self.perform_config_update().await
|
||||
}
|
||||
|
||||
fn should_update_config(&self) -> Result<bool> {
|
||||
fn should_update_config(&self) -> bool {
|
||||
let now = Instant::now();
|
||||
let last = self.get_last_update();
|
||||
|
||||
if let Some(last_time) = last
|
||||
&& now.duration_since(*last_time) < timing::CONFIG_UPDATE_DEBOUNCE
|
||||
{
|
||||
return Ok(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
self.set_last_update(now);
|
||||
Ok(true)
|
||||
true
|
||||
}
|
||||
|
||||
async fn perform_config_update(&self) -> Result<(bool, String)> {
|
||||
@@ -78,22 +77,14 @@ impl CoreManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn put_configs_force(&self, path: PathBuf) -> Result<()> {
|
||||
self.apply_config(path).await
|
||||
}
|
||||
|
||||
pub(super) async fn apply_config(&self, path: PathBuf) -> Result<()> {
|
||||
let path_str = dirs::path_to_str(&path)?;
|
||||
|
||||
match self.reload_config(path_str).await {
|
||||
async fn apply_config(&self, path: PathBuf) -> Result<()> {
|
||||
let path = dirs::path_to_str(&path)?;
|
||||
match self.reload_config(path).await {
|
||||
Ok(_) => {
|
||||
Config::runtime().await.apply();
|
||||
logging!(info, Type::Core, "Configuration applied");
|
||||
Ok(())
|
||||
}
|
||||
Err(err) if Self::should_restart_on_error(&err) => {
|
||||
self.retry_with_restart(path_str).await
|
||||
}
|
||||
Err(err) => {
|
||||
Config::runtime().await.discard();
|
||||
Err(anyhow!("Failed to apply config: {}", err))
|
||||
@@ -101,54 +92,10 @@ impl CoreManager {
|
||||
}
|
||||
}
|
||||
|
||||
async fn retry_with_restart(&self, config_path: &str) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
return Err(anyhow!("Application exiting"));
|
||||
}
|
||||
|
||||
logging!(warn, Type::Core, "Restarting core for config reload");
|
||||
self.restart_core().await?;
|
||||
sleep(timing::CONFIG_RELOAD_DELAY).await;
|
||||
|
||||
self.reload_config(config_path).await?;
|
||||
Config::runtime().await.apply();
|
||||
logging!(info, Type::Core, "Configuration applied after restart");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reload_config(&self, path: &str) -> Result<(), MihomoError> {
|
||||
handle::Handle::mihomo()
|
||||
.await
|
||||
.reload_config(true, path)
|
||||
.await
|
||||
}
|
||||
|
||||
fn should_restart_on_error(err: &MihomoError) -> bool {
|
||||
match err {
|
||||
MihomoError::ConnectionFailed | MihomoError::ConnectionLost => true,
|
||||
MihomoError::Io(io_err) => Self::is_connection_io_error(io_err.kind()),
|
||||
MihomoError::Reqwest(req_err) => {
|
||||
req_err.is_connect()
|
||||
|| req_err.is_timeout()
|
||||
|| Self::contains_error_pattern(&req_err.to_string())
|
||||
}
|
||||
MihomoError::FailedResponse(msg) => Self::contains_error_pattern(msg),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
const fn is_connection_io_error(kind: std::io::ErrorKind) -> bool {
|
||||
matches!(
|
||||
kind,
|
||||
std::io::ErrorKind::ConnectionAborted
|
||||
| std::io::ErrorKind::ConnectionRefused
|
||||
| std::io::ErrorKind::ConnectionReset
|
||||
| std::io::ErrorKind::NotFound
|
||||
)
|
||||
}
|
||||
|
||||
fn contains_error_pattern(text: &str) -> bool {
|
||||
use crate::constants::error_patterns::CONNECTION_ERRORS;
|
||||
CONNECTION_ERRORS.iter().any(|p| text.contains(p))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::{CoreManager, RunningMode};
|
||||
use crate::config::{Config, ConfigType, IVerge};
|
||||
use crate::cmd::StringifyErr as _;
|
||||
use crate::config::{Config, IVerge};
|
||||
use crate::{
|
||||
core::{
|
||||
logger::CLASH_LOGGER,
|
||||
@@ -55,13 +56,8 @@ impl CoreManager {
|
||||
let verge_data = Config::verge().await.latest_arc();
|
||||
verge_data.save_file().await.map_err(|e| e.to_string())?;
|
||||
|
||||
let run_path = Config::generate_file(ConfigType::Run)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
self.apply_config(run_path)
|
||||
.await
|
||||
.map_err(|e| e.to_string().into())
|
||||
self.update_config().await.stringify_err()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn prepare_startup(&self) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user