From 3a783be87004f0328e3d812e68ff79799cb15228 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Sat, 9 Oct 2021 17:38:15 +0200 Subject: refactor url_preview() to run all URLs in parallel --- src/hooks/url.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/hooks/url.rs b/src/hooks/url.rs index 4e19282..22b7d64 100644 --- a/src/hooks/url.rs +++ b/src/hooks/url.rs @@ -6,7 +6,7 @@ use regex::Regex; extern crate kuchiki; use kuchiki::{parse_html, traits::*}; use reqwest::{get, Url}; -use tracing::{error, trace}; +use tracing::trace; pub const URL_REGEX: &str = r#"(https?://|www.)\S+"#; @@ -43,19 +43,29 @@ pub async fn url_title(url: &str) -> Result { #[tracing::instrument(skip(bot))] pub async fn url_preview(bot: &crate::Bot, msg: Message) -> Result<()> { if let Command::PRIVMSG(target, text) = msg.command.clone() { - let mut titles: Vec = Vec::new(); + let mut futures: Vec> = Vec::new(); for url in url_parser(&text) { - trace!("got url: {:?}", url); - match url_title(&url.as_str()).await { - Ok(title) => { - trace!("extracted title from url: {:?}, {:?}", title, url); - titles.push(title); + futures.push(tokio::spawn(async move { + trace!("got url: {:?}", url); + match url_title(&url.as_str()).await { + Ok(title) => { + trace!("extracted title from url: {:?}, {:?}", title, url); + Ok(title) + } + Err(err) => bail!("Failed to get urls title: {:?}", err), } - Err(err) => error!("Failed to get urls title: {:?}", err), - } + })) } + let titles = futures::future::join_all(futures).await; + + let titles: Vec = titles + .into_iter() + .filter_map(|x| x.ok()) + .filter_map(|x| x.ok()) + .collect(); + if !titles.is_empty() { bot.send_privmsg(&target, &msg_builder(&titles))?; } -- cgit v1.2.3