refactor: Update charging-engine dependencies and fix rate limiter configuration

- Upgrade tower_governor from 0.4.2 to 0.8.0 and add governor 0.10.4 dependency
- Add thiserror 2.0.18 for error handling
- Update dev dependencies: tokio-test to 0.4.5, mockall to 0.14.0, serial_test to 3.4.0
- Add Copy trait to CircuitState enum
- Add Clone derive to CircuitBreaker struct
- Add error trait bounds to CircuitBreakerError generic type
- Update create_rate_limiter return type to use explicit generic parameters
- Fix Go
This commit is contained in:
nutcas3
2026-04-26 00:42:19 +03:00
parent 133523d83b
commit b4b0966937
3 changed files with 16 additions and 12 deletions
+6 -4
View File
@@ -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"
+7 -2
View File
@@ -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<E> {
pub enum CircuitBreakerError<E>
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<RwLock<CircuitState>>,
failure_count: Arc<RwLock<u32>>,
@@ -39,6 +43,7 @@ impl CircuitBreaker {
pub async fn execute<F, T, E>(&self, operation: F) -> Result<T, CircuitBreakerError<E>>
where
F: std::future::Future<Output = Result<T, E>>,
E: std::error::Error + Send + Sync + 'static,
{
// Check if circuit is open and should attempt to close
self.check_state().await;
+3 -6
View File
@@ -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<SmartIpKeyExtractor> {
pub fn create_rate_limiter() -> tower_governor::GovernorLayer<SmartIpKeyExtractor, governor::state::InMemoryState, governor::clock::DefaultClock> {
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<SmartIpKeyExtractor> {
.finish()
.unwrap();
GovernorLayer {
config,
tower_governor::GovernorLayer {
config: Arc::new(config),
key_extractor: SmartIpKeyExtractor,
}
}