Skip to content

Commit

Permalink
fix(tray): proxies updating deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Feb 14, 2024
1 parent 5772101 commit 9ae958e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
8 changes: 8 additions & 0 deletions backend/tauri/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use sysproxy::Sysproxy;

use tauri::api::dialog::FileDialogBuilder;

use self::clash::proxies::ProxiesGuard;

type CmdResult<T = ()> = Result<T, String>;

#[tauri::command]
Expand Down Expand Up @@ -330,6 +332,12 @@ pub async fn clash_api_get_proxy_delay(
pub async fn get_proxies() -> CmdResult<crate::core::clash::proxies::Proxies> {
use crate::core::clash::proxies::ProxiesGuard;
use crate::core::clash::proxies::ProxiesGuardExt;
{
let guard = ProxiesGuard::global().read();
if guard.is_updated() {
return Ok(guard.inner().clone());
}
}
match ProxiesGuard::global().update().await {
Ok(_) => {
let proxies = ProxiesGuard::global().read().inner().clone();
Expand Down
7 changes: 6 additions & 1 deletion backend/tauri/src/core/clash/proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl ProxiesGuard {
}

pub fn replace(&mut self, proxies: Proxies) {
let now = tokio::time::Instant::now().elapsed().as_secs();
let now = chrono::Utc::now().timestamp() as u64;
self.inner = proxies;
self.updated_at = now;

Expand All @@ -240,6 +240,11 @@ impl ProxiesGuard {
pub fn updated_at(&self) -> u64 {
self.updated_at
}

pub fn is_updated(&self) -> bool {
let now = chrono::Utc::now().timestamp() as u64;
now - self.updated_at <= 3
}
}

pub trait ProxiesGuardExt {
Expand Down
2 changes: 1 addition & 1 deletion backend/tauri/src/core/tray/proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod platform_impl {
let mut menu = menu.to_owned();
match mode.as_str() {
"rule" | "script" | "global" => {
if mode == "global" {
if mode == "global" || proxies.groups.is_empty() {
let group_selector = generate_group_selector(&proxies.global);
menu = menu.add_submenu(group_selector);
}
Expand Down
4 changes: 2 additions & 2 deletions backend/tauri/src/utils/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ macro_rules! log_err {
macro_rules! dialog_err {
($result: expr) => {
if let Err(err) = $result {
crate::utils::dialog::error_dialog(format!("{:?}", err));
$crate::utils::dialog::error_dialog(format!("{:?}", err));
}
};

($result: expr, $err_str: expr) => {
if let Err(_) = $result {
crate::utils::dialog::error_dialog($err_str.into());
$crate::utils::dialog::error_dialog($err_str.into());
}
};
}
Expand Down

0 comments on commit 9ae958e

Please sign in to comment.