@@ -80,23 +80,23 @@ pub fn Ok<T>(value: T) -> Result<T> {
8080}
8181
8282pub struct RetryOptions {
83- pub retry_timeout : Duration ,
83+ pub retry_timeout : Option < Duration > ,
8484 pub initial_backoff : Duration ,
8585 pub max_backoff : Duration ,
8686}
8787
8888impl Default for RetryOptions {
8989 fn default ( ) -> Self {
9090 Self {
91- retry_timeout : DEFAULT_RETRY_TIMEOUT ,
91+ retry_timeout : Some ( DEFAULT_RETRY_TIMEOUT ) ,
9292 initial_backoff : Duration :: from_millis ( 100 ) ,
9393 max_backoff : Duration :: from_secs ( 10 ) ,
9494 }
9595 }
9696}
9797
9898pub static HEAVY_LOADED_OPTIONS : RetryOptions = RetryOptions {
99- retry_timeout : DEFAULT_RETRY_TIMEOUT ,
99+ retry_timeout : Some ( DEFAULT_RETRY_TIMEOUT ) ,
100100 initial_backoff : Duration :: from_secs ( 1 ) ,
101101 max_backoff : Duration :: from_secs ( 60 ) ,
102102} ;
@@ -110,8 +110,9 @@ pub async fn run<
110110 f : F ,
111111 options : & RetryOptions ,
112112) -> Result < Ok , Err > {
113- let start_time = Instant :: now ( ) ;
114- let deadline = start_time + options. retry_timeout ;
113+ let deadline = options
114+ . retry_timeout
115+ . map ( |timeout| Instant :: now ( ) + timeout) ;
115116 let mut backoff = options. initial_backoff ;
116117
117118 loop {
@@ -121,13 +122,22 @@ pub async fn run<
121122 if !err. is_retryable ( ) {
122123 return Result :: Err ( err) ;
123124 }
124- let now = Instant :: now ( ) ;
125- if now >= deadline {
126- return Result :: Err ( err) ;
125+ let mut sleep_duration = backoff;
126+ if let Some ( deadline) = deadline {
127+ let now = Instant :: now ( ) ;
128+ if now >= deadline {
129+ return Result :: Err ( err) ;
130+ }
131+ let remaining_time = deadline. saturating_duration_since ( now) ;
132+ if remaining_time < sleep_duration {
133+ sleep_duration = remaining_time;
134+ }
127135 }
128- trace ! ( "Will retry in {}ms for error: {}" , backoff. as_millis( ) , err) ;
129- let remaining_time = deadline. saturating_duration_since ( now) ;
130- let sleep_duration = std:: cmp:: min ( backoff, remaining_time) ;
136+ trace ! (
137+ "Will retry in {}ms for error: {}" ,
138+ sleep_duration. as_millis( ) ,
139+ err
140+ ) ;
131141 tokio:: time:: sleep ( sleep_duration) . await ;
132142 if backoff < options. max_backoff {
133143 backoff = std:: cmp:: min (
0 commit comments