Skip to content

Commit 41fc616

Browse files
committed
Tweak retry counter
1 parent 7f61e2f commit 41fc616

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/lib.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,9 @@ impl Rng for ThreadRng {
986986
/// `EntropyRng` uses the interface for random numbers provided by the operating
987987
/// system ([`OsRng`]). If that returns an error, it will fall back to the
988988
/// [`JitterRng`] entropy collector. Occasionally it will then check if `OsRng`
989-
/// is still not available, and switch back if possible.
989+
/// is still not available, and switch back if possible. Once every 8 calls to
990+
/// `try_fill_bytes` if `OsRng` failed with `ErrorKind:Unavailable`, otherwise
991+
/// with every call.
990992
///
991993
/// [`OsRng`]: os/struct.OsRng.html
992994
/// [`JitterRng`]: jitter/struct.JitterRng.html
@@ -1074,22 +1076,28 @@ impl Rng for EntropyRng {
10741076
}
10751077
}
10761078
EntropySource::Jitter(ref mut rng) => {
1077-
if self.counter >= 8 {
1078-
if let Ok(os_rng) = try_os_new(dest) {
1079-
switch_rng = Some(EntropySource::Os(os_rng));
1080-
} else {
1081-
self.counter = (self.counter + 1) % 8;
1082-
return rng.try_fill_bytes(dest); // use JitterRng
1079+
if self.counter == 0 {
1080+
match try_os_new(dest) {
1081+
Ok(os_rng) => {
1082+
switch_rng = Some(EntropySource::Os(os_rng));
1083+
}
1084+
Err(e) => {
1085+
if e.kind() == ErrorKind::Unavailable {
1086+
self.counter = 8; // Wait 8 calls before retry
1087+
} else {
1088+
self.counter = 0; // Retry on next call
1089+
}
1090+
return rng.try_fill_bytes(dest); // use JitterRng
1091+
}
10831092
}
10841093
} else {
1085-
self.counter = (self.counter + 1) % 8;
1094+
self.counter -= 1;
10861095
return rng.try_fill_bytes(dest); // use JitterRng
10871096
}
10881097
}
10891098
}
10901099
if let Some(rng) = switch_rng {
10911100
self.rng = rng;
1092-
self.counter = 0;
10931101
}
10941102
Ok(())
10951103
}

0 commit comments

Comments
 (0)