From 2dd6c3304d192399120c01edb97a19eca9738192 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sat, 25 Apr 2026 03:06:37 +0300 Subject: [PATCH] feat: Add configurable expiration times for credit and block operations via environment variables - Add CREDIT_EXPIRATION_SECONDS environment variable to configure credit key expiration (defaults to 86400) - Add BLOCK_EXPIRATION_SECONDS environment variable to configure block key expiration (defaults to 86400) - Replace hardcoded 86400 expiration values with configurable variables in add_credit and block_user - Update block_user warning message to include expiration duration --- .../src/charging/credit_management.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/charging-engine/src/charging/credit_management.rs b/apps/charging-engine/src/charging/credit_management.rs index 391e376..14e7c0f 100644 --- a/apps/charging-engine/src/charging/credit_management.rs +++ b/apps/charging-engine/src/charging/credit_management.rs @@ -24,8 +24,12 @@ impl crate::charging::ChargingEngine { let new_balance: u64 = redis::AsyncCommands::incr(&mut conn, &key, bytes_to_add).await .map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?; - // Set expiration if this is a new key - let _: () = redis::AsyncCommands::expire(&mut conn, &key, 86400).await.unwrap_or(()); + // Set expiration if this is a new key (configurable via CREDIT_EXPIRATION_SECONDS, defaults to 24 hours) + let credit_expiration: u64 = std::env::var("CREDIT_EXPIRATION_SECONDS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(86400); + let _: () = redis::AsyncCommands::expire(&mut conn, &key, credit_expiration as i64).await.unwrap_or(()); info!("Added {} bytes credit to IP: {}, new balance: {}", bytes_to_add, ip, new_balance); Ok(new_balance) @@ -75,10 +79,14 @@ impl crate::charging::ChargingEngine { let _: () = redis::AsyncCommands::set(&mut conn, &key, "blocked").await .map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?; - // Set expiration for block (24 hours) - let _: () = redis::AsyncCommands::expire(&mut conn, &key, 86400).await.unwrap_or(()); + // Set expiration for block (configurable via BLOCK_EXPIRATION_SECONDS, defaults to 24 hours) + let block_expiration: u64 = std::env::var("BLOCK_EXPIRATION_SECONDS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(86400); + let _: () = redis::AsyncCommands::expire(&mut conn, &key, block_expiration as i64).await.unwrap_or(()); - warn!("User IP: {} has been blocked", ip); + warn!("User IP: {} has been blocked for {} seconds", ip, block_expiration); Ok(()) }