@@ -9,6 +9,7 @@ use crate::{
9
9
web:: {
10
10
axum_parse_uri_with_params, axum_redirect, encode_url_path,
11
11
error:: { AxumNope , AxumResult } ,
12
+ extractors:: DbConnection ,
12
13
match_version_axum,
13
14
} ,
14
15
BuildQueue , Config , InstanceMetrics ,
@@ -133,7 +134,7 @@ struct SearchResult {
133
134
///
134
135
/// This delegates to the crates.io search API.
135
136
async fn get_search_results (
136
- pool : Pool ,
137
+ conn : & mut sqlx :: PgConnection ,
137
138
config : & Config ,
138
139
query_params : & str ,
139
140
) -> Result < SearchResult , anyhow:: Error > {
@@ -212,10 +213,6 @@ async fn get_search_results(
212
213
// So for now we are using the version with the youngest release_time.
213
214
// This is different from all other release-list views where we show
214
215
// our latest build.
215
- let mut conn = pool
216
- . get_async ( )
217
- . await
218
- . context ( "can't get pool connection" ) ?;
219
216
let crates: HashMap < String , Release > = sqlx:: query!(
220
217
r#"SELECT
221
218
crates.name,
@@ -276,12 +273,7 @@ impl_axum_webpage! {
276
273
HomePage = "core/home.html" ,
277
274
}
278
275
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 > {
285
277
let recent_releases =
286
278
get_releases ( & mut conn, 1 , RELEASES_IN_HOME , Order :: ReleaseTime , true ) . await ?;
287
279
@@ -298,14 +290,7 @@ impl_axum_webpage! {
298
290
content_type = "application/xml" ,
299
291
}
300
292
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 > {
309
294
let recent_releases =
310
295
get_releases ( & mut conn, 1 , RELEASES_IN_FEED , Order :: ReleaseTime , true ) . await ?;
311
296
Ok ( ReleaseFeed { recent_releases } )
@@ -337,15 +322,10 @@ pub(crate) enum ReleaseType {
337
322
}
338
323
339
324
pub ( crate ) async fn releases_handler (
340
- pool : Pool ,
325
+ conn : & mut sqlx :: PgConnection ,
341
326
page : Option < i64 > ,
342
327
release_type : ReleaseType ,
343
328
) -> AxumResult < impl IntoResponse > {
344
- let mut conn = pool
345
- . get_async ( )
346
- . await
347
- . context ( "can't get pool connection" ) ?;
348
-
349
329
let page_number = page. unwrap_or ( 1 ) ;
350
330
351
331
let ( description, release_order, latest_only) = match release_type {
@@ -368,7 +348,7 @@ pub(crate) async fn releases_handler(
368
348
} ;
369
349
370
350
let releases = get_releases (
371
- & mut conn,
351
+ & mut * conn,
372
352
page_number,
373
353
RELEASES_IN_RELEASES ,
374
354
release_order,
@@ -395,30 +375,30 @@ pub(crate) async fn releases_handler(
395
375
396
376
pub ( crate ) async fn recent_releases_handler (
397
377
page : Option < Path < i64 > > ,
398
- Extension ( pool ) : Extension < Pool > ,
378
+ mut conn : DbConnection ,
399
379
) -> 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
401
381
}
402
382
403
383
pub ( crate ) async fn releases_by_stars_handler (
404
384
page : Option < Path < i64 > > ,
405
- Extension ( pool ) : Extension < Pool > ,
385
+ mut conn : DbConnection ,
406
386
) -> 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
408
388
}
409
389
410
390
pub ( crate ) async fn releases_recent_failures_handler (
411
391
page : Option < Path < i64 > > ,
412
- Extension ( pool ) : Extension < Pool > ,
392
+ mut conn : DbConnection ,
413
393
) -> 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
415
395
}
416
396
417
397
pub ( crate ) async fn releases_failures_by_stars_handler (
418
398
page : Option < Path < i64 > > ,
419
- Extension ( pool ) : Extension < Pool > ,
399
+ mut conn : DbConnection ,
420
400
) -> 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
422
402
}
423
403
424
404
pub ( crate ) async fn owner_handler ( Path ( owner) : Path < String > ) -> AxumResult < impl IntoResponse > {
@@ -460,19 +440,14 @@ impl Default for Search {
460
440
async fn redirect_to_random_crate (
461
441
config : Arc < Config > ,
462
442
metrics : Arc < InstanceMetrics > ,
463
- pool : Pool ,
443
+ conn : & mut sqlx :: PgConnection ,
464
444
) -> AxumResult < impl IntoResponse > {
465
445
// We try to find a random crate and redirect to it.
466
446
//
467
447
// The query is efficient, but relies on a static factor which depends
468
448
// on the amount of crates with > 100 GH stars over the amount of all crates.
469
449
//
470
450
// 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
-
476
451
let row = sqlx:: query!(
477
452
"WITH params AS (
478
453
-- get maximum possible id-value in crates-table
@@ -519,6 +494,7 @@ impl_axum_webpage! {
519
494
}
520
495
521
496
pub ( crate ) async fn search_handler (
497
+ mut conn : DbConnection ,
522
498
Extension ( pool) : Extension < Pool > ,
523
499
Extension ( config) : Extension < Arc < Config > > ,
524
500
Extension ( metrics) : Extension < Arc < InstanceMetrics > > ,
@@ -534,7 +510,7 @@ pub(crate) async fn search_handler(
534
510
if params. remove ( "i-am-feeling-lucky" ) . is_some ( ) || query. contains ( "::" ) {
535
511
// redirect to a random crate if query is empty
536
512
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 )
538
514
. await ?
539
515
. into_response ( ) ) ;
540
516
}
@@ -595,14 +571,14 @@ pub(crate) async fn search_handler(
595
571
return Err ( AxumNope :: NoResults ) ;
596
572
}
597
573
598
- get_search_results ( pool , & config, & query_params) . await ?
574
+ get_search_results ( & mut conn , & config, & query_params) . await ?
599
575
} else if !query. is_empty ( ) {
600
576
let query_params: String = form_urlencoded:: Serializer :: new ( String :: new ( ) )
601
577
. append_pair ( "q" , & query)
602
578
. append_pair ( "per_page" , & RELEASES_IN_RELEASES . to_string ( ) )
603
579
. finish ( ) ;
604
580
605
- get_search_results ( pool , & config, & format ! ( "?{}" , & query_params) ) . await ?
581
+ get_search_results ( & mut conn , & config, & format ! ( "?{}" , & query_params) ) . await ?
606
582
} else {
607
583
return Err ( AxumNope :: NoResults ) ;
608
584
} ;
0 commit comments