From 1d489a0a8a910883a07e5bcd57e97479926e14a3 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sun, 26 Apr 2026 02:00:27 +0300 Subject: [PATCH] refactor: Make eBPF dependencies optional and add conditional compilation support to packet-gateway - Add ebpf feature flag to Cargo.toml with default disabled - Make aya and aya-log dependencies optional and tied to ebpf feature - Add #[cfg(feature = "ebpf")] conditional compilation directives for EbpfManager imports and usage - Add feature-gated readiness check logic (ebpf_ok only checked when feature enabled) - Add feature-gated metrics_handler implementations (full version with eBPF, stub version without) - Change --- apps/packet-gateway/Cargo.toml | 8 ++++++-- apps/packet-gateway/src/charging_client.rs | 2 +- apps/packet-gateway/src/health.rs | 24 ++++++++++++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/apps/packet-gateway/Cargo.toml b/apps/packet-gateway/Cargo.toml index 8d4b553..ef99cbb 100644 --- a/apps/packet-gateway/Cargo.toml +++ b/apps/packet-gateway/Cargo.toml @@ -6,9 +6,13 @@ edition.workspace = true rust-version.workspace = true license.workspace = true +[features] +default = [] +ebpf = [] + [dependencies] -aya = { workspace = true } -aya-log = { workspace = true } +aya = { version = "0.13.1", features = ["async_tokio"], optional = true } +aya-log = { version = "0.2.1", optional = true } tokio = { workspace = true } redis = { workspace = true } anyhow = { workspace = true } diff --git a/apps/packet-gateway/src/charging_client.rs b/apps/packet-gateway/src/charging_client.rs index 7eccb0c..3130e26 100644 --- a/apps/packet-gateway/src/charging_client.rs +++ b/apps/packet-gateway/src/charging_client.rs @@ -78,7 +78,7 @@ impl ChargingEngineClient { } Err(e) => { error!("Failed to send usage to charging engine: {}", e); - Err(e.context("Failed to send usage to charging engine")) + Err(anyhow::anyhow!("Failed to send usage to charging engine: {}", e)) } } } diff --git a/apps/packet-gateway/src/health.rs b/apps/packet-gateway/src/health.rs index 9d3d9b6..d79a61e 100644 --- a/apps/packet-gateway/src/health.rs +++ b/apps/packet-gateway/src/health.rs @@ -6,6 +6,8 @@ use redis::AsyncCommands; use serde::Serialize; use std::sync::atomic::AtomicBool; use std::sync::Arc; + +#[cfg(feature = "ebpf")] use crate::ebpf::EbpfManager; #[derive(Serialize)] @@ -57,7 +59,7 @@ pub async fn readiness_handler( let redis_ok = match redis_client.get_multiplexed_async_connection().await { Ok(mut conn) => { - match redis::cmd("PING").query_async::<_, String>(&mut conn).await { + match redis::AsyncCommands::ping::(&mut conn).await { Ok(_) => { checks.insert("redis".to_string(), serde_json::Value::String("ok".to_string())); true @@ -77,7 +79,12 @@ pub async fn readiness_handler( let ebpf_ok = ebpf_attached.load(std::sync::atomic::Ordering::Relaxed); checks.insert("ebpf".to_string(), serde_json::Value::String(if ebpf_ok { "ok".to_string() } else { "not attached".to_string() })); - if redis_ok && ebpf_ok { + #[cfg(feature = "ebpf")] + let ready = redis_ok && ebpf_ok; + #[cfg(not(feature = "ebpf"))] + let ready = redis_ok; + + if ready { Ok(Json(ReadinessResponse { status: "ready".to_string(), service: "packet-gateway".to_string(), @@ -89,6 +96,7 @@ pub async fn readiness_handler( } } +#[cfg(feature = "ebpf")] pub async fn metrics_handler( State(ebpf_manager): State>, ) -> Json { @@ -113,3 +121,15 @@ pub async fn metrics_handler( low_credit_users, }) } + +#[cfg(not(feature = "ebpf"))] +pub async fn metrics_handler() -> Json { + Json(MetricsResponse { + status: "ok".to_string(), + service: "packet-gateway".to_string(), + timestamp: chrono::Utc::now().to_rfc3339(), + tracked_ips: 0, + total_bytes_processed: 0, + low_credit_users: 0, + }) +}