1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! No-op examples.

use crate::lib::*;
use crate::{
    algorithms::{Algorithm, RateLimitState},
    clock, DirectRateLimiter, InconsistentCapacity, NegativeMultiDecision,
};

/// The most naive implementation of a rate-limiter ever: Always
/// allows every cell through.
/// # Example
/// ```
/// use ratelimit_meter::DirectRateLimiter;
/// use ratelimit_meter::example_algorithms::Allower;
/// let mut allower = Allower::ratelimiter();
/// assert!(allower.check().is_ok());
/// ```
#[derive(Default, Copy, Clone, Debug)]
pub struct Allower {}

impl Allower {
    /// Return a rate-limiter that lies, i.e. that allows all requests
    /// through.
    pub fn ratelimiter() -> DirectRateLimiter<Allower, ForeverClock> {
        // These numbers are fake, but we make them up for convenience:
        DirectRateLimiter::per_second(nonzero!(1u32))
    }
}

impl RateLimitState<Allower, Always> for () {
    fn last_touched(&self, _params: &Allower) -> Option<Always> {
        None
    }
}

/// A non-error - the Allower example rate-limiter always returns a
/// positive result, so this error is never returned.
#[derive(Debug, PartialEq)]
pub enum Impossible {}

impl fmt::Display for Impossible {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "can't happen")
    }
}

impl Algorithm<Always> for Allower {
    type BucketState = ();
    type NegativeDecision = Impossible;

    fn construct(
        _capacity: NonZeroU32,
        _cell_weight: NonZeroU32,
        _per_time_unit: Duration,
    ) -> Result<Self, InconsistentCapacity> {
        Ok(Allower {})
    }

    /// Allows all cells through unconditionally.
    fn test_n_and_update(
        &self,
        _state: &Self::BucketState,
        _n: u32,
        _t0: Always,
    ) -> Result<(), NegativeMultiDecision<Impossible>> {
        Ok(())
    }
}

/// A pseudo-instant that never changes.
///
/// It is used to implement the `Allower` rate-limiter type, which
/// never denies any requests.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Always();
impl clock::Reference for Always {
    fn duration_since(&self, _other: Self) -> Duration {
        Duration::new(0, 0)
    }

    fn saturating_sub(&self, _: Duration) -> Self {
        *self
    }
}

impl Add<Duration> for Always {
    type Output = Always;
    fn add(self, _rhs: Duration) -> Always {
        Always()
    }
}

impl Sub<Duration> for Always {
    type Output = Always;
    fn sub(self, _rhs: Duration) -> Always {
        Always()
    }
}

#[derive(Default, Debug, Clone)]
pub struct ForeverClock();

impl ForeverClock {
    pub fn now() -> Always {
        Always()
    }
}

impl clock::Clock for ForeverClock {
    type Instant = Always;

    fn now(&self) -> Self::Instant {
        Always()
    }
}