use super::{RateLimitConfig, RateLimitResult, RateLimiter};
use crate::error::Result;
use async_trait::async_trait;
use dashmap::DashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tokio::time::interval;
#[derive(Clone)]
struct RateLimitEntry {
count: u32,
window_start: Instant,
expiry: Instant,
}
pub struct MemoryRateLimiter {
limits: Arc<DashMap<String, RateLimitEntry, ahash::RandomState>>,
config: RateLimitConfig,
#[allow(dead_code)]
cleanup_task: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
}
impl MemoryRateLimiter {
pub fn new(max_requests: u32, window_secs: u64) -> Self {
Self::with_config(RateLimitConfig {
max_requests,
window_secs,
identifier: Some("memory".to_string()),
})
}
pub fn with_config(config: RateLimitConfig) -> Self {
let limits: Arc<DashMap<String, RateLimitEntry, ahash::RandomState>> =
Arc::new(DashMap::with_hasher(ahash::RandomState::new()));
let limits_clone = Arc::clone(&limits);
let cleanup_task = tokio::spawn(async move {
let mut interval = interval(Duration::from_secs(10)); loop {
interval.tick().await;
let now = Instant::now();
limits_clone.retain(|_, value| value.expiry > now);
}
});
Self {
limits,
config,
cleanup_task: Arc::new(Mutex::new(Some(cleanup_task))),
}
}
}
#[async_trait]
impl RateLimiter for MemoryRateLimiter {
async fn check(&self, key: &str) -> Result<RateLimitResult> {
let now = Instant::now();
if let Some(entry) = self.limits.get(key) {
if entry.expiry <= now {
return Ok(RateLimitResult {
allowed: true,
remaining: self.config.max_requests,
reset_after: self.config.window_secs,
limit: self.config.max_requests,
});
}
let remaining = self.config.max_requests.saturating_sub(entry.count);
let reset_after = entry.expiry.saturating_duration_since(now).as_secs();
Ok(RateLimitResult {
allowed: remaining > 0,
remaining,
reset_after,
limit: self.config.max_requests,
})
} else {
Ok(RateLimitResult {
allowed: true,
remaining: self.config.max_requests,
reset_after: self.config.window_secs,
limit: self.config.max_requests,
})
}
}
async fn increment(&self, key: &str) -> Result<RateLimitResult> {
let now = Instant::now();
let result = if let Some(mut entry) = self.limits.get_mut(key) {
if entry.expiry <= now {
entry.count = 1;
entry.window_start = now;
entry.expiry = now + Duration::from_secs(self.config.window_secs);
RateLimitResult {
allowed: true,
remaining: self.config.max_requests - 1,
reset_after: self.config.window_secs,
limit: self.config.max_requests,
}
} else {
let new_count = entry.count + 1;
entry.count = new_count;
let remaining = self.config.max_requests.saturating_sub(new_count);
let reset_after = entry.expiry.saturating_duration_since(now).as_secs();
RateLimitResult {
allowed: remaining > 0,
remaining,
reset_after,
limit: self.config.max_requests,
}
}
} else {
let entry = RateLimitEntry {
count: 1,
window_start: now,
expiry: now + Duration::from_secs(self.config.window_secs),
};
self.limits.insert(key.to_string(), entry);
RateLimitResult {
allowed: true,
remaining: self.config.max_requests - 1,
reset_after: self.config.window_secs,
limit: self.config.max_requests,
}
};
Ok(result)
}
async fn reset(&self, key: &str) -> Result<()> {
self.limits.remove(key);
Ok(())
}
async fn get_remaining(&self, key: &str) -> Result<u32> {
let now = Instant::now();
if let Some(entry) = self.limits.get(key) {
if entry.expiry <= now {
return Ok(self.config.max_requests);
}
let remaining = self.config.max_requests.saturating_sub(entry.count);
Ok(remaining)
} else {
Ok(self.config.max_requests)
}
}
}
impl Drop for MemoryRateLimiter {
fn drop(&mut self) {
if let Ok(mut task_guard) = self.cleanup_task.try_lock() {
if let Some(task) = task_guard.take() {
task.abort();
}
}
}
}