@@ -5,7 +5,7 @@ use crate::{
5
5
cdn,
6
6
db:: Pool ,
7
7
impl_axum_webpage,
8
- utils:: { report_error, spawn_blocking} ,
8
+ utils:: { report_error, retry_async , spawn_blocking} ,
9
9
web:: {
10
10
axum_parse_uri_with_params, axum_redirect, encode_url_path,
11
11
error:: { AxumNope , AxumResult } ,
@@ -128,7 +128,11 @@ struct SearchResult {
128
128
/// Get the search results for a crate search query
129
129
///
130
130
/// 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 > {
132
136
#[ derive( Deserialize ) ]
133
137
struct CratesIoSearchResult {
134
138
crates : Vec < CratesIoCrate > ,
@@ -178,13 +182,19 @@ async fn get_search_results(pool: Pool, query_params: &str) -> Result<SearchResu
178
182
}
179
183
} ) ;
180
184
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 ?;
188
198
189
199
let names = Arc :: new (
190
200
releases
@@ -584,14 +594,14 @@ pub(crate) async fn search_handler(
584
594
return Err ( AxumNope :: NoResults ) ;
585
595
}
586
596
587
- get_search_results ( pool, & query_params) . await ?
597
+ get_search_results ( pool, & config , & query_params) . await ?
588
598
} else if !query. is_empty ( ) {
589
599
let query_params: String = form_urlencoded:: Serializer :: new ( String :: new ( ) )
590
600
. append_pair ( "q" , & query)
591
601
. append_pair ( "per_page" , & RELEASES_IN_RELEASES . to_string ( ) )
592
602
. finish ( ) ;
593
603
594
- get_search_results ( pool, & format ! ( "?{}" , & query_params) ) . await ?
604
+ get_search_results ( pool, & config , & format ! ( "?{}" , & query_params) ) . await ?
595
605
} else {
596
606
return Err ( AxumNope :: NoResults ) ;
597
607
} ;
@@ -968,6 +978,10 @@ mod tests {
968
978
#[ test_case( StatusCode :: BAD_GATEWAY ) ]
969
979
fn crates_io_errors_are_correctly_returned_and_we_dont_try_parsing ( status : StatusCode ) {
970
980
wrapper ( |env| {
981
+ env. override_config ( |config| {
982
+ config. crates_io_api_call_retries = 0 ;
983
+ } ) ;
984
+
971
985
let _m = mock ( "GET" , "/api/v1/crates" )
972
986
. match_query ( Matcher :: AllOf ( vec ! [
973
987
Matcher :: UrlEncoded ( "q" . into( ) , "doesnt_matter_here" . into( ) ) ,
0 commit comments