Skip to content

Commit

Permalink
Implicit error handling (#25)
Browse files Browse the repository at this point in the history
Add rand_core::impls::fill_via_try_fill with handling for errors.
  • Loading branch information
dhardy committed Nov 9, 2017
1 parent 6302f53 commit 4769224
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
36 changes: 36 additions & 0 deletions rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use core::intrinsics::transmute;
use core::slice;
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use Rng;

/// Implement `next_u64` via `next_u32`, little-endian order.
Expand Down Expand Up @@ -109,4 +110,39 @@ pub fn next_u128_via_fill<R: Rng+?Sized>(rng: &mut R) -> u128 {
impl_uint_from_fill!(rng, u128, 16)
}

const LIMIT_ERR_MAX: usize = 20; // arbitrary
static LIMIT_ERR_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

/// Implement `fill_bytes` via `try_fill` with implicit error handling.
pub fn fill_via_try_fill<R: Rng+?Sized>(rng: &mut R, dest: &mut [u8]) {
loop {
if let Err(e) = rng.try_fill(dest) {
if e.kind.should_retry() {
if e.kind.limit_retries() {
// We use a global counter since we don't have any local memory.
let count = LIMIT_ERR_COUNT.fetch_add(1, Ordering::Relaxed);
if count > LIMIT_ERR_MAX {
// TODO: log details & cause?
panic!("Too many RNG errors; last error: {}", e.msg());
}
}

if e.kind.should_wait() {
#[cfg(feature="std")]{
let dur = ::std::time::Duration::from_millis(10);
::std::thread::sleep(dur);
}
}

continue;
}

// TODO: log details & cause?
panic!("Fatal RNG error: {}", e.msg());
}

break;
}
}

// TODO: implement tests for the above
12 changes: 8 additions & 4 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,19 @@ impl ErrorKind {
_ => false,
}
}

/// True if we should retry but wait before retrying
///
/// This implies `should_retry()` is true.
pub fn should_wait(self) -> bool {
match self {
ErrorKind::NotReady => true,
_ => false,
}
self == ErrorKind::NotReady
}

/// True if we should limit the number of retries before giving up.
pub fn limit_retries(self) -> bool {
self == ErrorKind::Transient
}

/// A description of this error kind
pub fn description(self) -> &'static str {
match self {
Expand Down
4 changes: 2 additions & 2 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Rng for OsRng {
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
self.try_fill(dest).unwrap();
::rand_core::impls::fill_via_try_fill(self, dest)
}

fn try_fill(&mut self, v: &mut [u8]) -> Result<(), Error> {
Expand Down Expand Up @@ -152,7 +152,7 @@ mod imp {
continue;
} else {
return Err(Error::new_with_cause(
ErrorKind::Other,
ErrorKind::Unavailable,
"unexpected getrandom error",
err,
));
Expand Down
2 changes: 1 addition & 1 deletion src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<R: Read> Rng for ReadRng<R> {
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
self.try_fill(dest).unwrap();
::rand_core::impls::fill_via_try_fill(self, dest)
}

fn try_fill(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Expand Down

0 comments on commit 4769224

Please sign in to comment.