use crate::dao::GarrisonDao;
use crate::error::GarrisonError;
use async_trait::async_trait;
use chrono::Utc;
use limiteron::error::{ConsumeResult, StorageError};
use limiteron::storage::{QuotaInfo, QuotaStorage};
use std::sync::Arc;
use std::time::Duration;
use super::errors::map_to_storage_err;
const QUOTA_KEY_PREFIX: &str = "limiteron:quota";
fn quota_count_key(user_id: &str, resource: &str) -> String {
format!("{}:{}:{}:count", QUOTA_KEY_PREFIX, user_id, resource)
}
fn quota_meta_key(user_id: &str, resource: &str) -> String {
format!("{}:{}:{}:meta", QUOTA_KEY_PREFIX, user_id, resource)
}
pub struct GarrisonDaoQuotaStorage {
pub(super) dao: Arc<dyn GarrisonDao>,
}
impl GarrisonDaoQuotaStorage {
pub fn new(dao: Arc<dyn GarrisonDao>) -> Self {
Self { dao }
}
}
#[async_trait]
impl QuotaStorage for GarrisonDaoQuotaStorage {
async fn get_quota(
&self,
user_id: &str,
resource: &str,
) -> Result<Option<QuotaInfo>, StorageError> {
let meta_key = quota_meta_key(user_id, resource);
let count_key = quota_count_key(user_id, resource);
let meta = self.dao.get(&meta_key).await.map_err(map_to_storage_err)?;
let count = self.dao.get(&count_key).await.map_err(map_to_storage_err)?;
match (meta, count) {
(Some(meta_str), Some(count_str)) => {
let consumed: u64 = count_str.parse().map_err(|e| {
map_to_storage_err(GarrisonError::Dao(format!(
"limiteron-quota-count-parse-failed::{}::{}::{}",
count_key, count_str, e
)))
})?;
let parts: Vec<&str> = meta_str.split('|').collect();
if parts.len() != 4 {
return Err(map_to_storage_err(GarrisonError::Dao(format!(
"limiteron-quota-meta-format-error::{}::{}::{}",
meta_key,
meta_str,
parts.len()
))));
}
let limit: u64 = parts[1].parse().map_err(|e| {
map_to_storage_err(GarrisonError::Dao(format!(
"limiteron-quota-limit-parse-failed::{}::{}::{}",
meta_key, parts[1], e
)))
})?;
let window_start_ts: i64 = parts[2].parse().map_err(|e| {
map_to_storage_err(GarrisonError::Dao(format!(
"limiteron-quota-window-start-parse-failed::{}::{}::{}",
meta_key, parts[2], e
)))
})?;
let window_end_ts: i64 = parts[3].parse().map_err(|e| {
map_to_storage_err(GarrisonError::Dao(format!(
"limiteron-quota-window-end-parse-failed::{}::{}::{}",
meta_key, parts[3], e
)))
})?;
let window_start = chrono::DateTime::from_timestamp(window_start_ts, 0)
.ok_or_else(|| {
map_to_storage_err(GarrisonError::Dao(format!(
"limiteron-quota-window-start-datetime-failed::{}",
window_start_ts
)))
})?;
let window_end =
chrono::DateTime::from_timestamp(window_end_ts, 0).ok_or_else(|| {
map_to_storage_err(GarrisonError::Dao(format!(
"limiteron-quota-window-end-datetime-failed::{}",
window_end_ts
)))
})?;
Ok(Some(QuotaInfo {
consumed,
limit,
window_start,
window_end,
}))
},
_ => Ok(None),
}
}
async fn consume(
&self,
user_id: &str,
resource: &str,
cost: u64,
limit: u64,
window: Duration,
) -> Result<ConsumeResult, StorageError> {
let count_key = quota_count_key(user_id, resource);
let meta_key = quota_meta_key(user_id, resource);
let ttl = window.as_secs();
let mut new_count = 0u64;
for _ in 0..cost {
new_count = self
.dao
.incr(&count_key, ttl)
.await
.map_err(map_to_storage_err)?;
}
let now = Utc::now();
let window_end = now
+ chrono::Duration::from_std(window)
.unwrap_or_else(|_| chrono::Duration::seconds(window.as_secs() as i64));
let meta_val = format!(
"{}|{}|{}|{}",
new_count,
limit,
now.timestamp(),
window_end.timestamp()
);
self.dao
.set(&meta_key, &meta_val, ttl)
.await
.map_err(map_to_storage_err)?;
let allowed = new_count <= limit;
let remaining = limit.saturating_sub(new_count);
let usage_percent = if limit == 0 {
100.0
} else {
(new_count as f64 / limit as f64) * 100.0
};
Ok(ConsumeResult {
allowed,
remaining,
alert_triggered: usage_percent >= 80.0,
usage_percent,
})
}
async fn reset(
&self,
user_id: &str,
resource: &str,
_limit: u64,
_window: Duration,
) -> Result<(), StorageError> {
let count_key = quota_count_key(user_id, resource);
let meta_key = quota_meta_key(user_id, resource);
self.dao
.delete(&count_key)
.await
.map_err(map_to_storage_err)?;
self.dao.delete(&meta_key).await.map_err(map_to_storage_err)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dao::tests::MockDao;
fn make_dao() -> Arc<dyn GarrisonDao> {
Arc::new(MockDao::new())
}
#[tokio::test]
async fn quota_consume_within_limit() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
let result = quota
.consume("user1", "sms", 1, 5, Duration::from_secs(3600))
.await
.unwrap();
assert!(result.allowed);
assert_eq!(result.remaining, 4);
}
#[tokio::test]
async fn quota_consume_exceeds_limit() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
for _ in 0..5 {
let r = quota
.consume("user2", "sms", 1, 5, Duration::from_secs(3600))
.await
.unwrap();
assert!(r.allowed);
}
let result = quota
.consume("user2", "sms", 1, 5, Duration::from_secs(3600))
.await
.unwrap();
assert!(!result.allowed);
assert_eq!(result.remaining, 0);
}
#[tokio::test]
async fn quota_reset_clears_counters() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
quota
.consume("user3", "sms", 3, 10, Duration::from_secs(60))
.await
.unwrap();
assert!(quota.get_quota("user3", "sms").await.unwrap().is_some());
quota
.reset("user3", "sms", 10, Duration::from_secs(60))
.await
.unwrap();
assert!(quota.get_quota("user3", "sms").await.unwrap().is_none());
}
#[tokio::test]
async fn m3_quota_get_quota_dirty_count_returns_err() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
quota
.dao
.set("limiteron:quota:user1:res:count", "not-a-number", 0)
.await
.unwrap();
quota
.dao
.set("limiteron:quota:user1:res:meta", "1|5|1000|2000", 0)
.await
.unwrap();
let result = quota.get_quota("user1", "res").await;
assert!(
result.is_err(),
"脏 count 数据应返回错误,实际: {:?}",
result
);
let err_msg = format!("{}", result.unwrap_err());
assert!(
err_msg.contains("limiteron-quota-count-parse-failed"),
"错误消息应包含 'limiteron-quota-count-parse-failed',实际: {}",
err_msg
);
}
#[tokio::test]
async fn m3_quota_get_quota_dirty_meta_returns_err() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
quota
.dao
.set("limiteron:quota:user2:res:count", "1", 0)
.await
.unwrap();
quota
.dao
.set(
"limiteron:quota:user2:res:meta",
"1|not-number|1000|2000",
0,
)
.await
.unwrap();
let result = quota.get_quota("user2", "res").await;
assert!(
result.is_err(),
"脏 meta 数据应返回错误,实际: {:?}",
result
);
let err_msg = format!("{}", result.unwrap_err());
assert!(
err_msg.contains("limiteron-quota-limit-parse-failed"),
"错误消息应包含 'limiteron-quota-limit-parse-failed',实际: {}",
err_msg
);
}
#[test]
fn quota_key_format_correct() {
assert_eq!(
quota_count_key("user1", "sms"),
"limiteron:quota:user1:sms:count"
);
assert_eq!(
quota_meta_key("user1", "sms"),
"limiteron:quota:user1:sms:meta"
);
assert_eq!(quota_count_key("", ""), "limiteron:quota:::count");
}
#[tokio::test]
async fn quota_get_quota_after_consume_returns_valid_info() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
quota
.consume("user_rt", "res", 1, 10, Duration::from_secs(3600))
.await
.unwrap();
let info = quota.get_quota("user_rt", "res").await.unwrap();
assert!(info.is_some(), "consume 后 get_quota 应返回 Some");
let info = info.unwrap();
assert_eq!(info.consumed, 1);
assert_eq!(info.limit, 10);
}
#[tokio::test]
async fn quota_consume_cost_greater_than_one() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
let result = quota
.consume("user_cost", "res", 5, 10, Duration::from_secs(60))
.await
.unwrap();
assert!(result.allowed, "5 <= 10 应允许");
assert_eq!(result.remaining, 5, "remaining = 10 - 5 = 5");
assert!(
result.usage_percent >= 50.0,
"usage_percent 应 >= 50%,实际: {}",
result.usage_percent
);
}
#[tokio::test]
async fn quota_consume_limit_zero_usage_100_percent() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
let result = quota
.consume("user_zero", "res", 1, 0, Duration::from_secs(60))
.await
.unwrap();
assert!(!result.allowed, "count 1 > limit 0 应拒绝");
assert_eq!(result.remaining, 0);
assert_eq!(
result.usage_percent, 100.0,
"limit=0 时 usage_percent 应为 100%"
);
}
#[tokio::test]
async fn quota_consume_alert_triggered_at_80_percent() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
let result = quota
.consume("user_alert", "res", 8, 10, Duration::from_secs(60))
.await
.unwrap();
assert!(
result.alert_triggered,
"usage 80% 应触发 alert,实际 usage_percent: {}",
result.usage_percent
);
assert!(result.usage_percent >= 80.0);
}
#[tokio::test]
async fn quota_consume_no_alert_below_80_percent() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
let result = quota
.consume("user_noalert", "res", 7, 10, Duration::from_secs(60))
.await
.unwrap();
assert!(
!result.alert_triggered,
"usage 70% 不应触发 alert,实际 usage_percent: {}",
result.usage_percent
);
}
#[tokio::test]
async fn quota_get_quota_meta_wrong_parts_returns_err() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
quota
.dao
.set("limiteron:quota:user_w:res:count", "1", 0)
.await
.unwrap();
quota
.dao
.set("limiteron:quota:user_w:res:meta", "1|5", 0)
.await
.unwrap();
let result = quota.get_quota("user_w", "res").await;
assert!(result.is_err(), "meta 段数不对应返回错误");
let err_msg = format!("{}", result.unwrap_err());
assert!(
err_msg.contains("limiteron-quota-meta-format-error"),
"错误消息应包含 'limiteron-quota-meta-format-error',实际: {}",
err_msg
);
}
#[tokio::test]
async fn quota_get_quota_invalid_window_start_ts_returns_err() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
quota
.dao
.set("limiteron:quota:user_ts:res:count", "1", 0)
.await
.unwrap();
quota
.dao
.set("limiteron:quota:user_ts:res:meta", "1|5|not_number|2000", 0)
.await
.unwrap();
let result = quota.get_quota("user_ts", "res").await;
assert!(result.is_err(), "无效 window_start_ts 应返回错误");
}
#[tokio::test]
async fn quota_get_quota_count_without_meta_returns_none() {
let quota = GarrisonDaoQuotaStorage::new(make_dao());
quota
.dao
.set("limiteron:quota:user_nm:res:count", "5", 0)
.await
.unwrap();
let result = quota.get_quota("user_nm", "res").await.unwrap();
assert!(result.is_none(), "count 有但 meta 缺失时应返回 None");
}
}