@@ -136,40 +136,39 @@ pub enum Health {
136
136
Unhealthy ,
137
137
}
138
138
139
- // Ideally this would use Durations when https://github.com/rust-lang/rust/issues/54361 stabilises.
140
139
#[ derive( Clone , Copy , Debug ) ]
141
140
struct UnhealthyInfo {
142
141
unhealthy_since : Instant ,
143
- next_attempt_after_millis : f64 ,
142
+ next_attempt_after : Duration ,
144
143
}
145
144
146
145
impl UnhealthyInfo {
147
146
fn new ( backoff_config : BackoffConfig ) -> UnhealthyInfo {
148
147
UnhealthyInfo {
149
148
unhealthy_since : Instant :: now ( ) ,
150
- next_attempt_after_millis : backoff_config. initial_lame_millis as f64 ,
149
+ next_attempt_after : backoff_config. initial_lame ,
151
150
}
152
151
}
153
152
154
153
fn healthy_at ( & self ) -> Instant {
155
- self . unhealthy_since + Duration :: from_millis ( self . next_attempt_after_millis as u64 )
154
+ self . unhealthy_since + self . next_attempt_after
156
155
}
157
156
158
157
fn increase_backoff ( & mut self , backoff_config : BackoffConfig ) {
159
158
self . unhealthy_since = Instant :: now ( ) ;
160
- self . next_attempt_after_millis = f64:: min (
161
- backoff_config. max_lame_millis ,
162
- self . next_attempt_after_millis * backoff_config. ratio ,
163
- ) ;
159
+ self . next_attempt_after = Duration :: from_secs_f64 ( f64:: min (
160
+ backoff_config. max_lame . as_secs_f64 ( ) ,
161
+ self . next_attempt_after . as_secs_f64 ( ) * backoff_config. ratio ,
162
+ ) ) ;
164
163
}
165
164
166
165
fn decrease_backoff ( mut self , backoff_config : BackoffConfig ) -> Option < UnhealthyInfo > {
167
166
self . unhealthy_since = Instant :: now ( ) ;
168
- let next_value = self . next_attempt_after_millis / backoff_config. ratio ;
169
- if next_value < backoff_config. initial_lame_millis {
167
+ let next_value = self . next_attempt_after . as_secs_f64 ( ) / backoff_config. ratio ;
168
+ if next_value < backoff_config. initial_lame . as_secs_f64 ( ) {
170
169
None
171
170
} else {
172
- self . next_attempt_after_millis = next_value;
171
+ self . next_attempt_after = Duration :: from_secs_f64 ( next_value) ;
173
172
Some ( self )
174
173
}
175
174
}
@@ -191,13 +190,12 @@ impl Backend {
191
190
}
192
191
}
193
192
194
- // Ideally this would use Durations when https://github.com/rust-lang/rust/issues/54361 stabilises.
195
193
#[ derive( Clone , Copy , Debug ) ]
196
194
pub struct BackoffConfig {
197
195
///
198
196
/// The time a backend will be skipped after it is first reported unhealthy.
199
197
///
200
- initial_lame_millis : f64 ,
198
+ initial_lame : Duration ,
201
199
202
200
///
203
201
/// Ratio by which to multiply the most recent lame duration if a backend continues to be
@@ -209,7 +207,7 @@ pub struct BackoffConfig {
209
207
///
210
208
/// Maximum duration to wait between attempts.
211
209
///
212
- max_lame_millis : f64 ,
210
+ max_lame : Duration ,
213
211
}
214
212
215
213
impl BackoffConfig {
@@ -225,15 +223,10 @@ impl BackoffConfig {
225
223
) ) ;
226
224
}
227
225
228
- let initial_lame_millis =
229
- initial_lame. as_secs ( ) as f64 * 1000_f64 + f64:: from ( initial_lame. subsec_millis ( ) ) ;
230
- let max_lame_millis =
231
- max_lame. as_secs ( ) as f64 * 1000_f64 + f64:: from ( max_lame. subsec_millis ( ) ) ;
232
-
233
226
Ok ( BackoffConfig {
234
- initial_lame_millis ,
227
+ initial_lame ,
235
228
ratio,
236
- max_lame_millis ,
229
+ max_lame ,
237
230
} )
238
231
}
239
232
}
0 commit comments