Skip to content

Commit

Permalink
feat(iroh-net): Metrics for connections that switch to direct
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Oct 7, 2024
1 parent e2ea45f commit 6881a25
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 12 additions & 5 deletions iroh-net/src/endpoint/rtt_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use std::collections::HashMap;
use futures_concurrency::stream::stream_group;
use futures_lite::StreamExt;
use iroh_base::key::NodeId;
use iroh_metrics::inc;
use tokio::sync::{mpsc, Notify};
use tokio::task::JoinHandle;
use tokio::time::Duration;
use tracing::{debug, error, info_span, trace, warn, Instrument};

use crate::magicsock::{ConnectionType, ConnectionTypeStream};
use crate::metrics::MagicsockMetrics;

#[derive(Debug)]
pub(super) struct RttHandle {
Expand Down Expand Up @@ -63,7 +65,7 @@ struct RttActor {
///
/// These are weak references so not to keep the connections alive. The key allows
/// removing the corresponding stream from `conn_type_changes`.
connections: HashMap<stream_group::Key, (quinn::WeakConnectionHandle, NodeId)>,
connections: HashMap<stream_group::Key, (quinn::WeakConnectionHandle, NodeId, bool)>,
/// A way to notify the main actor loop to run over.
///
/// E.g. when a new stream was added.
Expand Down Expand Up @@ -111,8 +113,9 @@ impl RttActor {
node_id: NodeId,
) {
let key = self.connection_events.insert(conn_type_changes);
self.connections.insert(key, (connection, node_id));
self.connections.insert(key, (connection, node_id, false));
self.tick.notify_one();
inc!(MagicsockMetrics, connection_handshake_success);
}

/// Performs the congestion controller reset for a magic socket path change.
Expand All @@ -123,14 +126,18 @@ impl RttActor {
/// happens commonly.
fn do_reset_rtt(&mut self, item: Option<(stream_group::Key, ConnectionType)>) {
match item {
Some((key, new_conn_type)) => match self.connections.get(&key) {
Some((handle, node_id)) => {
Some((key, new_conn_type)) => match self.connections.get_mut(&key) {
Some((handle, node_id, was_direct_before)) => {
if handle.reset_congestion_state() {
debug!(
node_id = %node_id.fmt_short(),
new_type = ?new_conn_type,
"Congestion controller state reset",
);
if !*was_direct_before {
*was_direct_before = true;
inc!(MagicsockMetrics, connection_became_direct);
}
} else {
debug!(
node_id = %node_id.fmt_short(),
Expand All @@ -149,7 +156,7 @@ impl RttActor {

/// Performs cleanup for closed connection.
fn do_connections_cleanup(&mut self) {
for (key, (handle, node_id)) in self.connections.iter() {
for (key, (handle, node_id, _)) in self.connections.iter() {
if !handle.is_alive() {
trace!(node_id = %node_id.fmt_short(), "removing stale connection");
self.connection_events.remove(*key);
Expand Down
8 changes: 8 additions & 0 deletions iroh-net/src/magicsock/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ pub struct Metrics {
pub nodes_contacted: Counter,
/// Number of nodes we have managed to contact directly.
pub nodes_contacted_directly: Counter,

/// Number of connections with a successful handshake
pub connection_handshake_success: Counter,
/// Number of connections that became direct
pub connection_became_direct: Counter,
}

impl Default for Metrics {
Expand Down Expand Up @@ -141,6 +146,9 @@ impl Default for Metrics {

nodes_contacted: Counter::new("nodes_contacted"),
nodes_contacted_directly: Counter::new("nodes_contacted_directly"),

connection_handshake_success: Counter::new("connection_handshake_success"),
connection_became_direct: Counter::new("connection_became_direct"),
}
}
}
Expand Down

0 comments on commit 6881a25

Please sign in to comment.