fastly 0.12.0

Fastly Compute API
Documentation
//! Edge rate limiter and supporting functions.
//!
pub(crate) mod handle;

use std::time::Duration;

pub use handle::ERLError;
use handle::{ERLHandle, PenaltyboxHandle, RateCounterHandle};

/// An edge rate limiter which includes a ratecounter and a penaltybox.
///
pub struct ERL {
    handle: ERLHandle,
}

/// A rate counter that can be used with a edge rate limiter or stand alone for counting and rate calculations
///
pub struct RateCounter {
    handle: RateCounterHandle,
}

/// To be used for picking the window in a rate counter lookup_rate or a ERL check_rate
///
#[repr(u32)]
pub enum RateWindow {
    /// A one secound rate window
    OneSec = 1,
    /// A ten secound rate window
    TenSecs = 10,
    /// A sixty secound rate window
    SixtySecs = 60,
}

/// To be used for picking the duration in a rate counter lookup_count call
///
#[repr(u32)]
pub enum CounterDuration {
    /// A one second counter window
    TenSec = 10,
    /// A twenty second counter window
    TwentySecs = 20,
    /// A thirty second counter window
    ThirtySecs = 30,
    /// A forty second counter window
    FortySecs = 40,
    /// A fifty second counter window
    FiftySecs = 50,
    /// A sixty second counter window
    SixtySecs = 60,
}

/// A penaltybox that can be used with the edge rate limiter or stand alone for adding and checking if some entry is in the data set.
///
pub struct Penaltybox {
    handle: PenaltyboxHandle,
}

impl ERL {
    /// Open a ERL with the given ratecounter and penaltybox.
    ///
    pub fn open(ratecounter: RateCounter, penaltybox: Penaltybox) -> Self {
        Self {
            handle: ERLHandle::open(ratecounter, penaltybox),
        }
    }

    /// Increment an entry in a rate counter and check if the client has exceeded some average number of requests per second (RPS) over the window.
    /// If the client is over the rps limit for the window, add to the penaltybox for ttl.
    /// Valid ttl span is 1m to 1h and TTL value is truncated to the nearest minute.
    ///
    pub fn check_rate(
        &self,
        entry: &str,
        delta: u32,
        window: RateWindow,
        limit: u32,
        ttl: Duration,
    ) -> Result<bool, ERLError> {
        self.handle.check_rate(entry, delta, window, limit, ttl)
    }
}

impl RateCounter {
    /// Open a RateCounter with the given name for a ratecounter.
    ///
    pub fn open(ratecounter_name: &str) -> Self {
        Self {
            handle: RateCounterHandle::open(ratecounter_name),
        }
    }

    /// Increment an entry in the ratecounter by delta.
    ///
    pub fn increment(&self, entry: &str, delta: u32) -> Result<(), ERLError> {
        self.handle.increment(entry, delta)
    }

    /// Lookup the current rate for entry in the ratecounter for a window.
    ///
    pub fn lookup_rate(&self, entry: &str, window: RateWindow) -> Result<u32, ERLError> {
        self.handle.lookup_rate(entry, window)
    }

    /// Lookup the current count for entry in the ratecounter for duration.
    ///
    pub fn lookup_count(&self, entry: &str, duration: CounterDuration) -> Result<u32, ERLError> {
        self.handle.lookup_count(entry, duration)
    }
}

impl Penaltybox {
    /// Open a Penaltybox identified by the given name.
    ///
    pub fn open(penaltybox_name: &str) -> Self {
        Self {
            handle: PenaltyboxHandle::open(penaltybox_name),
        }
    }

    /// Add entry to a the penaltybox for the duration of ttl.
    /// Valid ttl span is 1m to 1h and TTL value is truncated to the nearest minute.
    ///
    pub fn add(&self, entry: &str, ttl: Duration) -> Result<(), ERLError> {
        self.handle.add(entry, ttl)
    }

    /// Check if entry is in the penaltybox.
    ///  
    pub fn has(&self, entry: &str) -> Result<bool, ERLError> {
        self.handle.has(entry)
    }
}