Skip to content

Commit 190f8f1

Browse files
committed
Use Duration not f64
rust-lang/rust#54361 stabilised :)
1 parent 26e156f commit 190f8f1

File tree

1 file changed

+14
-21
lines changed
  • src/rust/engine/serverset/src

1 file changed

+14
-21
lines changed

src/rust/engine/serverset/src/lib.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,40 +136,39 @@ pub enum Health {
136136
Unhealthy,
137137
}
138138

139-
// Ideally this would use Durations when https://github.com/rust-lang/rust/issues/54361 stabilises.
140139
#[derive(Clone, Copy, Debug)]
141140
struct UnhealthyInfo {
142141
unhealthy_since: Instant,
143-
next_attempt_after_millis: f64,
142+
next_attempt_after: Duration,
144143
}
145144

146145
impl UnhealthyInfo {
147146
fn new(backoff_config: BackoffConfig) -> UnhealthyInfo {
148147
UnhealthyInfo {
149148
unhealthy_since: Instant::now(),
150-
next_attempt_after_millis: backoff_config.initial_lame_millis as f64,
149+
next_attempt_after: backoff_config.initial_lame,
151150
}
152151
}
153152

154153
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
156155
}
157156

158157
fn increase_backoff(&mut self, backoff_config: BackoffConfig) {
159158
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+
));
164163
}
165164

166165
fn decrease_backoff(mut self, backoff_config: BackoffConfig) -> Option<UnhealthyInfo> {
167166
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() {
170169
None
171170
} else {
172-
self.next_attempt_after_millis = next_value;
171+
self.next_attempt_after = Duration::from_secs_f64(next_value);
173172
Some(self)
174173
}
175174
}
@@ -191,13 +190,12 @@ impl Backend {
191190
}
192191
}
193192

194-
// Ideally this would use Durations when https://github.com/rust-lang/rust/issues/54361 stabilises.
195193
#[derive(Clone, Copy, Debug)]
196194
pub struct BackoffConfig {
197195
///
198196
/// The time a backend will be skipped after it is first reported unhealthy.
199197
///
200-
initial_lame_millis: f64,
198+
initial_lame: Duration,
201199

202200
///
203201
/// Ratio by which to multiply the most recent lame duration if a backend continues to be
@@ -209,7 +207,7 @@ pub struct BackoffConfig {
209207
///
210208
/// Maximum duration to wait between attempts.
211209
///
212-
max_lame_millis: f64,
210+
max_lame: Duration,
213211
}
214212

215213
impl BackoffConfig {
@@ -225,15 +223,10 @@ impl BackoffConfig {
225223
));
226224
}
227225

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-
233226
Ok(BackoffConfig {
234-
initial_lame_millis,
227+
initial_lame,
235228
ratio,
236-
max_lame_millis,
229+
max_lame,
237230
})
238231
}
239232
}

0 commit comments

Comments
 (0)