use crate::domain::services::rate_limiting_service::{RateLimitResult, RateLimitingService};
use crate::presentation::errors::CrawlRsError;
use crate::presentation::handlers::response_builder;
use crate::presentation::middleware::rate_limit_middleware::RATE_LIMIT_FAIL_OPEN;
use axum::response::Response;
use log::{error, warn};
use std::fmt::Display;
#[allow(clippy::result_large_err)]
pub async fn check_rate_limit<T, K>(service: &T, api_key: K, endpoint: &str) -> Result<(), Response>
where
T: RateLimitingService + ?Sized,
K: Display,
{
let api_key_str = api_key.to_string();
match service.check_rate_limit(&api_key_str, endpoint).await {
Ok(RateLimitResult::Denied { reason }) => Err(response_builder::errors::too_many_requests(
format!("Rate limit exceeded: {}", reason),
)),
Ok(RateLimitResult::RetryAfter {
retry_after_seconds,
}) => Err(response_builder::errors::too_many_requests_with_retry(
format!(
"Rate limit exceeded, please retry after {} seconds",
retry_after_seconds
),
retry_after_seconds,
)),
Ok(RateLimitResult::Allowed) => Ok(()),
Err(e) => {
if *RATE_LIMIT_FAIL_OPEN {
warn!(
"Rate limiting service error - failing open (allowing request). \
error={} endpoint={}",
e, endpoint
);
Ok(())
} else {
error!(
"Rate limiting service error - failing closed (rejecting request). \
error={} endpoint={}",
e, endpoint
);
Err(response_builder::errors::service_unavailable(
"Rate limiting service is temporarily unavailable. Please try again later.",
))
}
}
}
}
pub async fn check_rate_limit_as_app_error<T, K>(
service: &T,
api_key: K,
endpoint: &str,
) -> Result<(), CrawlRsError>
where
T: RateLimitingService + ?Sized,
K: Display,
{
let api_key_str = api_key.to_string();
match service.check_rate_limit(&api_key_str, endpoint).await {
Ok(RateLimitResult::Denied { reason }) => Err(CrawlRsError::RateLimit(format!(
"Rate limit exceeded: {}",
reason
))),
Ok(RateLimitResult::RetryAfter {
retry_after_seconds,
}) => Err(CrawlRsError::RateLimit(format!(
"Rate limit exceeded, please retry after {} seconds",
retry_after_seconds
))),
Ok(RateLimitResult::Allowed) => Ok(()),
Err(e) => {
if *RATE_LIMIT_FAIL_OPEN {
warn!(
"Rate limiting service error - failing open (allowing request). \
error={} endpoint={}",
e, endpoint
);
Ok(())
} else {
error!(
"Rate limiting service error - failing closed (rejecting request). \
error={} endpoint={}",
e, endpoint
);
Err(CrawlRsError::ServiceUnavailable(
"Rate limiting service is temporarily unavailable. Please try again later."
.to_string(),
))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::services::rate_limiting_service::{
BacklogService, ConcurrencyConfig, ConcurrencyControlService, ConcurrencyResult,
QuotaService, RateLimitConfig, RateLimitResult, RateLimitService, RateLimitingError,
};
use async_trait::async_trait;
use axum::http::StatusCode;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[derive(Clone, Copy)]
enum MockError {
Database,
Credits,
Configuration,
Other,
}
struct MockRateLimitingService {
call_count: Arc<AtomicUsize>,
result: RateLimitResult,
error: Option<MockError>,
}
impl MockRateLimitingService {
fn new(result: RateLimitResult) -> Self {
Self {
call_count: Arc::new(AtomicUsize::new(0)),
result,
error: None,
}
}
fn with_error() -> Self {
Self {
call_count: Arc::new(AtomicUsize::new(0)),
result: RateLimitResult::Allowed,
error: Some(MockError::Database),
}
}
fn with_error_kind(kind: MockError) -> Self {
Self {
call_count: Arc::new(AtomicUsize::new(0)),
result: RateLimitResult::Allowed,
error: Some(kind),
}
}
fn make_error(kind: MockError) -> RateLimitingError {
match kind {
MockError::Database => RateLimitingError::DatabaseError,
MockError::Credits => RateLimitingError::CreditsError,
MockError::Configuration => {
RateLimitingError::ConfigurationError("mock config error".to_string())
}
MockError::Other => RateLimitingError::Other(anyhow::anyhow!("mock other error")),
}
}
}
#[async_trait]
impl RateLimitService for MockRateLimitingService {
async fn check_rate_limit(
&self,
_api_key: &str,
_endpoint: &str,
) -> Result<RateLimitResult, RateLimitingError> {
self.call_count.fetch_add(1, Ordering::SeqCst);
if let Some(kind) = self.error {
return Err(Self::make_error(kind));
}
Ok(self.result.clone())
}
async fn get_team_rate_limit_config(
&self,
_team_id: crate::domain::id::Id,
) -> Result<RateLimitConfig, RateLimitingError> {
Ok(RateLimitConfig::default())
}
async fn update_team_rate_limit_config(
&self,
_team_id: crate::domain::id::Id,
_config: RateLimitConfig,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn cleanup_expired_rate_limits(&self) -> Result<u64, RateLimitingError> {
Ok(0)
}
}
#[async_trait]
impl ConcurrencyControlService for MockRateLimitingService {
async fn check_team_concurrency(
&self,
_team_id: crate::domain::id::Id,
_task_id: crate::domain::id::Id,
) -> Result<ConcurrencyResult, RateLimitingError> {
Ok(ConcurrencyResult::Allowed)
}
async fn release_team_concurrency_slot(
&self,
_team_id: crate::domain::id::Id,
_task_id: crate::domain::id::Id,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn get_team_current_concurrency(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u32, RateLimitingError> {
Ok(0)
}
async fn get_team_concurrency_config(
&self,
_team_id: crate::domain::id::Id,
) -> Result<ConcurrencyConfig, RateLimitingError> {
Ok(ConcurrencyConfig::default())
}
async fn update_team_concurrency_config(
&self,
_team_id: crate::domain::id::Id,
_config: ConcurrencyConfig,
) -> Result<(), RateLimitingError> {
Ok(())
}
}
#[async_trait]
impl BacklogService for MockRateLimitingService {
async fn process_backlog_tasks(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u32, RateLimitingError> {
Ok(0)
}
}
#[async_trait]
impl QuotaService for MockRateLimitingService {
async fn check_and_deduct_quota(
&self,
_team_id: crate::domain::id::Id,
_amount: i64,
_transaction_type: crate::domain::models::CreditsTransactionType,
_description: String,
_reference_id: Option<crate::domain::id::Id>,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn get_quota_balance(
&self,
_team_id: crate::domain::id::Id,
) -> Result<i64, RateLimitingError> {
Ok(1000)
}
async fn refund_quota(
&self,
_team_id: crate::domain::id::Id,
_amount: i64,
_description: String,
_reference_id: Option<crate::domain::id::Id>,
) -> Result<(), RateLimitingError> {
Ok(())
}
}
#[async_trait]
impl RateLimitingService for MockRateLimitingService {}
#[tokio::test]
async fn test_allowed_passed() {
let call_count = Arc::new(AtomicUsize::new(0));
let service = MockRateLimitingService {
call_count: call_count.clone(),
result: RateLimitResult::Allowed,
error: None,
};
let result = check_rate_limit(&service, "test-key", "/v1/test").await;
assert!(result.is_ok());
assert_eq!(call_count.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn test_denied_returns_429() {
let service = MockRateLimitingService {
call_count: Arc::new(AtomicUsize::new(0)),
result: RateLimitResult::Denied {
reason: "Too many requests".to_string(),
},
error: None,
};
let result = check_rate_limit(&service, "test-key", "/v1/test").await;
assert!(result.is_err());
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
}
#[tokio::test]
async fn test_retry_after_returns_429_with_seconds() {
let service = MockRateLimitingService {
call_count: Arc::new(AtomicUsize::new(0)),
result: RateLimitResult::RetryAfter {
retry_after_seconds: 30,
},
error: None,
};
let result = check_rate_limit(&service, "test-key", "/v1/test").await;
assert!(result.is_err());
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
}
#[tokio::test]
async fn test_check_rate_limit_error_fails_closed() {
let service = MockRateLimitingService::with_error();
let result = check_rate_limit(&service, "test-key", "/v1/test").await;
assert!(
result.is_err(),
"fail-closed should return Err on service error"
);
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(
service.call_count.load(Ordering::SeqCst),
1,
"service should be called once"
);
}
#[tokio::test]
async fn test_app_error_allowed() {
let service = MockRateLimitingService::new(RateLimitResult::Allowed);
let result = check_rate_limit_as_app_error(&service, "test-key", "/v1/test").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_app_error_denied() {
let service = MockRateLimitingService::new(RateLimitResult::Denied {
reason: "Too many requests".to_string(),
});
let result = check_rate_limit_as_app_error(&service, "test-key", "/v1/test").await;
let err = result.expect_err("Denied should map to CrawlRsError::RateLimit");
assert_eq!(err.status_code(), StatusCode::TOO_MANY_REQUESTS);
assert_eq!(err.error_code(), "RATE_LIMITED");
match err {
CrawlRsError::RateLimit(msg) => {
assert!(msg.contains("Rate limit exceeded"));
assert!(msg.contains("Too many requests"));
}
other => panic!("expected CrawlRsError::RateLimit, got {:?}", other),
}
}
#[tokio::test]
async fn test_app_error_retry_after() {
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: 60,
});
let result = check_rate_limit_as_app_error(&service, "test-key", "/v1/test").await;
let err = result.expect_err("RetryAfter should map to CrawlRsError::RateLimit");
assert_eq!(err.status_code(), StatusCode::TOO_MANY_REQUESTS);
match err {
CrawlRsError::RateLimit(msg) => {
assert!(msg.contains("retry after"));
assert!(msg.contains("60 seconds"));
}
other => panic!("expected CrawlRsError::RateLimit, got {:?}", other),
}
}
#[tokio::test]
async fn test_app_error_service_error_fails_closed() {
let service = MockRateLimitingService::with_error();
let result = check_rate_limit_as_app_error(&service, "test-key", "/v1/test").await;
let err = result.expect_err("fail-closed should return Err on service error");
assert_eq!(err.status_code(), StatusCode::SERVICE_UNAVAILABLE);
match err {
CrawlRsError::ServiceUnavailable(msg) => {
assert!(msg.contains("temporarily unavailable"));
}
other => panic!("expected CrawlRsError::ServiceUnavailable, got {:?}", other),
}
}
#[tokio::test]
async fn test_check_rate_limit_response_body_denied() {
let service = MockRateLimitingService::new(RateLimitResult::Denied {
reason: "quota exhausted".to_string(),
});
let result = check_rate_limit(&service, "k", "/v1/test").await;
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let body_str = String::from_utf8_lossy(&body);
assert!(
body_str.contains("false"),
"success=false expected: {}",
body_str
);
assert!(
body_str.contains("quota exhausted"),
"reason in body expected: {}",
body_str
);
let json: serde_json::Value =
serde_json::from_slice(&body).expect("body should be valid JSON");
assert_eq!(
json["error"]["code"], "RATE_LIMITED",
"standard envelope error.code expected: {}",
body_str
);
assert!(json.get("data").is_none() || json["data"].is_null());
}
#[tokio::test]
async fn test_check_rate_limit_response_body_retry_after() {
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: 42,
});
let result = check_rate_limit(&service, "k", "/v1/test").await;
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let body_str = String::from_utf8_lossy(&body);
assert!(
body_str.contains("42"),
"retry seconds in body expected: {}",
body_str
);
assert!(
body_str.contains("retry_after_seconds"),
"retry_after_seconds key expected: {}",
body_str
);
let json: serde_json::Value =
serde_json::from_slice(&body).expect("body should be valid JSON");
assert_eq!(
json["error"]["code"], "RATE_LIMITED",
"standard envelope error.code expected: {}",
body_str
);
}
#[tokio::test]
async fn test_check_rate_limit_denied_body_contains_exact_reason() {
let reason = "quota exhausted for team abc-123";
let service = MockRateLimitingService::new(RateLimitResult::Denied {
reason: reason.to_string(),
});
let result = check_rate_limit(&service, "key", "/v1/scrape").await;
let response = result.unwrap_err();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let body_str = String::from_utf8_lossy(&body);
assert!(
body_str.contains(reason),
"exact reason expected in body: {}",
body_str
);
assert!(
body_str.contains("Rate limit exceeded"),
"prefix expected: {}",
body_str
);
}
#[tokio::test]
async fn test_app_error_denied_contains_exact_reason() {
let reason = "per-minute limit hit";
let service = MockRateLimitingService::new(RateLimitResult::Denied {
reason: reason.to_string(),
});
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
match result.expect_err("should be RateLimit") {
CrawlRsError::RateLimit(msg) => {
assert!(
msg.contains(reason),
"msg should contain exact reason: {}",
msg
);
}
other => panic!("expected CrawlRsError::RateLimit, got {:?}", other),
}
}
#[tokio::test]
async fn test_app_error_retry_after_contains_exact_seconds() {
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: 120,
});
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
match result.expect_err("should be RateLimit") {
CrawlRsError::RateLimit(msg) => {
assert!(
msg.contains("120 seconds"),
"msg should contain seconds: {}",
msg
);
}
other => panic!("expected CrawlRsError::RateLimit, got {:?}", other),
}
}
#[tokio::test]
async fn test_check_rate_limit_with_empty_inputs() {
let service = MockRateLimitingService::new(RateLimitResult::Allowed);
let result = check_rate_limit(&service, "", "").await;
assert!(result.is_ok(), "empty inputs with Allowed should return Ok");
}
#[tokio::test]
async fn test_app_error_with_empty_inputs() {
let service = MockRateLimitingService::new(RateLimitResult::Allowed);
let result = check_rate_limit_as_app_error(&service, "", "").await;
assert!(result.is_ok(), "empty inputs with Allowed should return Ok");
}
#[tokio::test]
async fn test_check_rate_limit_fails_closed_on_credits_error() {
let service = MockRateLimitingService::with_error_kind(MockError::Credits);
let result = check_rate_limit(&service, "k", "/v1/test").await;
assert!(result.is_err(), "CreditsError should fail-closed");
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(
service.call_count.load(Ordering::SeqCst),
1,
"service should be called once"
);
}
#[tokio::test]
async fn test_check_rate_limit_fails_closed_on_configuration_error() {
let service = MockRateLimitingService::with_error_kind(MockError::Configuration);
let result = check_rate_limit(&service, "k", "/v1/test").await;
assert!(result.is_err(), "ConfigurationError should fail-closed");
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
}
#[tokio::test]
async fn test_check_rate_limit_fails_closed_on_other_error() {
let service = MockRateLimitingService::with_error_kind(MockError::Other);
let result = check_rate_limit(&service, "k", "/v1/test").await;
assert!(result.is_err(), "Other error should fail-closed");
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
}
#[tokio::test]
async fn test_app_error_fails_closed_on_credits_error() {
let service = MockRateLimitingService::with_error_kind(MockError::Credits);
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
let err = result.expect_err("CreditsError should fail-closed in app_error path");
assert_eq!(err.status_code(), StatusCode::SERVICE_UNAVAILABLE);
}
#[tokio::test]
async fn test_app_error_fails_closed_on_other_error() {
let service = MockRateLimitingService::with_error_kind(MockError::Other);
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
let err = result.expect_err("Other error should fail-closed in app_error path");
assert_eq!(err.status_code(), StatusCode::SERVICE_UNAVAILABLE);
}
#[tokio::test]
async fn test_denied_with_empty_reason() {
let service = MockRateLimitingService::new(RateLimitResult::Denied {
reason: String::new(),
});
let result = check_rate_limit(&service, "k", "/v1/test").await;
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let body_str = String::from_utf8_lossy(&body);
assert!(
body_str.contains("Rate limit exceeded"),
"prefix expected even with empty reason: {}",
body_str
);
assert!(
body_str.contains("\"success\":false"),
"success=false expected: {}",
body_str
);
}
#[tokio::test]
async fn test_retry_after_with_zero_seconds() {
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: 0,
});
let result = check_rate_limit(&service, "k", "/v1/test").await;
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let body_str = String::from_utf8_lossy(&body);
assert!(
body_str.contains("\"retry_after_seconds\":0"),
"zero seconds expected in body: {}",
body_str
);
}
#[tokio::test]
async fn test_retry_after_with_max_seconds() {
let max_secs = u64::MAX;
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: max_secs,
});
let result = check_rate_limit(&service, "k", "/v1/test").await;
let response = result.unwrap_err();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let body_str = String::from_utf8_lossy(&body);
assert!(
body_str.contains(&max_secs.to_string()),
"max seconds expected in body: {}",
body_str
);
}
#[tokio::test]
async fn test_app_error_denied_with_empty_reason() {
let service = MockRateLimitingService::new(RateLimitResult::Denied {
reason: String::new(),
});
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
match result.expect_err("should be RateLimit") {
CrawlRsError::RateLimit(msg) => {
assert!(
msg.contains("Rate limit exceeded"),
"prefix expected: {}",
msg
);
}
other => panic!("expected CrawlRsError::RateLimit, got {:?}", other),
}
}
#[tokio::test]
async fn test_app_error_retry_after_with_zero_seconds() {
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: 0,
});
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
match result.expect_err("should be RateLimit") {
CrawlRsError::RateLimit(msg) => {
assert!(
msg.contains("0 seconds"),
"zero seconds expected in msg: {}",
msg
);
}
other => panic!("expected CrawlRsError::RateLimit, got {:?}", other),
}
}
#[tokio::test]
async fn test_multiple_calls_increment_counter() {
let service = MockRateLimitingService::new(RateLimitResult::Allowed);
for _ in 0..5 {
let result = check_rate_limit(&service, "k", "/v1/test").await;
assert!(result.is_ok(), "Allowed should always return Ok");
}
assert_eq!(
service.call_count.load(Ordering::SeqCst),
5,
"service should be called 5 times"
);
}
#[tokio::test]
async fn test_multiple_calls_app_error_increment_counter() {
let service = MockRateLimitingService::new(RateLimitResult::Allowed);
for _ in 0..3 {
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
assert!(result.is_ok(), "Allowed should always return Ok");
}
assert_eq!(
service.call_count.load(Ordering::SeqCst),
3,
"service should be called 3 times"
);
}
#[tokio::test]
async fn test_denied_response_json_has_success_false_and_error_key() {
let service = MockRateLimitingService::new(RateLimitResult::Denied {
reason: "limit hit".to_string(),
});
let result = check_rate_limit(&service, "k", "/v1/test").await;
let response = result.unwrap_err();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let json: serde_json::Value =
serde_json::from_slice(&body).expect("body should be valid JSON");
assert_eq!(json["success"], serde_json::Value::Bool(false));
assert_eq!(json["error"]["code"], "RATE_LIMITED");
assert!(
json["error"]["message"]
.as_str()
.unwrap()
.contains("limit hit"),
"message should contain reason: {}",
json
);
assert!(json.get("data").is_none() || json["data"].is_null());
}
#[tokio::test]
async fn test_retry_after_response_json_has_all_three_keys() {
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: 99,
});
let result = check_rate_limit(&service, "k", "/v1/test").await;
let response = result.unwrap_err();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.expect("body should be readable");
let json: serde_json::Value =
serde_json::from_slice(&body).expect("body should be valid JSON");
assert_eq!(json["success"], serde_json::Value::Bool(false));
assert_eq!(json["error"]["code"], "RATE_LIMITED");
assert_eq!(json["retry_after_seconds"], serde_json::json!(99));
}
#[tokio::test]
async fn test_app_error_status_code_and_error_code_for_retry_after() {
let service = MockRateLimitingService::new(RateLimitResult::RetryAfter {
retry_after_seconds: 7,
});
let result = check_rate_limit_as_app_error(&service, "k", "/v1/test").await;
let err = result.expect_err("RetryAfter should map to CrawlRsError::RateLimit");
assert_eq!(err.status_code(), StatusCode::TOO_MANY_REQUESTS);
assert_eq!(err.error_code(), "RATE_LIMITED");
}
}