Skip to main content

wae_authentication/rate_limit/
config.rs

1//! 速率限制配置
2
3/// 速率限制键类型
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5pub enum RateLimitKey {
6    /// 按 IP 地址
7    Ip(String),
8    /// 按用户 ID
9    UserId(String),
10    /// 按用户名/邮箱
11    Identifier(String),
12}
13
14/// 速率限制配置
15#[derive(Debug, Clone)]
16pub struct RateLimitConfig {
17    /// 最大请求数
18    pub max_requests: u32,
19    /// 时间窗口(秒)
20    pub window_seconds: u64,
21}
22
23impl Default for RateLimitConfig {
24    fn default() -> Self {
25        Self { max_requests: 5, window_seconds: 60 }
26    }
27}
28
29impl RateLimitConfig {
30    /// 创建新的速率限制配置
31    ///
32    /// # Arguments
33    /// * `max_requests` - 最大请求数
34    /// * `window_seconds` - 时间窗口(秒)
35    pub fn new(max_requests: u32, window_seconds: u64) -> Self {
36        Self { max_requests, window_seconds }
37    }
38
39    /// 设置最大请求数
40    ///
41    /// # Arguments
42    /// * `max` - 最大请求数
43    pub fn with_max_requests(mut self, max: u32) -> Self {
44        self.max_requests = max;
45        self
46    }
47
48    /// 设置时间窗口
49    ///
50    /// # Arguments
51    /// * `seconds` - 时间窗口(秒)
52    pub fn with_window_seconds(mut self, seconds: u64) -> Self {
53        self.window_seconds = seconds;
54        self
55    }
56}