@@ -34,6 +34,7 @@ use axum_extra::{
3434 typed_header:: TypedHeader ,
3535} ;
3636use docs_rs_cargo_metadata:: Dependency ;
37+ use docs_rs_database:: Pool ;
3738use docs_rs_headers:: { ETagComputer , IfNoneMatch , X_ROBOTS_TAG } ;
3839use docs_rs_registry_api:: OwnerKind ;
3940use docs_rs_rustdoc_json:: RustdocJsonFormatVersion ;
@@ -186,13 +187,13 @@ pub(crate) struct RustdocRedirectorParams {
186187/// Handler called for `/:crate` and `/:crate/:version` URLs. Automatically redirects to the docs
187188/// or crate details page based on whether the given crate version was successfully built.
188189#[ allow( clippy:: too_many_arguments) ]
189- #[ instrument( skip( storage, conn ) ) ]
190+ #[ instrument( skip( storage, pool ) ) ]
190191pub ( crate ) async fn rustdoc_redirector_handler (
191192 Path ( params) : Path < RustdocRedirectorParams > ,
192193 original_uri : Uri ,
193194 matched_path : MatchedPath ,
194195 Extension ( storage) : Extension < Arc < AsyncStorage > > ,
195- mut conn : DbConnection ,
196+ Extension ( pool ) : Extension < Pool > ,
196197 if_none_match : Option < TypedHeader < IfNoneMatch > > ,
197198 RawQuery ( original_query) : RawQuery ,
198199) -> AxumResult < impl IntoResponse > {
@@ -291,6 +292,9 @@ pub(crate) async fn rustdoc_redirector_handler(
291292 . map_err ( AxumNope :: BadRequest ) ?
292293 . with_page_kind ( PageKind :: Rustdoc ) ;
293294
295+ // NOTE: we try to shorten the time we hold the database connection from the pool.
296+ let mut conn = pool. get_async ( ) . await ?;
297+
294298 // it doesn't matter if the version that was given was exact or not, since we're redirecting
295299 // anyway
296300 let matched_release = match_version ( & mut conn, & crate_name, & params. req_version ( ) . clone ( ) )
@@ -316,6 +320,10 @@ pub(crate) async fn rustdoc_redirector_handler(
316320 return async {
317321 let krate = CrateDetails :: from_matched_release ( & mut conn, matched_release) . await ?;
318322
323+ // NOTE: we want to give back the db connection to the pool
324+ // before we do the long S3 requests.
325+ drop ( conn) ;
326+
319327 match storage
320328 . stream_rustdoc_file (
321329 params. name ( ) ,
@@ -597,6 +605,10 @@ pub(crate) async fn rustdoc_html_server_handler(
597605
598606 let krate = CrateDetails :: from_matched_release ( & mut conn, matched_release) . await ?;
599607
608+ // NOTE: we want to give back the db connection to the pool
609+ // before we do the long S3 requests.
610+ drop ( conn) ;
611+
600612 trace ! (
601613 ?params,
602614 doc_targets=?krate. metadata. doc_targets,
@@ -784,9 +796,13 @@ pub(crate) async fn target_redirect_handler(
784796 . await ?
785797 . into_canonical_req_version_or_else ( |_, _| AxumNope :: VersionNotFound ) ?;
786798 let params = params. apply_matched_release ( & matched_release) ;
799+ trace ! ( ?params, "parsed params" ) ;
787800
788801 let crate_details = CrateDetails :: from_matched_release ( & mut conn, matched_release) . await ?;
789- trace ! ( ?params, "parsed params" ) ;
802+
803+ // NOTE: we want to give back the db connection to the pool
804+ // before we do the long S3 requests.
805+ drop ( conn) ;
790806
791807 let storage_path = params. storage_path ( ) ;
792808 trace ! ( storage_path, "checking if path exists in other version" ) ;
@@ -893,6 +909,10 @@ pub(crate) async fn json_download_handler(
893909
894910 let krate = CrateDetails :: from_matched_release ( & mut conn, matched_release) . await ?;
895911
912+ // NOTE: we want to give back the db connection to the pool
913+ // before we do the long S3 requests.
914+ drop ( conn) ;
915+
896916 let wanted_format_version = if let Some ( request_format_version) = json_params. format_version {
897917 // axum doesn't support extension suffixes in the route yet, not as parameter, and not
898918 // statically, when combined with a parameter (like `.../{format_version}.gz`).
@@ -1001,6 +1021,11 @@ pub(crate) async fn download_handler(
10011021 CachePolicy :: ForeverInCdn ( confirmed_name. into ( ) ) ,
10021022 )
10031023 } ) ?;
1024+
1025+ // NOTE: we want to give back the db connection to the pool
1026+ // before we do the long S3 requests.
1027+ drop ( conn) ;
1028+
10041029 params = params. apply_matched_release ( & matched_release) ;
10051030
10061031 let version = & matched_release. release . version ;
0 commit comments