From c64e6573d9be7890bf543e1cdf6cb7974f42c54e Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 5 Jan 2025 16:53:16 +0800 Subject: [PATCH] arc on downloaded_data --- src/main.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 94ab47d..9f3d704 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,10 +19,10 @@ struct Downloader { #[derive(Debug)] struct DownloadState { notify: Notify, - result: Mutex>>, + result: Mutex, (StatusCode, String)>>>, } -#[derive(Debug, Clone)] +#[derive(Debug)] struct DownloadData { body: Bytes, headers: reqwest::header::HeaderMap, @@ -35,7 +35,7 @@ impl Downloader { } } - async fn download(&self, target_url: &str) -> Result { + async fn download(&self, target_url: &str) -> Result, (StatusCode, String)> { // check if the url is already downloading { let states = self.states.lock().await; @@ -82,18 +82,17 @@ impl Downloader { .await .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; + let download_data = Arc::new(DownloadData { body, headers }); + // notify all waiters { let mut states = self.states.lock().await; let state = states.remove(target_url).unwrap(); - state.result.lock().await.replace(Ok(DownloadData { - body: body.clone(), - headers: headers.clone(), - })); + state.result.lock().await.replace(Ok(download_data.clone())); state.notify.notify_waiters(); } - Ok(DownloadData { body, headers }) + Ok(download_data.into()) } } #[tokio::main] @@ -147,7 +146,7 @@ async fn api_proxy_image( } let downlaod_begin = std::time::Instant::now(); - let DownloadData { body, headers } = downloader.download(target_url).await?; + let DownloadData { body, headers } = &*downloader.download(target_url).await?; let ori_content_type = headers.get("Content-Type").unwrap().to_str().unwrap(); if !ori_content_type.starts_with("image/") { return Err((