pub struct RedisRateLimiter { /* private fields */ }Available on crate features
resilience and resilience-redis only.Expand description
Redis-backed sliding window rate limiter.
Uses Redis sorted sets to implement a sliding window rate limiter that works across distributed deployments.
§Algorithm
Uses the sliding window log algorithm:
- Remove timestamps older than the window
- Count remaining timestamps
- If under limit, add current timestamp
- Return allow/deny
§Features
- Distributed: Works across multiple instances
- Sliding window: More accurate than fixed windows
- Auto-cleanup: Old entries are automatically removed
Implementations§
Source§impl RedisRateLimiter
impl RedisRateLimiter
Sourcepub async fn new(
redis_url: &str,
max_requests: u32,
window_seconds: u64,
) -> Result<Self, RedisRateLimiterError>
pub async fn new( redis_url: &str, max_requests: u32, window_seconds: u64, ) -> Result<Self, RedisRateLimiterError>
Create a new Redis rate limiter.
§Arguments
redis_url- Redis connection URL (e.g., “redis://localhost:6379”)max_requests- Maximum requests per windowwindow_seconds- Window duration in seconds
Sourcepub async fn with_config(
redis_url: &str,
config: RedisRateLimiterConfig,
) -> Result<Self, RedisRateLimiterError>
pub async fn with_config( redis_url: &str, config: RedisRateLimiterConfig, ) -> Result<Self, RedisRateLimiterError>
Create a new Redis rate limiter with custom configuration.
Sourcepub fn from_connection(
conn: ConnectionManager,
config: RedisRateLimiterConfig,
) -> Self
pub fn from_connection( conn: ConnectionManager, config: RedisRateLimiterConfig, ) -> Self
Create from an existing Redis connection manager.
Sourcepub async fn check(&self, key: &str) -> Result<u32, RateLimitError>
pub async fn check(&self, key: &str) -> Result<u32, RateLimitError>
Check if a request for the given key is allowed.
Returns Ok(remaining) with the number of remaining requests if allowed,
or Err(RateLimitError) if rate limited.
Sourcepub async fn get_count(&self, key: &str) -> Result<u32, RedisRateLimiterError>
pub async fn get_count(&self, key: &str) -> Result<u32, RedisRateLimiterError>
Get the current count for a key without incrementing.
Sourcepub async fn get_remaining(
&self,
key: &str,
) -> Result<u32, RedisRateLimiterError>
pub async fn get_remaining( &self, key: &str, ) -> Result<u32, RedisRateLimiterError>
Get remaining requests for a key.
Sourcepub async fn reset(&self, key: &str) -> Result<(), RedisRateLimiterError>
pub async fn reset(&self, key: &str) -> Result<(), RedisRateLimiterError>
Reset the rate limit for a key.
Sourcepub fn config(&self) -> &RedisRateLimiterConfig
pub fn config(&self) -> &RedisRateLimiterConfig
Get the configuration.
Auto Trait Implementations§
impl Freeze for RedisRateLimiter
impl !RefUnwindSafe for RedisRateLimiter
impl Send for RedisRateLimiter
impl Sync for RedisRateLimiter
impl Unpin for RedisRateLimiter
impl !UnwindSafe for RedisRateLimiter
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request