throttled-reader 1.0.1

An io::Read proxy that limits calls to read()
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented1 out of 7 items with examples
  • Size
  • Source code size: 20.6 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.71 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • jonhoo/throttled-reader
    7 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jonhoo

throttled-reader

Crates.io Documentation Build Status

This crate provides ThrottledReader, a proxy-type for io::Read that limits how many times the underlying reader can be read from. If the read budget is exceeded, io::ErrorKind::WouldBlock is returned instead. This type can be useful to enforce fairness when reading from many (potentially asynchronous) input streams with highly varying load. If one stream always has data available, a worker may continue consuming its input forever, neglecting the other stream.

Examples

let mut buf = [0];
let mut stream = ThrottledReader::new(io::empty());

// initially no limit
assert!(stream.read(&mut buf).is_ok());
assert!(stream.read(&mut buf).is_ok());

// set a limit
stream.set_limit(2);
assert!(stream.read(&mut buf).is_ok()); // first is allowed through
assert!(stream.read(&mut buf).is_ok()); // second is also allowed through
// but now the limit is reached, and the underlying stream is no longer accessible
assert_eq!(
    stream.read(&mut buf).unwrap_err().kind(),
    io::ErrorKind::WouldBlock
);

// we can then unthrottle it again after checking other streams
stream.unthrottle();
assert!(stream.read(&mut buf).is_ok());
assert!(stream.read(&mut buf).is_ok());