neighborly 0.0.1

Tools for managing distributed workloads.
Documentation
/// Util API

use tokio::time::Instant;

use super::SimpleRateLimiter;

impl SimpleRateLimiter {
    /// Remove expired timestamps from the history.
    pub fn clean(&self) {
        let mut history = self
            .history
            .lock()
            .expect("rate limiter mutex was poisoned");
        self.clean_history(&mut history, Instant::now());
    }
    /// Returns the number of scheduled, inflight requests. This is
    /// equal to the length of the underlying `VecDeque<Instant>` after
    /// discarding expired entries.
    pub fn len(&self) -> usize {
        let mut history = self
            .history
            .lock()
            .expect("rate limiter mutex was poisoned");
        self.clean_history(&mut history, Instant::now());
        history.len()
    }
    /// Remove all timestamps from the history. Once the history is wiped,
    /// we can no longer ensure we do not violate the rate limit.
    pub unsafe fn clear(&self) {
        let mut history = self
            .history
            .lock()
            .expect("rate limiter mutex was poisoned");
        history.clear();
    }
}