#io-read #io #proxy #throttling #wrapper

throttled-reader

一个io::Read代理,限制read()调用的次数

2个稳定版本

使用旧的Rust 2015

1.0.1 2018年12月4日
1.0.0 2018年2月8日

#4 in #throttling

MIT/Apache

9KB
109

throttled-reader

Crates.io Documentation Build Status

这个crate提供了一个名为ThrottledReader的代理类型,用于限制底层reader可被读取的次数。如果读取预算超出,则返回io::ErrorKind::WouldBlock。这种类型在从许多(可能是异步的)具有高度变化负载的输入流中强制公平性时可能很有用。如果一个流始终有数据可用,一个工作进程可能永远继续消费其输入,而忽略其他流。

示例

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());

无运行时依赖