1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-07-02 16:38:36 +00:00

Simplify the request limiting

This commit is contained in:
Reiner Herrmann 2023-08-19 23:39:35 +02:00
parent fc42243ec2
commit 2d9248ed3b
3 changed files with 17 additions and 46 deletions

View file

@ -13,7 +13,6 @@ use {
webpage::HTML,
reqwest::Url,
std::{io::Cursor, net::IpAddr, sync::Arc},
tokio::sync::Notify,
image::io::Reader as ImgReader,
};
@ -198,49 +197,21 @@ async fn get_url_preview(url: &str) -> Result<UrlPreviewData> {
return Ok(preview);
}
let notif_opt = services()
.media
.url_preview_requests
.read()
.unwrap()
.get(url)
.cloned();
// ensure that only one request is made per URL
let mutex_request = Arc::clone(
services()
.media
.url_preview_mutex
.write()
.unwrap()
.entry(url.to_owned())
.or_default(),
);
let _request_lock = mutex_request.lock().await;
match notif_opt {
None => {
let notifier = Arc::new(Notify::new());
{
services().media
.url_preview_requests
.write()
.unwrap()
.insert(url.to_owned(), notifier.clone());
}
let data = request_url_preview(url).await;
notifier.notify_waiters();
{
services().media.url_preview_requests.write().unwrap().remove(url);
}
data
}
Some(notifier) => {
// wait until being notified that request is finished
let notifier = notifier.clone();
let notifier = notifier.notified();
notifier.await;
services().media
.get_url_preview(url)
.await
.ok_or(Error::BadRequest(
ErrorKind::Unknown,
"No Preview available",
))
}
match services().media.get_url_preview(url).await {
Some(preview) => Ok(preview),
None => request_url_preview(url).await
}
}