mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-19 18:48:46 +00:00
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:
@@ -18,7 +18,8 @@ chrono = { workspace = true }
|
|||||||
axum = { workspace = true }
|
axum = { workspace = true }
|
||||||
tower = { workspace = true }
|
tower = { workspace = true }
|
||||||
tower-http = { workspace = true }
|
tower-http = { workspace = true }
|
||||||
tower-governor = "0.4.2"
|
tower_governor = "0.8.0"
|
||||||
|
governor = "0.10.4"
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
serde_json = { workspace = true }
|
serde_json = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
@@ -27,11 +28,12 @@ anyhow = { workspace = true }
|
|||||||
dotenv = { workspace = true }
|
dotenv = { workspace = true }
|
||||||
uuid = { workspace = true }
|
uuid = { workspace = true }
|
||||||
jsonwebtoken = "10.3.0"
|
jsonwebtoken = "10.3.0"
|
||||||
|
thiserror = "2.0.18"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio-test = "0.4"
|
tokio-test = "0.4.5"
|
||||||
mockall = "0.13"
|
mockall = "0.14.0"
|
||||||
serial_test = "3.2"
|
serial_test = "3.4.0"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "charging-engine"
|
name = "charging-engine"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub enum CircuitState {
|
pub enum CircuitState {
|
||||||
Closed,
|
Closed,
|
||||||
Open,
|
Open,
|
||||||
@@ -10,13 +10,17 @@ pub enum CircuitState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[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")]
|
#[error("Circuit breaker is open")]
|
||||||
Open,
|
Open,
|
||||||
#[error("Wrapped error: {0}")]
|
#[error("Wrapped error: {0}")]
|
||||||
Inner(#[from] E),
|
Inner(#[from] E),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct CircuitBreaker {
|
pub struct CircuitBreaker {
|
||||||
state: Arc<RwLock<CircuitState>>,
|
state: Arc<RwLock<CircuitState>>,
|
||||||
failure_count: Arc<RwLock<u32>>,
|
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>>
|
pub async fn execute<F, T, E>(&self, operation: F) -> Result<T, CircuitBreakerError<E>>
|
||||||
where
|
where
|
||||||
F: std::future::Future<Output = Result<T, E>>,
|
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
|
// Check if circuit is open and should attempt to close
|
||||||
self.check_state().await;
|
self.check_state().await;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::Request,
|
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
@@ -9,14 +8,12 @@ use governor::{
|
|||||||
Jitter, Quota, RateLimiter,
|
Jitter, Quota, RateLimiter,
|
||||||
};
|
};
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::sync::Arc;
|
|
||||||
use tower_governor::{
|
use tower_governor::{
|
||||||
governor::config::GovernorConfigBuilder,
|
governor::config::GovernorConfigBuilder,
|
||||||
key_extractor::SmartIpKeyExtractor,
|
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")
|
let requests_per_second: u32 = std::env::var("RATE_LIMIT_RPS")
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|s| s.parse().ok())
|
.and_then(|s| s.parse().ok())
|
||||||
@@ -35,8 +32,8 @@ pub fn create_rate_limiter() -> GovernorLayer<SmartIpKeyExtractor> {
|
|||||||
.finish()
|
.finish()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
GovernorLayer {
|
tower_governor::GovernorLayer {
|
||||||
config,
|
config: Arc::new(config),
|
||||||
key_extractor: SmartIpKeyExtractor,
|
key_extractor: SmartIpKeyExtractor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user