1
1
use std:: error:: Error ;
2
2
use std:: fmt:: Debug ;
3
3
use std:: fmt:: Write ;
4
+ use std:: num:: ParseIntError ;
4
5
use std:: path:: Path ;
5
6
use std:: sync:: Arc ;
6
7
use std:: time:: Duration ;
7
8
use std:: { env, io, iter} ;
8
9
9
- use anyhow:: Context ;
10
10
use anyhow:: anyhow;
11
11
use http:: {
12
12
HeaderMap , HeaderName , HeaderValue , Method , StatusCode ,
@@ -22,6 +22,7 @@ use reqwest_retry::policies::ExponentialBackoff;
22
22
use reqwest_retry:: {
23
23
DefaultRetryableStrategy , RetryTransientMiddleware , Retryable , RetryableStrategy ,
24
24
} ;
25
+ use thiserror:: Error ;
25
26
use tracing:: { debug, trace} ;
26
27
use url:: ParseError ;
27
28
use url:: Url ;
@@ -42,7 +43,9 @@ use crate::linehaul::LineHaul;
42
43
use crate :: middleware:: OfflineMiddleware ;
43
44
use crate :: tls:: read_identity;
44
45
46
+ /// Do not use this value directly outside tests, use [`retries_from_env`] instead.
45
47
pub const DEFAULT_RETRIES : u32 = 3 ;
48
+
46
49
/// Maximum number of redirects to follow before giving up.
47
50
///
48
51
/// This is the default used by [`reqwest`].
@@ -169,23 +172,13 @@ impl<'a> BaseClientBuilder<'a> {
169
172
self
170
173
}
171
174
172
- /// Read the retry count from [`EnvVars::UV_HTTP_RETRIES`] if set, otherwise, make no change.
175
+ /// Read the retry count from [`EnvVars::UV_HTTP_RETRIES`] if set, otherwise use the default
176
+ /// retries.
173
177
///
174
178
/// Errors when [`EnvVars::UV_HTTP_RETRIES`] is not a valid u32.
175
- pub fn retries_from_env ( self ) -> anyhow:: Result < Self > {
176
- // TODO(zanieb): We should probably parse this in another layer, but there's not a natural
177
- // fit for it right now
178
- if let Some ( value) = env:: var_os ( EnvVars :: UV_HTTP_RETRIES ) {
179
- Ok ( self . retries (
180
- value
181
- . to_string_lossy ( )
182
- . as_ref ( )
183
- . parse :: < u32 > ( )
184
- . context ( "Failed to parse `UV_HTTP_RETRIES`" ) ?,
185
- ) )
186
- } else {
187
- Ok ( self )
188
- }
179
+ pub fn retries_from_env ( mut self ) -> Result < Self , RetryParsingError > {
180
+ self . retries = retries_from_env ( ) ?;
181
+ Ok ( self )
189
182
}
190
183
191
184
#[ must_use]
@@ -982,6 +975,26 @@ fn find_sources<E: Error + 'static>(orig: &dyn Error) -> impl Iterator<Item = &E
982
975
iter:: successors ( find_source :: < E > ( orig) , |& err| find_source ( err) )
983
976
}
984
977
978
+ // TODO(konsti): Remove once we find a native home for `retries_from_env`
979
+ #[ derive( Debug , Error ) ]
980
+ pub enum RetryParsingError {
981
+ #[ error( "Failed to parse `UV_HTTP_RETRIES`" ) ]
982
+ ParseInt ( #[ from] ParseIntError ) ,
983
+ }
984
+
985
+ /// Read the retry count from [`EnvVars::UV_HTTP_RETRIES`] if set, otherwise, make no change.
986
+ ///
987
+ /// Errors when [`EnvVars::UV_HTTP_RETRIES`] is not a valid u32.
988
+ pub fn retries_from_env ( ) -> Result < u32 , RetryParsingError > {
989
+ // TODO(zanieb): We should probably parse this in another layer, but there's not a natural
990
+ // fit for it right now
991
+ if let Some ( value) = env:: var_os ( EnvVars :: UV_HTTP_RETRIES ) {
992
+ Ok ( value. to_string_lossy ( ) . as_ref ( ) . parse :: < u32 > ( ) ?)
993
+ } else {
994
+ Ok ( DEFAULT_RETRIES )
995
+ }
996
+ }
997
+
985
998
#[ cfg( test) ]
986
999
mod tests {
987
1000
use super :: * ;
0 commit comments