diff --git a/apps/charging-engine/Cargo.toml b/apps/charging-engine/Cargo.toml index 2ca5bbd..2ebe1c0 100644 --- a/apps/charging-engine/Cargo.toml +++ b/apps/charging-engine/Cargo.toml @@ -18,7 +18,8 @@ chrono = { workspace = true } axum = { workspace = true } tower = { workspace = true } tower-http = { workspace = true } -tower-governor = "0.4.2" +tower_governor = "0.8.0" +governor = "0.10.4" serde = { workspace = true } serde_json = { workspace = true } tracing = { workspace = true } @@ -27,11 +28,12 @@ anyhow = { workspace = true } dotenv = { workspace = true } uuid = { workspace = true } jsonwebtoken = "10.3.0" +thiserror = "2.0.18" [dev-dependencies] -tokio-test = "0.4" -mockall = "0.13" -serial_test = "3.2" +tokio-test = "0.4.5" +mockall = "0.14.0" +serial_test = "3.4.0" [[bin]] name = "charging-engine" diff --git a/apps/charging-engine/src/circuit_breaker.rs b/apps/charging-engine/src/circuit_breaker.rs index bfdd268..500e48e 100644 --- a/apps/charging-engine/src/circuit_breaker.rs +++ b/apps/charging-engine/src/circuit_breaker.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use std::time::{Duration, Instant}; use tokio::sync::RwLock; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Copy)] pub enum CircuitState { Closed, Open, @@ -10,13 +10,17 @@ pub enum CircuitState { } #[derive(Debug, thiserror::Error)] -pub enum CircuitBreakerError { +pub enum CircuitBreakerError +where + E: std::error::Error + Send + Sync + 'static, +{ #[error("Circuit breaker is open")] Open, #[error("Wrapped error: {0}")] Inner(#[from] E), } +#[derive(Clone)] pub struct CircuitBreaker { state: Arc>, failure_count: Arc>, @@ -39,6 +43,7 @@ impl CircuitBreaker { pub async fn execute(&self, operation: F) -> Result> where F: std::future::Future>, + E: std::error::Error + Send + Sync + 'static, { // Check if circuit is open and should attempt to close self.check_state().await; diff --git a/apps/charging-engine/src/rate_limit.rs b/apps/charging-engine/src/rate_limit.rs index de9e6f1..ad1f9b6 100644 --- a/apps/charging-engine/src/rate_limit.rs +++ b/apps/charging-engine/src/rate_limit.rs @@ -1,5 +1,4 @@ use axum::{ - extract::Request, http::StatusCode, response::{IntoResponse, Response}, }; @@ -9,14 +8,12 @@ use governor::{ Jitter, Quota, RateLimiter, }; use std::num::NonZeroU32; -use std::sync::Arc; use tower_governor::{ governor::config::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor, - GovernorLayer, }; -pub fn create_rate_limiter() -> GovernorLayer { +pub fn create_rate_limiter() -> tower_governor::GovernorLayer { let requests_per_second: u32 = std::env::var("RATE_LIMIT_RPS") .ok() .and_then(|s| s.parse().ok()) @@ -35,8 +32,8 @@ pub fn create_rate_limiter() -> GovernorLayer { .finish() .unwrap(); - GovernorLayer { - config, + tower_governor::GovernorLayer { + config: Arc::new(config), key_extractor: SmartIpKeyExtractor, } }