From 5d9f3a53abcc07769bdd9a992fdfdcb7ae7f4d47 Mon Sep 17 00:00:00 2001 From: XOR-op <17672363+XOR-op@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:12:31 -0400 Subject: [PATCH] chore: test latency for group type --- boltconn/src/external/controller.rs | 51 +++++++++++++++++------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/boltconn/src/external/controller.rs b/boltconn/src/external/controller.rs index bc97b7f..34084a4 100644 --- a/boltconn/src/external/controller.rs +++ b/boltconn/src/external/controller.rs @@ -11,6 +11,7 @@ use boltapi::{ ConnectionSchema, GetGroupRespSchema, GetInterceptDataResp, GetInterceptRangeReq, HttpInterceptSchema, ProcessSchema, ProxyData, SessionSchema, TrafficResp, TunStatusSchema, }; +use std::collections::HashSet; use std::io::Write; use std::sync::atomic::Ordering; use std::sync::Arc; @@ -420,7 +421,7 @@ impl Controller { } pub async fn update_latency(&self, group: String) { - tracing::debug!("Start speedtest for group {}", group); + tracing::trace!("Start speedtest for group {}", group); let speedtest_url = self.speedtest_url.read().unwrap().clone(); let list = self.dispatching.load().get_group_list(); for g in list.iter() { @@ -428,21 +429,28 @@ impl Controller { let iface = g.get_direct_interface(); // update all latency inside the group let mut handles = vec![]; + let mut tested_proxy = HashSet::new(); for p in g.get_members() { - if let GeneralProxy::Single(p) = p { - if let Ok(h) = latency_test( - self.dispatcher.as_ref(), - p.clone(), - speedtest_url.as_str(), - Duration::from_secs(2), - iface.clone(), - ) - .await - { - handles.push(h); - } else { - p.set_latency(Latency::Failed) - } + let p = match p { + GeneralProxy::Single(p) => p, + GeneralProxy::Group(g) => &g.get_proxy(), + }; + if tested_proxy.contains(&p.get_name()) { + continue; + } + tested_proxy.insert(p.get_name()); + if let Ok(h) = latency_test( + self.dispatcher.as_ref(), + p.clone(), + speedtest_url.as_str(), + Duration::from_secs(2), + iface.clone(), + ) + .await + { + handles.push(h); + } else { + p.set_latency(Latency::Failed) } } for h in handles { @@ -459,20 +467,21 @@ impl Controller { } fn pretty_proxy(g: &GeneralProxy) -> ProxyData { + let latency_to_str = |latency: Latency| match latency { + Latency::Unknown => None, + Latency::Value(ms) => Some(format!("{ms} ms")), + Latency::Failed => Some("Failed".to_string()), + }; match g { GeneralProxy::Single(p) => ProxyData { name: p.get_name(), proto: p.get_impl().simple_description(), - latency: match p.get_latency() { - Latency::Unknown => None, - Latency::Value(ms) => Some(format!("{ms} ms")), - Latency::Failed => Some("Failed".to_string()), - }, + latency: latency_to_str(p.get_latency()), }, GeneralProxy::Group(g) => ProxyData { name: g.get_name(), proto: "group".to_string(), - latency: None, + latency: latency_to_str(g.get_proxy().get_latency()), }, } }