mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-28 14:58:47 +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,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
use governor::{
|
|
||||||
clock::DefaultClock,
|
|
||||||
state::InMemoryState,
|
|
||||||
Quota,
|
|
||||||
};
|
|
||||||
use std::num::NonZeroU32;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tower_governor::{
|
use tower_governor::{
|
||||||
governor::GovernorConfig,
|
governor::GovernorConfig,
|
||||||
key_extractor::SmartIpKeyExtractor,
|
|
||||||
GovernorLayer,
|
GovernorLayer,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn create_rate_limiter() -> GovernorLayer<SmartIpKeyExtractor, InMemoryState, DefaultClock> {
|
pub fn create_rate_limiter() -> GovernorLayer {
|
||||||
let requests_per_second: u32 = std::env::var("RATE_LIMIT_RPS")
|
let governor_conf = GovernorConfig::default();
|
||||||
.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);
|
|
||||||
|
|
||||||
let quota = Quota::per_second(NonZeroU32::new(requests_per_second).unwrap())
|
GovernorLayer::new(governor_conf)
|
||||||
.allow_burst(NonZeroU32::new(burst_size).unwrap());
|
|
||||||
|
|
||||||
let config = GovernorConfig::builder()
|
|
||||||
.per_second(quota)
|
|
||||||
.burst_size(burst_size)
|
|
||||||
.finish()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
GovernorLayer::new(&config)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RateLimitResponse;
|
pub struct RateLimitResponse;
|
||||||
|
|||||||
Reference in New Issue
Block a user