refactor: Improve Redis key consistency and add existence check in credit management

- Change Redis key prefix from "block:" to "blocked:" in unblock_user for consistency
- Replace is_some() check with if let pattern in block_user to avoid unused variable
- Add existence check before attempting to delete blocked user key in unblock_user
- Use if let pattern for blocked status check in unblock_user
- Return Ok(()) silently if user is not blocked instead of attempting deletion
This commit is contained in:
nutcas3
2026-04-29 04:24:49 +03:00
parent 4008a19f4a
commit b4e42d7717
@@ -100,7 +100,7 @@ impl crate::charging::ChargingEngine {
let blocked: Option<String> = redis::AsyncCommands::get(&mut conn, &key).await let blocked: Option<String> = redis::AsyncCommands::get(&mut conn, &key).await
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?; .map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;
if blocked.is_some() { if let Some(_blocked) = blocked {
return Err(ChargingError::UsageBlocked(format!("User IP {} is already blocked", ip))); return Err(ChargingError::UsageBlocked(format!("User IP {} is already blocked", ip)));
} }
@@ -127,12 +127,21 @@ impl crate::charging::ChargingEngine {
let mut conn = self.redis_client.get_multiplexed_async_connection().await let mut conn = self.redis_client.get_multiplexed_async_connection().await
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?; .map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
let key = format!("block:{}", ip); // Check if user is blocked
let _: () = redis::AsyncCommands::del(&mut conn, &key).await let blocked_key = format!("blocked:{}", ip);
let blocked: Option<String> = redis::AsyncCommands::get(&mut conn, &blocked_key).await
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?; .map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;
info!("User IP: {} has been unblocked", ip); if let Some(_blocked) = blocked {
Ok(())
let _: () = redis::AsyncCommands::del(&mut conn, &blocked_key).await
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;
info!("User IP: {} has been unblocked", ip);
Ok(())
} else {
Ok(())
}
}).await.map_err(|e| match e { }).await.map_err(|e| match e {
CircuitBreakerError::Open => ChargingError::RedisConnection("Circuit breaker is open".to_string()), CircuitBreakerError::Open => ChargingError::RedisConnection("Circuit breaker is open".to_string()),
CircuitBreakerError::Inner(e) => e, CircuitBreakerError::Inner(e) => e,