pub struct TimeWindow { /* private fields */ }Expand description
A time based window implementation. That will keep track of a total maximum over a given window of time.
Implementations§
Source§impl TimeWindow
impl TimeWindow
Sourcepub fn new(size: usize, slot_time: u64, max: u64) -> Self
pub fn new(size: usize, slot_time: u64, max: u64) -> Self
Creates a new time based window. The arguments are as following:
size- the number of slots to allocateslot_time- the time allowed to spend in each slotmax- the maximum allowed over the entire period.
§Example
to create a window that allows for 1000 entries per second and
keeps track of it at a granularity of 100 ms we would use:
use window::TimeWindow;
TimeWindow::new(10, 100, 1000);pub fn max(&self) -> u64
pub fn set_max(&mut self, max: u64) -> &Self
Sourcepub fn inc(&mut self) -> Result<u64, u64>
pub fn inc(&mut self) -> Result<u64, u64>
Tries to increment the counter by 1. See SlidingWindow::inc for details.
§Errors
if we would overshoot the maximum, the error is the number over max.
Sourcepub fn inc_t(&mut self, now: u64) -> Result<u64, u64>
pub fn inc_t(&mut self, now: u64) -> Result<u64, u64>
Tries to increment the counter by 1. See SlidingWindow::inc for details.
§Errors
if we would overshoot the maximum, the error is the number over max.
Sourcepub fn add(&mut self, n: u64) -> Result<u64, u64>
pub fn add(&mut self, n: u64) -> Result<u64, u64>
Tries to increment the counter by n. See SlidingWindow::add for details.
§Errors
if we would overshoot the maximum, the error is the number over max.
Sourcepub fn add_t(&mut self, now: u64, n: u64) -> Result<u64, u64>
pub fn add_t(&mut self, now: u64, n: u64) -> Result<u64, u64>
Tries to increment the counter by n. using a given time for now.
See SlidingWindow::add for details.
§Errors
if we would overshoot the maximum, the error is the number over max.