1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: MIT
//! Centralized configuration constants for Limiteron.
//!
//! This module provides well-documented constants used throughout the library.
//! All magic numbers are defined here with their purpose and usage context.
/// Maximum cost value for rate limiting operations.
///
/// This limit prevents excessive resource consumption from a single request.
/// Must be greater than 0 and less than or equal to 1,000,000.
///
/// # Usage
///
/// Used in [`validate_cost()`] to ensure cost parameters are within acceptable bounds.
///
/// [`validate_cost()`]: crate::limiters::validate_cost
pub const MAX_COST: u64 = 1_000_000;
// ============================================================================
// Rate Limiter Maximums
// ============================================================================
/// Maximum token bucket capacity (10M tokens).
///
/// Prevents excessive memory consumption (≈10MB per limiter instance at
/// 1 byte/token assumption); shared by config validation, the factory and
/// direct construction.
pub const MAX_TOKEN_BUCKET_CAPACITY: u64 = 10_000_000;
/// Maximum token refill rate per second (1M/s).
///
/// Prevents CPU over-consumption from high-frequency refill operations;
/// also reused as the leak-rate ceiling for leaky bucket limiters.
pub const MAX_TOKEN_BUCKET_REFILL_RATE: u64 = 1_000_000;
/// Maximum window request budget (10M).
///
/// Prevents oversized window bookkeeping; counter-based window limiters
/// (sharded sliding window / fixed window) scale O(1) with this value.
pub const MAX_WINDOW_REQUESTS: u64 = 10_000_000;
/// Maximum sliding window log entries (100K).
///
/// Each logged entry costs 16 bytes (`(u64, u64)`), so the ceiling bounds a
/// single instance at ≈1.6MB — six orders of magnitude tighter than the
/// counter-based window budget. Exact counting trades memory for precision;
/// larger budgets should use the sharded sliding window.
pub const MAX_SLIDING_LOG_REQUESTS: u64 = 100_000;
/// Maximum concurrency limit (100K).
///
/// Prevents oversized concurrency control structures.
pub const MAX_CONCURRENT_REQUESTS: u64 = 100_000;
// ============================================================================
// Circuit Breaker Constants
// ============================================================================
/// Default failure threshold for circuit breaker.
///
/// The circuit breaker transitions to open state after this many consecutive failures.
pub const DEFAULT_CIRCUIT_BREAKER_FAILURE_THRESHOLD: u64 = 5;
/// Default success threshold for circuit breaker half-open state.
///
/// The circuit breaker transitions to closed state after this many successes in half-open state.
pub const DEFAULT_CIRCUIT_BREAKER_SUCCESS_THRESHOLD: u64 = 3;
/// Default timeout duration for circuit breaker (30 seconds).
///
/// How long the circuit breaker remains open before attempting to half-open.
pub const DEFAULT_CIRCUIT_BREAKER_TIMEOUT_SECS: u64 = 30;
/// Maximum number of calls in half-open state.
///
/// Limits the number of trial requests when probing if the service has recovered.
pub const DEFAULT_CIRCUIT_BREAKER_HALF_OPEN_MAX_CALLS: u64 = 3;
/// Default slow call duration threshold for circuit breaker (500 milliseconds).
///
/// Calls exceeding this duration are considered slow calls.
pub const DEFAULT_CIRCUIT_BREAKER_SLOW_CALL_DURATION_MILLIS: u64 = 500;
/// Default slow call rate threshold for circuit breaker (50%).
///
/// The circuit breaker transitions to open state when the slow call rate exceeds this value.
pub const DEFAULT_CIRCUIT_BREAKER_SLOW_CALL_RATE_THRESHOLD: f64 = 0.5;
/// Default failure ratio threshold for manual circuit breaker (50%).
///
/// Ratio-triggered: the breaker opens when failures / total requests reaches this ratio.
pub const DEFAULT_MANUAL_CIRCUIT_BREAKER_FAILURE_THRESHOLD: f64 = 0.5;
/// Default minimum requests before evaluating the failure ratio (manual circuit breaker).
pub const DEFAULT_MANUAL_CIRCUIT_BREAKER_MIN_REQUESTS: u64 = 10;
/// Default cooldown for manual circuit breaker (30 seconds).
///
/// How long the breaker remains open before lazily transitioning to half-open.
pub const DEFAULT_MANUAL_CIRCUIT_BREAKER_HALF_OPEN_INTERVAL_SECS: u64 = 30;
/// Default consecutive successes required to close the manual circuit breaker from half-open.
pub const DEFAULT_MANUAL_CIRCUIT_BREAKER_HALF_OPEN_MAX_SUCCESSES: u64 = 3;
// ============================================================================
// Validation Constants
// ============================================================================
/// Maximum API key length (512 characters).
///
/// Standard length for API key validation.
pub const MAX_API_KEY_LENGTH: usize = 512;
/// Maximum header value length (8192 characters).
///
/// Standard length for HTTP header validation.
pub const MAX_HEADER_VALUE_LENGTH: usize = 8192;
/// Maximum path length (2048 characters).
///
/// Standard length for URL path validation.
pub const MAX_PATH_LENGTH: usize = 2048;
/// Maximum ban reason length (500 characters).
///
/// Prevents overly long ban reasons that could cause display issues.
/// 门控覆盖两个使用者:ban/types.rs(ban-manager)与 validation.rs(validation)。
/// 不能只挂 validation——ban-manager 单开时 ban/types.rs 的 import 会断编译。
pub const MAX_BAN_REASON_LENGTH: usize = 500;
/// Maximum user ID length (256 characters).
///
/// Standard length for user identifier validation.
pub const MAX_USER_ID_LENGTH: usize = 256;
/// Maximum MAC address length (17 characters).
///
/// Standard MAC address format: XX:XX:XX:XX:XX:XX
pub const MAX_MAC_ADDRESS_LENGTH: usize = 17;
/// Maximum IP address length (45 characters for IPv6 with port).
///
/// Covers both IPv4 and IPv6 address formats.
pub const MAX_IP_ADDRESS_LENGTH: usize = 45;