ProbabilisticStore

Struct ProbabilisticStore 

Source
pub struct ProbabilisticStore { /* private fields */ }
Expand description

Random-sampling cleanup store implementation

This store uses probabilistic cleanup where each operation has a small chance to trigger a cleanup cycle. Best suited for high-throughput applications where periodic cleanup would cause latency spikes.

§Features

  • Cleanup probability configurable (e.g., 1 in 10,000 operations)
  • Distributes cleanup cost across all operations
  • No periodic latency spikes
  • Excellent for very high request rates

§Example

use throttlecrab::{RateLimiter, ProbabilisticStore};
use std::time::SystemTime;

// Clean up with 1 in 5000 probability (0.02% chance per operation)
let store = ProbabilisticStore::builder()
    .cleanup_probability(5000)
    .build();
let mut limiter = RateLimiter::new(store);

§Cleanup Strategy

Uses a deterministic pseudo-random approach based on operation count, ensuring uniform distribution of cleanup operations over time.

Implementations§

Source§

impl ProbabilisticStore

Source

pub fn new() -> Self

Create a new ProbabilisticStore with default configuration

Uses a default capacity of 1000 entries and cleanup probability of 1/1000.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new ProbabilisticStore with specified capacity

The store will allocate 30% more space to reduce hash collisions.

§Parameters
  • capacity: Expected number of unique keys to track
Source

pub fn builder() -> ProbabilisticStoreBuilder

Create a new builder for configuring a ProbabilisticStore

Provides fine-grained control over store configuration.

Trait Implementations§

Source§

impl Default for ProbabilisticStore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Store for ProbabilisticStore

Source§

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

Atomically compare and swap a value with TTL Read more
Source§

fn get(&self, key: &str, now: SystemTime) -> Result<Option<i64>, String>

Get the current value for a key Read more
Source§

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

Set a value with TTL if the key doesn’t exist Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.