Trait Store

Source
pub trait Store {
    // Required methods
    fn compare_and_swap_with_ttl(
        &mut self,
        key: &str,
        old: i64,
        new: i64,
        ttl: Duration,
    ) -> Result<bool, CellError>;
    fn get_with_time(&self, key: &str) -> Result<(i64, Tm), CellError>;
    fn log_debug(&self, message: &str);
    fn set_if_not_exists_with_ttl(
        &mut self,
        key: &str,
        value: i64,
        ttl: Duration,
    ) -> Result<bool, CellError>;
}
Expand description

Store exposes the atomic data store operations that the GCRA rate limiter needs to function correctly.

Note that because the default mode for this library is to run within Redis (making every operation atomic by default), the encapsulation is not strictly needed. However, it’s written to be generic enough that a out-of-Redis Store could be written and have the rate limiter still work properly.

Required Methods§

Source

fn compare_and_swap_with_ttl( &mut self, key: &str, old: i64, new: i64, ttl: Duration, ) -> Result<bool, CellError>

Compares the value at the given key with a known old value and swaps it for a new value if and only if they’re equal. Also sets the key’s TTL until it expires.

Source

fn get_with_time(&self, key: &str) -> Result<(i64, Tm), CellError>

Gets the given key’s value and the current time as dictated by the store (this is done so that rate limiters running on a variety of different nodes can operate with a consistent clock instead of using their own). If the key was unset, -1 is returned.

Source

fn log_debug(&self, message: &str)

Logs a debug message to the data store.

Source

fn set_if_not_exists_with_ttl( &mut self, key: &str, value: i64, ttl: Duration, ) -> Result<bool, CellError>

Sets the given key to the given value if and only if it doesn’t already exit. Whether or not the key existed previously it’s given a new TTL.

Implementors§