pub struct Limiter<S>{
pub stream: S,
pub read_opt: Option<LimiterOptions>,
pub write_opt: Option<LimiterOptions>,
/* private fields */
}Expand description
A Limiter is a wrapper around a stream that implement Read and Write that limits the rate at which it can be read or written.
The rate is given in byte/s.
Fields§
§stream: S§read_opt: Option<LimiterOptions>§write_opt: Option<LimiterOptions>Implementations§
Source§impl<S> Limiter<S>
impl<S> Limiter<S>
Sourcepub fn new(
stream: S,
read_opt: Option<LimiterOptions>,
write_opt: Option<LimiterOptions>,
) -> Limiter<S> ⓘ
pub fn new( stream: S, read_opt: Option<LimiterOptions>, write_opt: Option<LimiterOptions>, ) -> Limiter<S> ⓘ
Create a new Limiter with the given stream and rate limiting:
window_length: The number of bytes that can be read or written in a given time window.window_time: The time window in whichwindow_lengthbytes can be read or written.
We initialize the limiter as if one period has already passed so that the first read/write is instant.
pub fn get_stream(self) -> S
pub fn limits(&self) -> (bool, bool)
pub fn read_instant(&mut self, buf: &mut [u8]) -> Result<usize>
pub fn write_instant(&mut self, buf: &[u8]) -> Result<usize>
Trait Implementations§
Source§impl<S> Read for Limiter<S>
impl<S> Read for Limiter<S>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Read a stream at a given rate. If the rate is 1 byte/s, it will take 1 second to read 1 byte. (except the first time which is instant) If you didn’t read for 10 secondes in this stream and you try to read 10 bytes, it will read instantly.
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read moreSource§impl<S> Write for Limiter<S>
impl<S> Write for Limiter<S>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a stream at a given rate. If the rate is 1 byte/s, it will take 1 second to write 1 byte. (except the first time which is instant) If you didn’t write for 10 secondes in this stream and you try to write 10 bytes, it will write instantly.
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)