Skip to content

Commit 73febcd

Browse files
fix: Check that jitter is not NaN instead of finiteness (#843)
Infinities are already covered by the bounds checks on jitter, so checking for finiteness is redundant. We should only need to check for NaN.
1 parent 719ec03 commit 73febcd

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

tower/src/retry/backoff.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ where
8282
/// - `max` == 0
8383
/// - `jitter` < `0.0`
8484
/// - `jitter` > `100.0`
85-
/// - `jitter` is not finite
85+
/// - `jitter` is NaN
8686
pub fn new(
8787
min: time::Duration,
8888
max: time::Duration,
@@ -101,8 +101,8 @@ where
101101
if jitter > 100.0 {
102102
return Err(InvalidBackoff("jitter must not be greater than 100"));
103103
}
104-
if !jitter.is_finite() {
105-
return Err(InvalidBackoff("jitter must be finite"));
104+
if jitter.is_nan() {
105+
return Err(InvalidBackoff("jitter must not be NaN"));
106106
}
107107

108108
Ok(ExponentialBackoffMaker {
@@ -260,4 +260,20 @@ mod tests {
260260
}
261261
}
262262
}
263+
264+
#[test]
265+
fn jitter_must_be_finite() {
266+
let min = time::Duration::from_millis(0);
267+
let max = time::Duration::from_millis(1);
268+
let rng = HasherRng::default();
269+
270+
for n in [f64::INFINITY, f64::NEG_INFINITY, f64::NAN] {
271+
let result = ExponentialBackoffMaker::new(min, max, n, rng.clone());
272+
assert!(
273+
matches!(result, Err(InvalidBackoff(_))),
274+
"{} should be an invalid jitter",
275+
n
276+
);
277+
}
278+
}
263279
}

0 commit comments

Comments
 (0)