use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::Mutex;
const MAX_ENTRIES: usize = 100_000;
const WINDOW_SECS: u64 = 60;
#[derive(Clone)]
struct WindowEntry {
window_start: u64,
count: usize,
}
pub struct RateLimiter {
windows: Arc<Mutex<HashMap<String, WindowEntry>>>,
#[allow(dead_code)]
shutdown_tx: Arc<tokio::sync::oneshot::Sender<()>>,
db_pool: Option<sqlx::SqlitePool>,
persist: bool,
}
fn now_secs() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}
impl RateLimiter {
pub fn new() -> Self {
Self::new_inner(None, false)
}
pub fn new_with_db(pool: sqlx::SqlitePool, persist: bool) -> Self {
Self::new_inner(Some(pool), persist)
}
fn new_inner(db_pool: Option<sqlx::SqlitePool>, persist: bool) -> Self {
let windows: Arc<Mutex<HashMap<String, WindowEntry>>> =
Arc::new(Mutex::new(HashMap::new()));
let windows_clone = windows.clone();
let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
loop {
tokio::select! {
_ = tokio::time::sleep(Duration::from_secs(WINDOW_SECS)) => {
let mut windows = windows_clone.lock().await;
let now = now_secs();
windows.retain(|_, entry| now.saturating_sub(entry.window_start) < WINDOW_SECS);
}
_ = &mut shutdown_rx => {
tracing::debug!("Rate limiter cleanup task shutting down");
break;
}
}
}
});
Self {
windows,
shutdown_tx: Arc::new(shutdown_tx),
db_pool,
persist,
}
}
pub async fn load_state(&self) {
let pool = match &self.db_pool {
Some(p) => p,
None => return,
};
if !self.persist {
return;
}
let rows: Vec<(String, i64, i64)> =
match sqlx::query_as("SELECT peer_ip, window_start, request_count FROM rate_limits")
.fetch_all(pool)
.await
{
Ok(r) => r,
Err(e) => {
tracing::warn!("Failed to load rate limit state: {}", e);
return;
}
};
let now = now_secs();
let mut windows = self.windows.lock().await;
let mut loaded = 0u32;
for (peer_ip, window_start, request_count) in rows {
if window_start < 0 || request_count <= 0 {
continue;
}
let Ok(ws) = u64::try_from(window_start) else {
continue;
};
let Ok(count) = usize::try_from(request_count) else {
continue;
};
if ws > now || now.saturating_sub(ws) >= WINDOW_SECS {
continue;
}
windows.insert(
peer_ip,
WindowEntry {
window_start: ws,
count,
},
);
loaded += 1;
}
tracing::info!("Loaded rate limit state for {} peers", loaded);
}
pub async fn save_state(&self) {
let pool = match &self.db_pool {
Some(p) => p,
None => return,
};
if !self.persist {
return;
}
let snapshot: Vec<(String, u64, usize)> = {
let windows = self.windows.lock().await;
windows
.iter()
.filter(|(_, entry)| entry.count > 0)
.map(|(k, e)| (k.clone(), e.window_start, e.count))
.collect()
};
let now = now_secs();
let mut tx = match pool.begin().await {
Ok(tx) => tx,
Err(e) => {
tracing::warn!("Failed to begin rate-limit persistence transaction: {}", e);
return;
}
};
for (key, window_start, count) in &snapshot {
if let Err(e) = sqlx::query(
"INSERT INTO rate_limits (peer_ip, window_start, request_count) VALUES (?, ?, ?)
ON CONFLICT(peer_ip) DO UPDATE SET
window_start = excluded.window_start,
request_count = excluded.request_count
WHERE excluded.window_start > rate_limits.window_start
OR (excluded.window_start = rate_limits.window_start
AND excluded.request_count >= rate_limits.request_count)",
)
.bind(key)
.bind(*window_start as i64)
.bind(*count as i64)
.execute(&mut *tx)
.await
{
tracing::warn!("Failed to save rate limit entry for {}: {}", key, e);
let _ = tx.rollback().await;
return;
}
}
if let Err(e) = sqlx::query("DELETE FROM rate_limits WHERE ? - window_start > ?")
.bind(now as i64)
.bind((WINDOW_SECS * 2) as i64)
.execute(&mut *tx)
.await
{
tracing::warn!("Failed to prune expired rate limit entries: {}", e);
let _ = tx.rollback().await;
return;
}
if let Err(e) = tx.commit().await {
tracing::warn!("Failed to commit rate-limit persistence transaction: {}", e);
}
}
pub fn start_persistence_task(
self: &Arc<Self>,
shutdown: impl std::future::Future<Output = ()> + Send + 'static,
) -> Option<tokio::task::JoinHandle<()>> {
if !self.persist || self.db_pool.is_none() {
return None;
}
let limiter = Arc::clone(self);
Some(tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(30));
tokio::pin!(shutdown);
loop {
tokio::select! {
_ = interval.tick() => {
limiter.save_state().await;
}
_ = &mut shutdown => {
tracing::debug!("Rate limiter persistence task shutting down");
limiter.save_state().await;
break;
}
}
}
}))
}
pub async fn allow(&self, key: &str, max_requests: usize, window: Duration) -> bool {
let now = now_secs();
let window_secs = window.as_secs();
let mut windows = self.windows.lock().await;
if windows.len() >= MAX_ENTRIES {
windows.retain(|_, entry| now.saturating_sub(entry.window_start) < window_secs);
}
if windows.len() >= MAX_ENTRIES {
let mut entries: Vec<(String, u64)> = windows
.iter()
.map(|(k, e)| (k.clone(), e.window_start))
.collect();
let mid = entries.len() / 2;
entries.select_nth_unstable_by(mid, |a, b| a.1.cmp(&b.1));
for (k, _) in entries.drain(..mid) {
windows.remove(&k);
}
}
let entry = windows
.entry(key.to_string())
.or_insert_with(|| WindowEntry {
window_start: now,
count: 0,
});
if now.saturating_sub(entry.window_start) >= window_secs {
entry.window_start = now;
entry.count = 0;
}
if entry.count >= max_requests {
return false;
}
entry.count += 1;
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_allow_within_limit() {
let limiter = RateLimiter::new();
assert!(
limiter
.allow("192.168.1.1", 5, Duration::from_secs(60))
.await
);
assert!(
limiter
.allow("192.168.1.1", 5, Duration::from_secs(60))
.await
);
assert!(
limiter
.allow("192.168.1.1", 5, Duration::from_secs(60))
.await
);
}
#[tokio::test]
async fn test_deny_at_limit() {
let limiter = RateLimiter::new();
for _ in 0..3 {
assert!(limiter.allow("10.0.0.1", 3, Duration::from_secs(60)).await);
}
assert!(!limiter.allow("10.0.0.1", 3, Duration::from_secs(60)).await);
}
#[tokio::test]
async fn test_separate_keys_independent() {
let limiter = RateLimiter::new();
for _ in 0..2 {
assert!(limiter.allow("key-a", 2, Duration::from_secs(60)).await);
}
assert!(!limiter.allow("key-a", 2, Duration::from_secs(60)).await);
assert!(limiter.allow("key-b", 2, Duration::from_secs(60)).await);
}
#[tokio::test]
async fn test_window_expiry_resets_count() {
let limiter = RateLimiter::new();
for _ in 0..2 {
assert!(
limiter
.allow("expire-test", 2, Duration::from_secs(1))
.await
);
}
assert!(
!limiter
.allow("expire-test", 2, Duration::from_secs(1))
.await
);
tokio::time::sleep(Duration::from_secs(2)).await;
assert!(
limiter
.allow("expire-test", 2, Duration::from_secs(1))
.await
);
}
#[tokio::test]
async fn test_zero_max_requests_deny_all() {
let limiter = RateLimiter::new();
assert!(!limiter.allow("zero", 0, Duration::from_secs(60)).await);
}
#[tokio::test]
async fn test_load_state_ignores_malformed_rows() {
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
sqlx::query(
"CREATE TABLE rate_limits (
peer_ip TEXT PRIMARY KEY,
window_start INTEGER NOT NULL,
request_count INTEGER NOT NULL
)",
)
.execute(&pool)
.await
.unwrap();
sqlx::query(
"INSERT INTO rate_limits (peer_ip, window_start, request_count)
VALUES ('negative', -1, 1), ('zero', 1, -1)",
)
.execute(&pool)
.await
.unwrap();
let limiter = RateLimiter::new_with_db(pool.clone(), true);
limiter.load_state().await;
assert!(limiter.allow("negative", 1, Duration::from_secs(60)).await);
assert!(limiter.allow("zero", 1, Duration::from_secs(60)).await);
assert!(limiter.allow("stale", 1, Duration::from_secs(60)).await);
limiter.save_state().await;
sqlx::query("UPDATE rate_limits SET request_count = 2 WHERE peer_ip = 'stale'")
.execute(&pool)
.await
.unwrap();
limiter.save_state().await;
let (count,): (i64,) =
sqlx::query_as("SELECT request_count FROM rate_limits WHERE peer_ip = 'stale'")
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(count, 2);
}
}