pub struct DistributedSlidingWindowSpec { /* private fields */ }Expand description
Distributed sliding window implementation
Uses sorted sets in a distributed backend (e.g., Redis ZSET) to implement sliding window algorithm across multiple workers.
§Redis Implementation
The sliding window is implemented using Redis sorted set:
{key}:window- Sorted set of timestamps (score = timestamp, member = UUID)
Operations:
- Acquire: Add current timestamp to sorted set if count < limit
- Cleanup: Remove timestamps outside the window using ZREMRANGEBYSCORE
- Count: Count timestamps within window using ZCOUNT
§Example Lua Script
local window_key = KEYS[1]
local now = tonumber(ARGV[1])
local window_size = tonumber(ARGV[2])
local max_count = tonumber(ARGV[3])
local uuid = ARGV[4]
local cutoff = now - window_size * 1000
redis.call('ZREMRANGEBYSCORE', window_key, '-inf', cutoff)
local count = redis.call('ZCARD', window_key)
if count < max_count then
redis.call('ZADD', window_key, now, uuid)
redis.call('EXPIRE', window_key, window_size * 2)
return 1
else
return 0
endImplementations§
Source§impl DistributedSlidingWindowSpec
impl DistributedSlidingWindowSpec
Sourcepub fn new(key: String, config: RateLimitConfig) -> Self
pub fn new(key: String, config: RateLimitConfig) -> Self
Create a new distributed sliding window specification
Sourcepub fn lua_acquire_script() -> &'static str
pub fn lua_acquire_script() -> &'static str
Get the Lua script for atomic window acquisition
Sourcepub fn lua_available_script() -> &'static str
pub fn lua_available_script() -> &'static str
Get the Lua script for querying available permits
Sourcepub fn lua_time_until_script() -> &'static str
pub fn lua_time_until_script() -> &'static str
Get the Lua script for querying time until available
Sourcepub fn max_executions(&self) -> usize
pub fn max_executions(&self) -> usize
Get the maximum number of executions allowed in the window
Sourcepub fn state(&self) -> &DistributedRateLimiterState
pub fn state(&self) -> &DistributedRateLimiterState
Get the state for implementing the distributed backend
Sourcepub fn try_acquire_fallback(&self) -> bool
pub fn try_acquire_fallback(&self) -> bool
Try to acquire using local fallback
Trait Implementations§
Source§impl Clone for DistributedSlidingWindowSpec
impl Clone for DistributedSlidingWindowSpec
Source§fn clone(&self) -> DistributedSlidingWindowSpec
fn clone(&self) -> DistributedSlidingWindowSpec
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for DistributedSlidingWindowSpec
impl RefUnwindSafe for DistributedSlidingWindowSpec
impl Send for DistributedSlidingWindowSpec
impl Sync for DistributedSlidingWindowSpec
impl Unpin for DistributedSlidingWindowSpec
impl UnwindSafe for DistributedSlidingWindowSpec
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