@@ -9,6 +9,7 @@ use crate::{
99 web:: {
1010 axum_parse_uri_with_params, axum_redirect, encode_url_path,
1111 error:: { AxumNope , AxumResult } ,
12+ extractors:: DbConnection ,
1213 match_version_axum,
1314 } ,
1415 BuildQueue , Config , InstanceMetrics ,
@@ -133,7 +134,7 @@ struct SearchResult {
133134///
134135/// This delegates to the crates.io search API.
135136async fn get_search_results (
136- pool : Pool ,
137+ conn : & mut sqlx :: PgConnection ,
137138 config : & Config ,
138139 query_params : & str ,
139140) -> Result < SearchResult , anyhow:: Error > {
@@ -212,10 +213,6 @@ async fn get_search_results(
212213 // So for now we are using the version with the youngest release_time.
213214 // This is different from all other release-list views where we show
214215 // our latest build.
215- let mut conn = pool
216- . get_async ( )
217- . await
218- . context ( "can't get pool connection" ) ?;
219216 let crates: HashMap < String , Release > = sqlx:: query!(
220217 r#"SELECT
221218 crates.name,
@@ -276,12 +273,7 @@ impl_axum_webpage! {
276273 HomePage = "core/home.html" ,
277274}
278275
279- pub ( crate ) async fn home_page ( Extension ( pool) : Extension < Pool > ) -> AxumResult < impl IntoResponse > {
280- let mut conn = pool
281- . get_async ( )
282- . await
283- . context ( "can't get pool connection" ) ?;
284-
276+ pub ( crate ) async fn home_page ( mut conn : DbConnection ) -> AxumResult < impl IntoResponse > {
285277 let recent_releases =
286278 get_releases ( & mut conn, 1 , RELEASES_IN_HOME , Order :: ReleaseTime , true ) . await ?;
287279
@@ -298,14 +290,7 @@ impl_axum_webpage! {
298290 content_type = "application/xml" ,
299291}
300292
301- pub ( crate ) async fn releases_feed_handler (
302- Extension ( pool) : Extension < Pool > ,
303- ) -> AxumResult < impl IntoResponse > {
304- let mut conn = pool
305- . get_async ( )
306- . await
307- . context ( "can't get pool connection" ) ?;
308-
293+ pub ( crate ) async fn releases_feed_handler ( mut conn : DbConnection ) -> AxumResult < impl IntoResponse > {
309294 let recent_releases =
310295 get_releases ( & mut conn, 1 , RELEASES_IN_FEED , Order :: ReleaseTime , true ) . await ?;
311296 Ok ( ReleaseFeed { recent_releases } )
@@ -337,15 +322,10 @@ pub(crate) enum ReleaseType {
337322}
338323
339324pub ( crate ) async fn releases_handler (
340- pool : Pool ,
325+ conn : & mut sqlx :: PgConnection ,
341326 page : Option < i64 > ,
342327 release_type : ReleaseType ,
343328) -> AxumResult < impl IntoResponse > {
344- let mut conn = pool
345- . get_async ( )
346- . await
347- . context ( "can't get pool connection" ) ?;
348-
349329 let page_number = page. unwrap_or ( 1 ) ;
350330
351331 let ( description, release_order, latest_only) = match release_type {
@@ -368,7 +348,7 @@ pub(crate) async fn releases_handler(
368348 } ;
369349
370350 let releases = get_releases (
371- & mut conn,
351+ & mut * conn,
372352 page_number,
373353 RELEASES_IN_RELEASES ,
374354 release_order,
@@ -395,30 +375,30 @@ pub(crate) async fn releases_handler(
395375
396376pub ( crate ) async fn recent_releases_handler (
397377 page : Option < Path < i64 > > ,
398- Extension ( pool ) : Extension < Pool > ,
378+ mut conn : DbConnection ,
399379) -> AxumResult < impl IntoResponse > {
400- releases_handler ( pool , page. map ( |p| p. 0 ) , ReleaseType :: Recent ) . await
380+ releases_handler ( & mut conn , page. map ( |p| p. 0 ) , ReleaseType :: Recent ) . await
401381}
402382
403383pub ( crate ) async fn releases_by_stars_handler (
404384 page : Option < Path < i64 > > ,
405- Extension ( pool ) : Extension < Pool > ,
385+ mut conn : DbConnection ,
406386) -> AxumResult < impl IntoResponse > {
407- releases_handler ( pool , page. map ( |p| p. 0 ) , ReleaseType :: Stars ) . await
387+ releases_handler ( & mut conn , page. map ( |p| p. 0 ) , ReleaseType :: Stars ) . await
408388}
409389
410390pub ( crate ) async fn releases_recent_failures_handler (
411391 page : Option < Path < i64 > > ,
412- Extension ( pool ) : Extension < Pool > ,
392+ mut conn : DbConnection ,
413393) -> AxumResult < impl IntoResponse > {
414- releases_handler ( pool , page. map ( |p| p. 0 ) , ReleaseType :: RecentFailures ) . await
394+ releases_handler ( & mut conn , page. map ( |p| p. 0 ) , ReleaseType :: RecentFailures ) . await
415395}
416396
417397pub ( crate ) async fn releases_failures_by_stars_handler (
418398 page : Option < Path < i64 > > ,
419- Extension ( pool ) : Extension < Pool > ,
399+ mut conn : DbConnection ,
420400) -> AxumResult < impl IntoResponse > {
421- releases_handler ( pool , page. map ( |p| p. 0 ) , ReleaseType :: Failures ) . await
401+ releases_handler ( & mut conn , page. map ( |p| p. 0 ) , ReleaseType :: Failures ) . await
422402}
423403
424404pub ( crate ) async fn owner_handler ( Path ( owner) : Path < String > ) -> AxumResult < impl IntoResponse > {
@@ -460,19 +440,14 @@ impl Default for Search {
460440async fn redirect_to_random_crate (
461441 config : Arc < Config > ,
462442 metrics : Arc < InstanceMetrics > ,
463- pool : Pool ,
443+ conn : & mut sqlx :: PgConnection ,
464444) -> AxumResult < impl IntoResponse > {
465445 // We try to find a random crate and redirect to it.
466446 //
467447 // The query is efficient, but relies on a static factor which depends
468448 // on the amount of crates with > 100 GH stars over the amount of all crates.
469449 //
470450 // If random-crate-searches end up being empty, increase that value.
471- let mut conn = pool
472- . get_async ( )
473- . await
474- . context ( "can't get pool connection" ) ?;
475-
476451 let row = sqlx:: query!(
477452 "WITH params AS (
478453 -- get maximum possible id-value in crates-table
@@ -519,6 +494,7 @@ impl_axum_webpage! {
519494}
520495
521496pub ( crate ) async fn search_handler (
497+ mut conn : DbConnection ,
522498 Extension ( pool) : Extension < Pool > ,
523499 Extension ( config) : Extension < Arc < Config > > ,
524500 Extension ( metrics) : Extension < Arc < InstanceMetrics > > ,
@@ -534,7 +510,7 @@ pub(crate) async fn search_handler(
534510 if params. remove ( "i-am-feeling-lucky" ) . is_some ( ) || query. contains ( "::" ) {
535511 // redirect to a random crate if query is empty
536512 if query. is_empty ( ) {
537- return Ok ( redirect_to_random_crate ( config, metrics, pool )
513+ return Ok ( redirect_to_random_crate ( config, metrics, & mut conn )
538514 . await ?
539515 . into_response ( ) ) ;
540516 }
@@ -595,14 +571,14 @@ pub(crate) async fn search_handler(
595571 return Err ( AxumNope :: NoResults ) ;
596572 }
597573
598- get_search_results ( pool , & config, & query_params) . await ?
574+ get_search_results ( & mut conn , & config, & query_params) . await ?
599575 } else if !query. is_empty ( ) {
600576 let query_params: String = form_urlencoded:: Serializer :: new ( String :: new ( ) )
601577 . append_pair ( "q" , & query)
602578 . append_pair ( "per_page" , & RELEASES_IN_RELEASES . to_string ( ) )
603579 . finish ( ) ;
604580
605- get_search_results ( pool , & config, & format ! ( "?{}" , & query_params) ) . await ?
581+ get_search_results ( & mut conn , & config, & format ! ( "?{}" , & query_params) ) . await ?
606582 } else {
607583 return Err ( AxumNope :: NoResults ) ;
608584 } ;
0 commit comments