From 6a05683d536604ac97c02aaadc8d931d87c11804 Mon Sep 17 00:00:00 2001 From: monosans Date: Thu, 8 May 2025 12:56:32 +0300 Subject: [PATCH] refactor checking code --- src/checker.rs | 70 +++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/src/checker.rs b/src/checker.rs index e06229e..e69da12 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -4,37 +4,7 @@ use color_eyre::eyre::WrapErr as _; #[cfg(feature = "tui")] use crate::event::{AppEvent, Event}; -use crate::{ - config::Config, proxy::Proxy, storage::ProxyStorage, utils::pretty_error, -}; - -async fn check_one( - config: Arc, - mut proxy: Proxy, - #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender, -) -> color_eyre::Result { - let check_result = proxy - .check(Arc::clone(&config)) - .await - .wrap_err("proxy did not pass checking"); - #[cfg(feature = "tui")] - tx.send(Event::App(AppEvent::ProxyChecked(proxy.protocol.clone())))?; - match check_result { - Ok(()) => { - #[cfg(feature = "tui")] - tx.send(Event::App(AppEvent::ProxyWorking( - proxy.protocol.clone(), - )))?; - Ok(proxy) - } - Err(e) => { - if log::log_enabled!(log::Level::Debug) { - log::debug!("{} | {}", proxy.as_str(true), pretty_error(&e)); - } - Err(e) - } - } -} +use crate::{config::Config, storage::ProxyStorage, utils::pretty_error}; pub async fn check_all( config: Arc, @@ -52,7 +22,7 @@ pub async fn check_all( let (result_tx, mut result_rx) = tokio::sync::mpsc::unbounded_channel(); - let mut join_set = tokio::task::JoinSet::new(); + let mut join_set = tokio::task::JoinSet::>::new(); for _ in 0..workers_count { let queue = Arc::clone(&queue); @@ -62,19 +32,33 @@ pub async fn check_all( let result_tx = result_tx.clone(); join_set.spawn(async move { loop { - let Some(proxy) = queue.lock().await.pop_front() else { + let Some(mut proxy) = queue.lock().await.pop_front() else { break Ok(()); }; - if let Ok(proxy) = check_one( - Arc::clone(&config), - proxy, - #[cfg(feature = "tui")] - tx.clone(), - ) - .await - { - if let Err(e) = result_tx.send(proxy) { - break Err(e); + let check_result = proxy + .check(Arc::clone(&config)) + .await + .wrap_err("proxy did not pass checking"); + #[cfg(feature = "tui")] + tx.send(Event::App(AppEvent::ProxyChecked( + proxy.protocol.clone(), + )))?; + match check_result { + Ok(()) => { + #[cfg(feature = "tui")] + tx.send(Event::App(AppEvent::ProxyWorking( + proxy.protocol.clone(), + )))?; + result_tx.send(proxy)?; + } + Err(e) => { + if log::log_enabled!(log::Level::Debug) { + log::debug!( + "{} | {}", + proxy.as_str(true), + pretty_error(&e) + ); + } } } }