Skip to content

Commit

Permalink
Improve ipv6 logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AgeManning committed Oct 15, 2024
1 parent 30e43d5 commit d38c5fb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub async fn run(server: &Server) {
log::info!("Server listening on {:?}", listen_config);
// Build the discv5 server using a default config
let config = ConfigBuilder::new(listen_config)
.request_timeout(Duration::from_secs(3))
.enr_peer_update_min(peer_update_min.try_into().unwrap())
.build();
let mut discv5 = Discv5::new(enr, enr_key, config).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub async fn run(discv5: Arc<Discv5>, break_time: Duration) {
}
}
}
log::info!("Connected Peers: {}", discv5.connected_peers());

tokio::time::sleep(break_time).await;
}
}
51 changes: 45 additions & 6 deletions src/server/services/stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use discv5::{ConnectionDirection, ConnectionState, Discv5};
use discv5::{ConnectionDirection, ConnectionState, Discv5, Event};
use log::info;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -7,9 +8,29 @@ use std::time::Duration;
pub fn run(discv5: Arc<Discv5>, break_time: Option<Duration>, stats: u64) {
let break_time = break_time.unwrap_or_else(|| Duration::from_secs(10));
tokio::spawn(async move {
let mut event_stream = discv5.event_stream().await.unwrap();
let mut stats_interval = tokio::time::interval(break_time);
let mut ipv6_connections = 0;
let mut ipv4_connections = 0;
loop {
tokio::time::sleep(break_time).await;
print_stats(Arc::clone(&discv5), stats);
tokio::select! {
_ = stats_interval.tick() => {
print_global_stats(Arc::clone(&discv5), ipv6_connections, ipv4_connections);
print_bucket_stats(Arc::clone(&discv5), stats);
}
Some(event) = event_stream.recv() => {
match event {

Check warning on line 22 in src/server/services/stats.rs

View workflow job for this annotation

GitHub Actions / cargo-clippy

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
Event::SessionEstablished(_enr,addr) => {
if addr.is_ipv6() {
ipv6_connections += 1;
} else if addr.is_ipv4() {
ipv4_connections +=1;
}
}
_ => {}
}
}
}
}
});
}
Expand All @@ -27,10 +48,21 @@ pub struct BucketStatistic {
pub incoming_peers: u64,
/// Outgoing Peer Count
pub outgoing_peers: u64,
/// The number of ipv6 nodes
pub ipv6_peers: u64,
}

/// Prints discv5 global statistics
fn print_global_stats(discv5: Arc<Discv5>, ipv6_connections: u64, ipv4_connections: u64) {
info!("Peers in routing table: {}", discv5.connected_peers());
info!(
"Sessions historically established, ipv4: {}, ipv6: {}",
ipv4_connections, ipv6_connections
);
}

/// Prints discv5 server stats.
pub fn print_stats(discv5: Arc<Discv5>, stats: u64) {
pub fn print_bucket_stats(discv5: Arc<Discv5>, stats: u64) {
let table_entries = discv5.table_entries();
let self_id: discv5::Key<_> = discv5.local_enr().node_id().into();

Expand All @@ -55,8 +87,9 @@ pub fn print_stats(discv5: Arc<Discv5>, stats: u64) {
let mut connected_incoming_peers = 0;
let mut connected_outgoing_peers = 0;
let mut disconnected_peers = 0;
let mut ipv6_peers = 0;

for (_enr, status) in entries {
for (enr, status) in entries {
match (status.state, status.direction) {
(ConnectionState::Connected, ConnectionDirection::Incoming) => {
connected_peers += 1;
Expand All @@ -70,6 +103,9 @@ pub fn print_stats(discv5: Arc<Discv5>, stats: u64) {
disconnected_peers += 1;
}
}
if matches!(status.state, ConnectionState::Connected) && enr.udp6_socket().is_some() {
ipv6_peers += 1;
}
}

bucket_stats.push(BucketStatistic {
Expand All @@ -78,6 +114,7 @@ pub fn print_stats(discv5: Arc<Discv5>, stats: u64) {
disconnected_peers,
incoming_peers: connected_incoming_peers,
outgoing_peers: connected_outgoing_peers,
ipv6_peers,
});
}

Expand All @@ -92,13 +129,15 @@ pub fn print_stats(discv5: Arc<Discv5>, stats: u64) {
disconnected_peers,
incoming_peers: connected_incoming_peers,
outgoing_peers: connected_outgoing_peers,
ipv6_peers,
} = bucket_stat;
log::info!(
"Bucket {} statistics: Connected peers: {} (Incoming: {}, Outgoing: {}), Disconnected Peers: {}",
"Bucket {} statistics: Connected peers: {} (Incoming: {}, Outgoing: {}, ipv6: {}), Disconnected Peers: {}",
bucket,
connected_peers,
connected_incoming_peers,
connected_outgoing_peers,
ipv6_peers,
disconnected_peers
);
}
Expand Down

0 comments on commit d38c5fb

Please sign in to comment.