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.
This commit is contained in:
Jordan Petridis 2018-08-28 21:22:34 +03:00 committed by Jordan Petridis
parent 273c9f7b99
commit 993b6e9d0a

View File

@ -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;