refactor checking code

This commit is contained in:
monosans
2025-05-08 12:56:32 +03:00
parent c406e1f2e1
commit 6a05683d53
+27 -43
View File
@@ -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<Config>,
mut proxy: Proxy,
#[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender<Event>,
) -> color_eyre::Result<Proxy> {
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<Config>,
@@ -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::<color_eyre::Result<()>>::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)
);
}
}
}
}