From 400f248e83ccbb40b9630a3651e775d855a3ce2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 26 Mar 2024 11:55:28 +0000 Subject: [PATCH] cherry-pick 80066d330527b7993cc85833b1b3ae96cc487bb9 to fix wasm build --- .../src/client/packet_statistics_control.rs | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/common/client-core/src/client/packet_statistics_control.rs b/common/client-core/src/client/packet_statistics_control.rs index 8f5aaf434d..a1173bec66 100644 --- a/common/client-core/src/client/packet_statistics_control.rs +++ b/common/client-core/src/client/packet_statistics_control.rs @@ -3,8 +3,7 @@ use std::{ time::{Duration, Instant}, }; -use log::{info, warn}; -use nym_metrics::{inc, inc_by, metrics}; +use nym_metrics::{inc, inc_by}; use si_scale::helpers::bibytes2; // Metrics server @@ -508,7 +507,7 @@ impl PacketStatisticsControl { cfg_if::cfg_if! { if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { log::warn!("Metrics server is not supported on wasm32-unknown-unknown"); - let listener = None; + let listener: Option = None; } else { let mut metrics_port = 18000; let listener: Option; @@ -516,9 +515,9 @@ impl PacketStatisticsControl { let addr = SocketAddr::from(([0, 0, 0, 0], metrics_port)); match TcpListener::bind(addr).await { Ok(l) => { - info!("###############################"); - info!("Metrics endpoint is at: {:?}", l.local_addr()); - info!("###############################"); + log::info!("###############################"); + log::info!("Metrics endpoint is at: {:?}", l.local_addr()); + log::info!("###############################"); listener = Some(l); break; }, @@ -545,10 +544,11 @@ impl PacketStatisticsControl { } }, // conditional will disable the branch if we're in wasm32-unknown-unknown - result = listener.as_ref().unwrap().accept(), if listener.is_some() => { + // use `_` to calm down clippy when running for wasm + _result = listener.as_ref().unwrap().accept(), if listener.is_some() => { cfg_if::cfg_if! { if #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] { - if let Ok((stream, _)) = result { + if let Ok((stream, _)) = _result { let io = TokioIo::new(stream); tokio::task::spawn(async move { @@ -556,11 +556,11 @@ impl PacketStatisticsControl { .serve_connection(io, service_fn(serve_metrics)) .await { - warn!("Error serving connection: {:?}", err); + log::warn!("Error serving connection: {:?}", err); } }); } else { - warn!("Error accepting connection"); + log::warn!("Error accepting connection"); } } } @@ -590,8 +590,19 @@ impl PacketStatisticsControl { } } +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] async fn serve_metrics( _: Request, ) -> Result>, Infallible> { + use nym_metrics::metrics; + Ok(Response::new(Full::new(Bytes::from(metrics!())))) } + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +struct WasmEmpty; + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +impl WasmEmpty { + async fn accept(&self) {} +}