From c160a7c4fa51be5893b1d4fa5482af5f91524a12 Mon Sep 17 00:00:00 2001 From: canarypwn Date: Thu, 25 Jan 2024 23:46:38 -0500 Subject: [PATCH] update main.rs: try to optimize proxy_connection test Moving from functional to procedural programming and adding comments --- speedtest-controller/src/main.rs | 76 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/speedtest-controller/src/main.rs b/speedtest-controller/src/main.rs index 6fcb8c6..43c2fba 100644 --- a/speedtest-controller/src/main.rs +++ b/speedtest-controller/src/main.rs @@ -59,46 +59,44 @@ async fn main() -> anyhow::Result<()> { ); None } - Ok(proxy_connection) => - Some(( - proxy.name.clone(), - stream - ::iter(test_providers.iter()) - .then(|(test_provider, (plugin, tests))| { - let proxy_connection = &proxy_connection; - async move { - ( - test_provider.clone(), - stream - ::iter(tests) - .filter_map(|test| async move { - let test_result = - plugin.run_test( - test, - proxy_connection - ).await; - match test_result { - Ok(p) => - Some(( - test.name.clone(), - p, - )), - Err(e) => { - log::error!( - "Failed to run test {test:?} given {proxy_connection:?}. {e}" - ); - None - } - } - }) - .collect::< - HashMap<_, _> - >().await, - ) + + Ok(proxy_connection) => { + // Initialize a HashMap to store results for each test provider. + let mut all_test_results = HashMap::new(); + + // Iterate over each test provider along with their plugins and tests. + for (test_provider, (plugin, tests)) in test_providers.iter() { + // Initialize a HashMap to store results for each test. + let mut test_results = HashMap::new(); + + // Iterate over each test. + for test in tests { + // Attempt to run the test, handling potential errors with '?' + // Errors are logged and the failed test is skipped. + let test_result = plugin.run_test(test, &proxy_connection).await; + match test_result { + Ok(p) => { + // On success, store the test result. + test_results.insert(test.name.clone(), p); } - }) - .collect::>().await, - )), + Err(e) => { + // Log the error and skip this test. + log::error!( + "Failed to run test {test:?} given {proxy_connection:?}. {e}" + ); + continue; + } + } + } + + // Store the results for this test provider. + all_test_results.insert(test_provider.clone(), test_results); + } + + // Return the proxy name and all the test results. + Some((proxy.name.clone(), all_test_results)) + } + } } })