forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(net): Metered senders (paradigmxyz#726)
Co-authored-by: Bjerg <[email protected]>
- Loading branch information
Showing
7 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "reth-metrics-common" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/paradigmxyz/reth" | ||
description = """ | ||
Common metric types across the Reth codebase | ||
""" | ||
|
||
[dependencies] | ||
# reth | ||
reth-metrics-derive = { path = "../../metrics/metrics-derive" } | ||
|
||
# async | ||
tokio = { version = "1.21.2", features = ["full"] } | ||
|
||
# metrics | ||
metrics = "0.20.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![warn(missing_docs, unreachable_pub)] | ||
#![deny(unused_must_use, rust_2018_idioms)] | ||
#![doc(test( | ||
no_crate_inject, | ||
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) | ||
))] | ||
|
||
//! Common metric types that can be used across the Reth codebase | ||
pub mod metered_sender; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Support for metering senders. Facilitates debugging by exposing metrics for number of messages | ||
//! sent, number of errors, etc. | ||
use metrics::Counter; | ||
use reth_metrics_derive::Metrics; | ||
use tokio::sync::mpsc::{ | ||
error::{SendError, TrySendError}, | ||
Sender, | ||
}; | ||
|
||
/// Network throughput metrics | ||
#[derive(Metrics)] | ||
#[metrics(dynamic = true)] | ||
struct MeteredSenderMetrics { | ||
/// Number of messages sent | ||
messages_sent: Counter, | ||
/// Number of failed message deliveries | ||
send_errors: Counter, | ||
} | ||
|
||
/// Manages updating the network throughput metrics for a metered stream | ||
pub struct MeteredSender<T> { | ||
/// The [`Sender`] that this wraps around | ||
sender: Sender<T>, | ||
/// Holds the gauges for inbound and outbound throughput | ||
metrics: MeteredSenderMetrics, | ||
} | ||
|
||
impl<T> MeteredSender<T> { | ||
/// Creates a new [`MeteredSender`] wrapping around the provided [`Sender`] | ||
pub fn new(sender: Sender<T>, scope: &'static str) -> Self { | ||
Self { sender, metrics: MeteredSenderMetrics::new(scope) } | ||
} | ||
|
||
/// Calls the underlying [`Sender`]'s `try_send`, incrementing the appropriate | ||
/// metrics depending on the result. | ||
pub fn try_send(&mut self, message: T) -> Result<(), TrySendError<T>> { | ||
match self.sender.try_send(message) { | ||
Ok(()) => { | ||
self.metrics.messages_sent.increment(1); | ||
Ok(()) | ||
} | ||
Err(error) => { | ||
self.metrics.send_errors.increment(1); | ||
Err(error) | ||
} | ||
} | ||
} | ||
|
||
/// Calls the underlying [`Sender`]'s `send`, incrementing the appropriate | ||
/// metrics depending on the result. | ||
pub async fn send(&mut self, value: T) -> Result<(), SendError<T>> { | ||
match self.sender.send(value).await { | ||
Ok(()) => { | ||
self.metrics.messages_sent.increment(1); | ||
Ok(()) | ||
} | ||
Err(error) => { | ||
self.metrics.send_errors.increment(1); | ||
Err(error) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters