Skip to content

Commit

Permalink
Add clone() to NetworkingMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dailinsubjam committed Sep 22, 2023
1 parent 35e4754 commit 417ab81
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/hotshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ sha3 = "^0.10"
snafu = { workspace = true }
surf-disco = { workspace = true }
time = { workspace = true }
dyn-clone = { git = "https://github.com/dtolnay/dyn-clone", tag = "1.0.14" }

tracing = { workspace = true }
typenum = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/hotshot/src/traits/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub use hotshot_types::traits::network::{
ChannelSendSnafu, CouldNotDeliverSnafu, FailedToDeserializeSnafu, FailedToSerializeSnafu,
NetworkError, NetworkReliability, NoSuchNodeSnafu, ShutDownSnafu,
};

use hotshot_types::traits::metrics::{Counter, Gauge, Metrics};

/// Contains the metrics that we're interested in from the networking interfaces
#[derive(Clone)]
pub struct NetworkingMetrics {
#[allow(dead_code)]
/// A [`Gauge`] which tracks how many peers are connected
Expand Down
9 changes: 6 additions & 3 deletions crates/hotshot/src/traits/networking/libp2p_network.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Libp2p based/production networking implementation
//! This module provides a libp2p based networking implementation where each node in the
//! network forms a tcp or udp connection to a subset of other nodes in the network
use super::NetworkingMetrics;
use crate::NodeImplementation;
use async_compatibility_layer::{
Expand Down Expand Up @@ -320,7 +319,7 @@ impl<M: NetworkMsg, K: SignatureKey + 'static> Libp2pNetwork<M, K> {
let (node_lookup_send, node_lookup_recv) = unbounded();
let (cache_gc_shutdown_send, cache_gc_shutdown_recv) = unbounded::<()>();

let result = Libp2pNetwork {
let mut result = Libp2pNetwork {
inner: Arc::new(Libp2pNetworkInner {
handle: network_handle,
broadcast_recv,
Expand Down Expand Up @@ -404,13 +403,14 @@ impl<M: NetworkMsg, K: SignatureKey + 'static> Libp2pNetwork<M, K> {
}

/// Initiates connection to the outside world
fn spawn_connect(&self, id: usize) {
fn spawn_connect(&mut self, id: usize) {
let pk = self.inner.pk.clone();
let bootstrap_ref = self.inner.bootstrap_addrs.clone();
let num_bootstrap = self.inner.bootstrap_addrs_len;
let handle = self.inner.handle.clone();
let is_bootstrapped = self.inner.is_bootstrapped.clone();
let node_type = self.inner.handle.config().node_type;
let metrics_connected_peers = self.inner.metrics.connected_peers.clone();
async_spawn({
let is_ready = self.inner.is_ready.clone();
async move {
Expand Down Expand Up @@ -439,6 +439,9 @@ impl<M: NetworkMsg, K: SignatureKey + 'static> Libp2pNetwork<M, K> {
.await
.unwrap();

let connected_num = handle.num_connected().await?;
metrics_connected_peers.set(connected_num);

while !is_bootstrapped.load(Ordering::Relaxed) {
async_sleep(Duration::from_secs(1)).await;
}
Expand Down
1 change: 1 addition & 0 deletions crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ time = { workspace = true }
tracing = { workspace = true }
ethereum-types = { workspace = true }
typenum = { workspace = true }
dyn-clone = { git = "https://github.com/dtolnay/dyn-clone", tag = "1.0.14" }

[dev-dependencies]
serde_json = "1.0.107"
Expand Down
15 changes: 10 additions & 5 deletions crates/types/src/traits/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! - [`Label`]: Stores the last string (example usage: current version, network online/offline)
use std::fmt::Debug;
use dyn_clone::DynClone;

/// The metrics type.
pub trait Metrics: Send + Sync {
Expand Down Expand Up @@ -78,12 +79,12 @@ impl Label for NoMetrics {
}

/// An ever-incrementing counter
pub trait Counter: Send + Sync + Debug {
pub trait Counter: Send + Sync + Debug + DynClone {
/// Add a value to the counter
fn add(&self, amount: usize);
}
/// A gauge that stores the latest value.
pub trait Gauge: Send + Sync + Debug {
pub trait Gauge: Send + Sync + Debug + DynClone {
/// Set the gauge value
fn set(&self, amount: usize);

Expand All @@ -92,16 +93,20 @@ pub trait Gauge: Send + Sync + Debug {
}

/// A histogram which will record a series of points.
pub trait Histogram: Send + Sync + Debug {
pub trait Histogram: Send + Sync + Debug + DynClone {
/// Add a point to this histogram.
fn add_point(&self, point: f64);
}

/// A label that stores the last string value.
pub trait Label: Send + Sync {
pub trait Label: Send + Sync + DynClone {
/// Set the label value
fn set(&self, value: String);
}
dyn_clone::clone_trait_object!(Gauge);
dyn_clone::clone_trait_object!(Counter);
dyn_clone::clone_trait_object!(Histogram);
dyn_clone::clone_trait_object!(Label);

#[cfg(test)]
mod test {
Expand All @@ -111,7 +116,7 @@ mod test {
sync::{Arc, Mutex},
};

#[derive(Debug)]
#[derive(Debug, Clone)]
struct TestMetrics {
prefix: String,
values: Arc<Mutex<Inner>>,
Expand Down

0 comments on commit 417ab81

Please sign in to comment.