From 2094a3b952e55aaa0962285870f1255a0f7a7d95 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sun, 26 Apr 2026 02:31:59 +0300 Subject: [PATCH] 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() --- apps/charging-engine/src/rate_limit.rs | 30 +++----------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/apps/charging-engine/src/rate_limit.rs b/apps/charging-engine/src/rate_limit.rs index 2ad195d..7d985c5 100644 --- a/apps/charging-engine/src/rate_limit.rs +++ b/apps/charging-engine/src/rate_limit.rs @@ -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 { - 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;