From c8a332fb85956acbb0a0a13e6d91a985330e4ea8 Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Fri, 9 Aug 2024 21:00:42 +0800 Subject: [PATCH] feat: add metrics for dropped outgoing messages (#10225) Co-authored-by: Matthias Seitz --- crates/net/network/src/metrics.rs | 2 ++ crates/net/network/src/session/mod.rs | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/net/network/src/metrics.rs b/crates/net/network/src/metrics.rs index 8f7b34617dd5..4333cf1408b0 100644 --- a/crates/net/network/src/metrics.rs +++ b/crates/net/network/src/metrics.rs @@ -83,6 +83,8 @@ pub struct NetworkMetrics { pub struct SessionManagerMetrics { /// Number of successful outgoing dial attempts. pub(crate) total_dial_successes: Counter, + /// Number of dropped outgoing peer messages. + pub(crate) total_outgoing_peer_messages_dropped: Counter, } /// Metrics for the [`TransactionsManager`](crate::transactions::TransactionsManager). diff --git a/crates/net/network/src/session/mod.rs b/crates/net/network/src/session/mod.rs index 7209e7414106..74f303df7b88 100644 --- a/crates/net/network/src/session/mod.rs +++ b/crates/net/network/src/session/mod.rs @@ -41,7 +41,7 @@ use secp256k1::SecretKey; use tokio::{ io::{AsyncRead, AsyncWrite}, net::TcpStream, - sync::{mpsc, oneshot}, + sync::{mpsc, mpsc::error::TrySendError, oneshot}, }; use tokio_stream::wrappers::ReceiverStream; use tokio_util::sync::PollSender; @@ -346,7 +346,18 @@ impl SessionManager { /// Sends a message to the peer's session pub fn send_message(&mut self, peer_id: &PeerId, msg: PeerMessage) { if let Some(session) = self.active_sessions.get_mut(peer_id) { - let _ = session.commands_to_session.try_send(SessionCommand::Message(msg)); + let _ = session.commands_to_session.try_send(SessionCommand::Message(msg)).inspect_err( + |e| { + if let TrySendError::Full(_) = e { + debug!( + target: "net::session", + ?peer_id, + "session command buffer full, dropping message" + ); + self.metrics.total_outgoing_peer_messages_dropped.increment(1); + } + }, + ); } }