refactor: Simplify rate limiter to use default configuration in charging-engine

- Remove unused imports: governor clock/state/Quota, std::num::NonZeroU32, std::sync::Arc, and SmartIpKeyExtractor
- Change create_rate_limiter return type from explicit generic parameters to simple GovernorLayer
- Replace custom rate limit configuration (RATE_LIMIT_RPS, RATE_LIMIT_BURST environment variables) with GovernorConfig::default()
- Remove quota builder logic and pass config directly to GovernorLayer::new()
This commit is contained in:
nutcas3
2026-04-26 02:31:59 +03:00
parent 186bba04a8
commit 2094a3b952
+3 -27
View File
@@ -2,39 +2,15 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use governor::{
clock::DefaultClock,
state::InMemoryState,
Quota,
};
use std::num::NonZeroU32;
use std::sync::Arc;
use tower_governor::{
governor::GovernorConfig,
key_extractor::SmartIpKeyExtractor,
GovernorLayer,
};
pub fn create_rate_limiter() -> GovernorLayer<SmartIpKeyExtractor, InMemoryState, DefaultClock> {
let requests_per_second: u32 = std::env::var("RATE_LIMIT_RPS")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(10);
let burst_size: u32 = std::env::var("RATE_LIMIT_BURST")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(20);
pub fn create_rate_limiter() -> GovernorLayer {
let governor_conf = GovernorConfig::default();
let quota = Quota::per_second(NonZeroU32::new(requests_per_second).unwrap())
.allow_burst(NonZeroU32::new(burst_size).unwrap());
let config = GovernorConfig::builder()
.per_second(quota)
.burst_size(burst_size)
.finish()
.unwrap();
GovernorLayer::new(&config)
GovernorLayer::new(governor_conf)
}
pub struct RateLimitResponse;