pub use crate::shared::rate_limit::ConnectionRateLimiter;
pub use crate::shared::rate_limit::RateLimitConfig;
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[tokio::test]
async fn test_rate_limiter_allows_burst() {
let limiter = ConnectionRateLimiter::with_simple_config(3, 1.0);
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_ok());
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_ok());
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_ok());
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_err());
}
#[tokio::test]
async fn test_rate_limiter_refills() {
let limiter = ConnectionRateLimiter::with_simple_config(2, 10.0);
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_ok());
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_ok());
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_err());
tokio::time::sleep(Duration::from_millis(150)).await;
assert!(limiter.try_acquire(&"test.com".to_string()).await.is_ok());
}
#[tokio::test]
async fn test_rate_limiter_per_host() {
let limiter = ConnectionRateLimiter::with_simple_config(1, 1.0);
assert!(limiter.try_acquire(&"host1.com".to_string()).await.is_ok());
assert!(limiter.try_acquire(&"host2.com".to_string()).await.is_ok());
assert!(limiter.try_acquire(&"host1.com".to_string()).await.is_err());
}
}