From 993b6e9d0a03d3ceceb6bf6bd79a21de94cca1d9 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 28 Aug 2018 21:22:34 +0300 Subject: [PATCH] Utils: only queue a single cover download Before we were inserting the id of the cover into the registry from a rayon thread. But rayon will only execute N threads at the same time and let the rest into a queue. This would casue mutliple jobs being queued since the cover id was not inserted in the registry until the downloading had started. This fixes said behavior by having the main thread block and write in the id in the registry. --- podcasts-gtk/src/utils.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/podcasts-gtk/src/utils.rs b/podcasts-gtk/src/utils.rs index 2e69c87..82e484e 100644 --- a/podcasts-gtk/src/utils.rs +++ b/podcasts-gtk/src/utils.rs @@ -285,20 +285,22 @@ pub(crate) fn set_image_from_path( } let (sender, receiver) = unbounded(); - THREADPOOL.spawn(move || { - if let Ok(mut guard) = COVER_DL_REGISTRY.write() { - guard.insert(show_id); - } + if let Ok(mut guard) = COVER_DL_REGISTRY.write() { + // Add the id to the hashmap from the main thread to avoid queuing more than one downloads. + guard.insert(show_id); + drop(guard); - // This operation is polling and will block the thread till the download is finished - if let Ok(pd) = dbqueries::get_podcast_cover_from_id(show_id) { - sender.send(downloader::cache_image(&pd)); - } + THREADPOOL.spawn(move || { + // This operation is polling and will block the thread till the download is finished + if let Ok(pd) = dbqueries::get_podcast_cover_from_id(show_id) { + sender.send(downloader::cache_image(&pd)); + } - if let Ok(mut guard) = COVER_DL_REGISTRY.write() { - guard.remove(&show_id); - } - }); + if let Ok(mut guard) = COVER_DL_REGISTRY.write() { + guard.remove(&show_id); + } + }); + } let image = image.clone(); let s = size as i32;