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
impl ProbabilisticStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new ProbabilisticStore with default configuration
Uses a default capacity of 1000 entries and cleanup probability of 1/1000.
Sourcepub fn with_capacity(capacity: usize) -> Self
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
Sourcepub fn builder() -> ProbabilisticStoreBuilder
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
impl Default for ProbabilisticStore
Source§impl Store for ProbabilisticStore
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>
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
Auto Trait Implementations§
impl Freeze for ProbabilisticStore
impl RefUnwindSafe for ProbabilisticStore
impl Send for ProbabilisticStore
impl Sync for ProbabilisticStore
impl Unpin for ProbabilisticStore
impl UnwindSafe for ProbabilisticStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more