@@ -5,7 +5,7 @@ use crate::{
55 cdn,
66 db:: Pool ,
77 impl_axum_webpage,
8- utils:: { report_error, spawn_blocking} ,
8+ utils:: { report_error, retry_async , spawn_blocking} ,
99 web:: {
1010 axum_parse_uri_with_params, axum_redirect, encode_url_path,
1111 error:: { AxumNope , AxumResult } ,
@@ -128,7 +128,11 @@ struct SearchResult {
128128/// Get the search results for a crate search query
129129///
130130/// This delegates to the crates.io search API.
131- async fn get_search_results ( pool : Pool , query_params : & str ) -> Result < SearchResult , anyhow:: Error > {
131+ async fn get_search_results (
132+ pool : Pool ,
133+ config : & Config ,
134+ query_params : & str ,
135+ ) -> Result < SearchResult , anyhow:: Error > {
132136 #[ derive( Deserialize ) ]
133137 struct CratesIoSearchResult {
134138 crates : Vec < CratesIoCrate > ,
@@ -178,13 +182,19 @@ async fn get_search_results(pool: Pool, query_params: &str) -> Result<SearchResu
178182 }
179183 } ) ;
180184
181- let releases: CratesIoSearchResult = HTTP_CLIENT
182- . get ( url)
183- . send ( )
184- . await ?
185- . error_for_status ( ) ?
186- . json ( )
187- . await ?;
185+ let releases: CratesIoSearchResult = retry_async (
186+ || async {
187+ Ok ( HTTP_CLIENT
188+ . get ( url. clone ( ) )
189+ . send ( )
190+ . await ?
191+ . error_for_status ( ) ?)
192+ } ,
193+ config. crates_io_api_call_retries ,
194+ )
195+ . await ?
196+ . json ( )
197+ . await ?;
188198
189199 let names = Arc :: new (
190200 releases
@@ -584,14 +594,14 @@ pub(crate) async fn search_handler(
584594 return Err ( AxumNope :: NoResults ) ;
585595 }
586596
587- get_search_results ( pool, & query_params) . await ?
597+ get_search_results ( pool, & config , & query_params) . await ?
588598 } else if !query. is_empty ( ) {
589599 let query_params: String = form_urlencoded:: Serializer :: new ( String :: new ( ) )
590600 . append_pair ( "q" , & query)
591601 . append_pair ( "per_page" , & RELEASES_IN_RELEASES . to_string ( ) )
592602 . finish ( ) ;
593603
594- get_search_results ( pool, & format ! ( "?{}" , & query_params) ) . await ?
604+ get_search_results ( pool, & config , & format ! ( "?{}" , & query_params) ) . await ?
595605 } else {
596606 return Err ( AxumNope :: NoResults ) ;
597607 } ;
@@ -968,6 +978,10 @@ mod tests {
968978 #[ test_case( StatusCode :: BAD_GATEWAY ) ]
969979 fn crates_io_errors_are_correctly_returned_and_we_dont_try_parsing ( status : StatusCode ) {
970980 wrapper ( |env| {
981+ env. override_config ( |config| {
982+ config. crates_io_api_call_retries = 0 ;
983+ } ) ;
984+
971985 let _m = mock ( "GET" , "/api/v1/crates" )
972986 . match_query ( Matcher :: AllOf ( vec ! [
973987 Matcher :: UrlEncoded ( "q" . into( ) , "doesnt_matter_here" . into( ) ) ,
0 commit comments