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:
parent
fc42243ec2
commit
2d9248ed3b
3 changed files with 17 additions and 46 deletions
|
@ -13,7 +13,6 @@ use {
|
||||||
webpage::HTML,
|
webpage::HTML,
|
||||||
reqwest::Url,
|
reqwest::Url,
|
||||||
std::{io::Cursor, net::IpAddr, sync::Arc},
|
std::{io::Cursor, net::IpAddr, sync::Arc},
|
||||||
tokio::sync::Notify,
|
|
||||||
image::io::Reader as ImgReader,
|
image::io::Reader as ImgReader,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -198,49 +197,21 @@ async fn get_url_preview(url: &str) -> Result<UrlPreviewData> {
|
||||||
return Ok(preview);
|
return Ok(preview);
|
||||||
}
|
}
|
||||||
|
|
||||||
let notif_opt = services()
|
// ensure that only one request is made per URL
|
||||||
|
let mutex_request = Arc::clone(
|
||||||
|
services()
|
||||||
.media
|
.media
|
||||||
.url_preview_requests
|
.url_preview_mutex
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.get(url)
|
|
||||||
.cloned();
|
|
||||||
|
|
||||||
match notif_opt {
|
|
||||||
None => {
|
|
||||||
let notifier = Arc::new(Notify::new());
|
|
||||||
{
|
|
||||||
services().media
|
|
||||||
.url_preview_requests
|
|
||||||
.write()
|
.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.insert(url.to_owned(), notifier.clone());
|
.entry(url.to_owned())
|
||||||
}
|
.or_default(),
|
||||||
|
);
|
||||||
|
let _request_lock = mutex_request.lock().await;
|
||||||
|
|
||||||
let data = request_url_preview(url).await;
|
match services().media.get_url_preview(url).await {
|
||||||
|
Some(preview) => Ok(preview),
|
||||||
notifier.notify_waiters();
|
None => request_url_preview(url).await
|
||||||
|
|
||||||
{
|
|
||||||
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",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ use image::imageops::FilterType;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{AsyncReadExt, AsyncWriteExt, BufReader},
|
io::{AsyncReadExt, AsyncWriteExt, BufReader},
|
||||||
sync::Notify,
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ pub struct UrlPreviewData {
|
||||||
|
|
||||||
pub struct Service {
|
pub struct Service {
|
||||||
pub db: &'static dyn Data,
|
pub db: &'static dyn Data,
|
||||||
pub url_preview_requests: RwLock<HashMap<String, Arc<Notify>>>,
|
pub url_preview_mutex: RwLock<HashMap<String, Arc<Mutex<()>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service {
|
impl Service {
|
||||||
|
|
|
@ -120,7 +120,7 @@ impl Services {
|
||||||
key_backups: key_backups::Service { db },
|
key_backups: key_backups::Service { db },
|
||||||
media: media::Service {
|
media: media::Service {
|
||||||
db,
|
db,
|
||||||
url_preview_requests: StdRwLock::new(HashMap::new())
|
url_preview_mutex: StdRwLock::new(HashMap::new()),
|
||||||
},
|
},
|
||||||
sending: sending::Service::build(db, &config),
|
sending: sending::Service::build(db, &config),
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue