Skip to content

Commit 86286ba

Browse files
authored
controllers/version/downloads: Remove spawn_blocking() call (#10029)
1 parent 2b78c36 commit 86286ba

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/controllers/version/downloads.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use super::version_and_crate;
66
use crate::app::AppState;
77
use crate::models::VersionDownload;
88
use crate::schema::*;
9-
use crate::tasks::spawn_blocking;
109
use crate::util::diesel::prelude::*;
1110
use crate::util::errors::{version_not_found, AppResult};
1211
use crate::util::{redirect, RequestUtils};
@@ -16,7 +15,6 @@ use axum::response::{IntoResponse, Response};
1615
use axum_extra::json;
1716
use axum_extra::response::ErasedJson;
1817
use chrono::{Duration, NaiveDate, Utc};
19-
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
2018
use http::request::Parts;
2119

2220
/// Handles the `GET /crates/:crate_id/:version/download` route.
@@ -41,33 +39,30 @@ pub async fn downloads(
4139
Path((crate_name, version)): Path<(String, String)>,
4240
req: Parts,
4341
) -> AppResult<ErasedJson> {
42+
use diesel_async::RunQueryDsl;
43+
4444
if semver::Version::parse(&version).is_err() {
4545
return Err(version_not_found(&crate_name, &version));
4646
}
4747

4848
let mut conn = app.db_read().await?;
4949
let (version, _) = version_and_crate(&mut conn, &crate_name, &version).await?;
50-
spawn_blocking(move || {
51-
use diesel::RunQueryDsl;
52-
53-
let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into();
5450

55-
let cutoff_end_date = req
56-
.query()
57-
.get("before_date")
58-
.and_then(|d| NaiveDate::parse_from_str(d, "%F").ok())
59-
.unwrap_or_else(|| Utc::now().date_naive());
60-
let cutoff_start_date = cutoff_end_date - Duration::days(89);
51+
let cutoff_end_date = req
52+
.query()
53+
.get("before_date")
54+
.and_then(|d| NaiveDate::parse_from_str(d, "%F").ok())
55+
.unwrap_or_else(|| Utc::now().date_naive());
56+
let cutoff_start_date = cutoff_end_date - Duration::days(89);
6157

62-
let downloads = VersionDownload::belonging_to(&version)
63-
.filter(version_downloads::date.between(cutoff_start_date, cutoff_end_date))
64-
.order(version_downloads::date)
65-
.load(conn)?
66-
.into_iter()
67-
.map(VersionDownload::into)
68-
.collect::<Vec<EncodableVersionDownload>>();
58+
let downloads = VersionDownload::belonging_to(&version)
59+
.filter(version_downloads::date.between(cutoff_start_date, cutoff_end_date))
60+
.order(version_downloads::date)
61+
.load(&mut conn)
62+
.await?
63+
.into_iter()
64+
.map(VersionDownload::into)
65+
.collect::<Vec<EncodableVersionDownload>>();
6966

70-
Ok(json!({ "version_downloads": downloads }))
71-
})
72-
.await?
67+
Ok(json!({ "version_downloads": downloads }))
7368
}

0 commit comments

Comments
 (0)