mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-24 05:32:16 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user