feat: use MeteredSender for ActiveSession Sender Half (#1150)

This commit is contained in:
Aurélien
2023-02-03 12:43:39 +01:00
committed by GitHub
parent a55e7fd9b5
commit 13eea35c63
5 changed files with 20 additions and 6 deletions

1
Cargo.lock generated
View File

@ -4396,6 +4396,7 @@ dependencies = [
"reth-ecies", "reth-ecies",
"reth-eth-wire", "reth-eth-wire",
"reth-interfaces", "reth-interfaces",
"reth-metrics-common",
"reth-metrics-derive", "reth-metrics-derive",
"reth-net-common", "reth-net-common",
"reth-network", "reth-network",

View File

@ -9,7 +9,7 @@ use tokio::sync::mpsc::{
}; };
/// Network throughput metrics /// Network throughput metrics
#[derive(Metrics)] #[derive(Clone, Metrics)]
#[metrics(dynamic = true)] #[metrics(dynamic = true)]
struct MeteredSenderMetrics { struct MeteredSenderMetrics {
/// Number of messages sent /// Number of messages sent
@ -19,6 +19,7 @@ struct MeteredSenderMetrics {
} }
/// Manages updating the network throughput metrics for a metered stream /// Manages updating the network throughput metrics for a metered stream
#[derive(Debug)]
pub struct MeteredSender<T> { pub struct MeteredSender<T> {
/// The [`Sender`] that this wraps around /// The [`Sender`] that this wraps around
sender: Sender<T>, sender: Sender<T>,
@ -34,7 +35,7 @@ impl<T> MeteredSender<T> {
/// Calls the underlying [`Sender`]'s `try_send`, incrementing the appropriate /// Calls the underlying [`Sender`]'s `try_send`, incrementing the appropriate
/// metrics depending on the result. /// metrics depending on the result.
pub fn try_send(&mut self, message: T) -> Result<(), TrySendError<T>> { pub fn try_send(&self, message: T) -> Result<(), TrySendError<T>> {
match self.sender.try_send(message) { match self.sender.try_send(message) {
Ok(()) => { Ok(()) => {
self.metrics.messages_sent.increment(1); self.metrics.messages_sent.increment(1);
@ -62,3 +63,9 @@ impl<T> MeteredSender<T> {
} }
} }
} }
impl<T> Clone for MeteredSender<T> {
fn clone(&self) -> Self {
Self { sender: self.sender.clone(), metrics: self.metrics.clone() }
}
}

View File

@ -30,6 +30,7 @@ reth-rlp-derive = { path = "../../rlp/rlp-derive" }
reth-tasks = { path = "../../tasks" } reth-tasks = { path = "../../tasks" }
reth-transaction-pool = { path = "../../transaction-pool" } reth-transaction-pool = { path = "../../transaction-pool" }
reth-provider = { path = "../../storage/provider"} reth-provider = { path = "../../storage/provider"}
reth-metrics-common = { path = "../../metrics/common" }
# async/futures # async/futures
futures = "0.3" futures = "0.3"

View File

@ -19,6 +19,7 @@ use reth_eth_wire::{
DisconnectReason, EthMessage, EthStream, P2PStream, DisconnectReason, EthMessage, EthStream, P2PStream,
}; };
use reth_interfaces::p2p::error::RequestError; use reth_interfaces::p2p::error::RequestError;
use reth_metrics_common::metered_sender::MeteredSender;
use reth_net_common::bandwidth_meter::MeteredStream; use reth_net_common::bandwidth_meter::MeteredStream;
use reth_primitives::PeerId; use reth_primitives::PeerId;
use std::{ use std::{
@ -74,7 +75,7 @@ pub(crate) struct ActiveSession {
/// Incoming commands from the manager /// Incoming commands from the manager
pub(crate) commands_rx: ReceiverStream<SessionCommand>, pub(crate) commands_rx: ReceiverStream<SessionCommand>,
/// Sink to send messages to the [`SessionManager`](super::SessionManager). /// Sink to send messages to the [`SessionManager`](super::SessionManager).
pub(crate) to_session: mpsc::Sender<ActiveSessionMessage>, pub(crate) to_session: MeteredSender<ActiveSessionMessage>,
/// Incoming request to send to delegate to the remote peer. /// Incoming request to send to delegate to the remote peer.
pub(crate) request_tx: Fuse<ReceiverStream<PeerRequest>>, pub(crate) request_tx: Fuse<ReceiverStream<PeerRequest>>,
/// All requests sent to the remote peer we're waiting on a response /// All requests sent to the remote peer we're waiting on a response
@ -744,7 +745,10 @@ mod tests {
remote_capabilities: Arc::clone(&capabilities), remote_capabilities: Arc::clone(&capabilities),
session_id, session_id,
commands_rx: ReceiverStream::new(commands_rx), commands_rx: ReceiverStream::new(commands_rx),
to_session: self.active_session_tx.clone(), to_session: MeteredSender::new(
self.active_session_tx.clone(),
"network_active_session",
),
request_tx: ReceiverStream::new(messages_rx).fuse(), request_tx: ReceiverStream::new(messages_rx).fuse(),
inflight_requests: Default::default(), inflight_requests: Default::default(),
conn, conn,

View File

@ -19,6 +19,7 @@ use reth_eth_wire::{
errors::EthStreamError, errors::EthStreamError,
DisconnectReason, HelloMessage, Status, UnauthedEthStream, UnauthedP2PStream, DisconnectReason, HelloMessage, Status, UnauthedEthStream, UnauthedP2PStream,
}; };
use reth_metrics_common::metered_sender::MeteredSender;
use reth_net_common::bandwidth_meter::{BandwidthMeter, MeteredStream}; use reth_net_common::bandwidth_meter::{BandwidthMeter, MeteredStream};
use reth_primitives::{ForkFilter, ForkId, ForkTransition, PeerId, H256, U256}; use reth_primitives::{ForkFilter, ForkId, ForkTransition, PeerId, H256, U256};
use reth_tasks::TaskExecutor; use reth_tasks::TaskExecutor;
@ -91,7 +92,7 @@ pub(crate) struct SessionManager {
/// ///
/// When active session state is reached, the corresponding [`ActiveSessionHandle`] will get a /// When active session state is reached, the corresponding [`ActiveSessionHandle`] will get a
/// clone of this sender half. /// clone of this sender half.
active_session_tx: mpsc::Sender<ActiveSessionMessage>, active_session_tx: MeteredSender<ActiveSessionMessage>,
/// Receiver half that listens for [`ActiveSessionMessage`] produced by pending sessions. /// Receiver half that listens for [`ActiveSessionMessage`] produced by pending sessions.
active_session_rx: ReceiverStream<ActiveSessionMessage>, active_session_rx: ReceiverStream<ActiveSessionMessage>,
/// Used to measure inbound & outbound bandwidth across all managed streams /// Used to measure inbound & outbound bandwidth across all managed streams
@ -129,7 +130,7 @@ impl SessionManager {
active_sessions: Default::default(), active_sessions: Default::default(),
pending_sessions_tx, pending_sessions_tx,
pending_session_rx: ReceiverStream::new(pending_sessions_rx), pending_session_rx: ReceiverStream::new(pending_sessions_rx),
active_session_tx, active_session_tx: MeteredSender::new(active_session_tx, "network_active_session"),
active_session_rx: ReceiverStream::new(active_session_rx), active_session_rx: ReceiverStream::new(active_session_rx),
bandwidth_meter, bandwidth_meter,
} }