Skip to content

Commit 49c7ce3

Browse files
committed
feat: add ConnectionInfo::on_closed
1 parent 50c9724 commit 49c7ce3

File tree

7 files changed

+44
-17
lines changed

7 files changed

+44
-17
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ unexpected_cfgs = { level = "warn", check-cfg = ["cfg(iroh_docsrs)", "cfg(iroh_l
4141
[workspace.lints.clippy]
4242
unused-async = "warn"
4343

44-
4544
[patch.crates-io]
46-
netwatch = { git = "https://github.com/n0-computer/net-tools", branch = "feat-multipath" }
47-
portmapper = { git = "https://github.com/n0-computer/net-tools", branch = "feat-multipath" }
45+
netwatch = { git = "https://github.com/n0-computer/net-tools", branch = "Frando/on_closed" }
46+
portmapper = { git = "https://github.com/n0-computer/net-tools", branch = "Frando/on_closed" }
4847

49-
[patch."https://github.com/n0-computer/quinn"]
48+
# [patch."https://github.com/n0-computer/quinn"]
5049
# iroh-quinn = { path = "../iroh-quinn/quinn" }
5150
# iroh-quinn-proto = { path = "../iroh-quinn/quinn-proto" }
5251
# iroh-quinn-udp = { path = "../iroh-quinn/quinn-udp" }

iroh-relay/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ postcard = { version = "1", default-features = false, features = [
4242
"use-std",
4343
"experimental-derive",
4444
] }
45-
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "main-iroh", default-features = false, features = ["rustls-ring"] }
46-
quinn-proto = { package = "iroh-quinn-proto", git = "https://github.com/n0-computer/quinn", branch = "main-iroh" }
45+
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "Frando/on_closed", default-features = false, features = ["rustls-ring"] }
46+
quinn-proto = { package = "iroh-quinn-proto", git = "https://github.com/n0-computer/quinn", branch = "Frando/on_closed" }
4747
rand = "0.9.2"
4848
reqwest = { version = "0.12", default-features = false, features = [
4949
"rustls-tls",

iroh/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ n0-watcher = "0.5"
4444
netwatch = { version = "0.12" }
4545
pin-project = "1"
4646
pkarr = { version = "5", default-features = false, features = ["relays"] }
47-
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "main-iroh", default-features = false, features = ["rustls-ring"] }
48-
quinn-proto = { package = "iroh-quinn-proto", git = "https://github.com/n0-computer/quinn", branch = "main-iroh" }
49-
quinn-udp = { package = "iroh-quinn-udp", git = "https://github.com/n0-computer/quinn", branch = "main-iroh" }
47+
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "Frando/on_closed", default-features = false, features = ["rustls-ring"] }
48+
quinn-proto = { package = "iroh-quinn-proto", git = "https://github.com/n0-computer/quinn", branch = "Frando/on_closed" }
49+
quinn-udp = { package = "iroh-quinn-udp", git = "https://github.com/n0-computer/quinn", branch = "Frando/on_closed" }
5050
rand = "0.9.2"
5151
reqwest = { version = "0.12", default-features = false, features = [
5252
"rustls-tls",
@@ -90,7 +90,7 @@ hickory-resolver = "0.25.1"
9090
igd-next = { version = "0.16", features = ["aio_tokio"] }
9191
netdev = { version = "0.38.1" }
9292
portmapper = { version = "0.12", default-features = false }
93-
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "main-iroh", default-features = false, features = ["runtime-tokio", "rustls-ring"] }
93+
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "Frando/on_closed", default-features = false, features = ["runtime-tokio", "rustls-ring"] }
9494
tokio = { version = "1", features = [
9595
"io-util",
9696
"macros",

iroh/bench/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ iroh = { path = ".." }
1212
iroh-metrics = "0.37"
1313
n0-future = "0.3.0"
1414
n0-error = "0.1.0"
15-
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "main-iroh" }
15+
quinn = { package = "iroh-quinn", git = "https://github.com/n0-computer/quinn", branch = "Frando/on_closed" }
1616
rand = "0.9.2"
1717
rcgen = "0.14"
1818
rustls = { version = "0.23.33", default-features = false, features = ["ring"] }

iroh/examples/monitor-connections.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use iroh::{
66
};
77
use n0_error::{Result, StackResultExt, StdResultExt, ensure_any};
88
use n0_future::task::AbortOnDropHandle;
9-
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
9+
use tokio::{
10+
sync::mpsc::{UnboundedReceiver, UnboundedSender},
11+
task::JoinSet,
12+
};
1013
use tracing::{Instrument, info, info_span};
1114

1215
const ALPN: &[u8] = b"iroh/test";
@@ -101,15 +104,32 @@ impl Monitor {
101104
}
102105

103106
async fn run(mut rx: UnboundedReceiver<ConnectionInfo>) {
107+
let mut tasks = JoinSet::new();
104108
loop {
105109
tokio::select! {
106110
Some(conn) = rx.recv() => {
107111
let alpn = String::from_utf8_lossy(conn.alpn()).to_string();
108112
let remote = conn.remote_id().fmt_short();
109113
info!(%remote, %alpn, rtt=?conn.rtt(), "new connection");
114+
tasks.spawn(async move {
115+
match conn.closed().await {
116+
Some((close_reason, stats)) => {
117+
// We have access to the final stats of the connection!
118+
info!(%remote, %alpn, ?close_reason, udp_rx=stats.udp_rx.bytes, udp_tx=stats.udp_tx.bytes, "connection closed");
119+
}
120+
None => {
121+
// The connection was closed before we could register our stats-on-close listener.
122+
info!(%remote, %alpn, "connection closed before tracking started");
123+
}
124+
}
125+
}.instrument(tracing::Span::current()));
110126
}
127+
Some(res) = tasks.join_next(), if !tasks.is_empty() => res.expect("conn close task panicked"),
111128
else => break,
112129
}
130+
while let Some(res) = tasks.join_next().await {
131+
res.expect("conn close task panicked");
132+
}
113133
}
114134
}
115135
}

iroh/src/endpoint/connection.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,14 @@ impl ConnectionInfo {
16081608
pub fn side(&self) -> Side {
16091609
self.side
16101610
}
1611+
1612+
/// Waits for the connection to be closed, and returns the close reason and final connection stats.
1613+
///
1614+
/// Returns `None` if the connection has been dropped already before this call.
1615+
pub async fn closed(&self) -> Option<(ConnectionError, ConnectionStats)> {
1616+
let fut = self.inner.upgrade()?.on_closed();
1617+
Some(fut.await)
1618+
}
16111619
}
16121620

16131621
#[cfg(test)]

0 commit comments

Comments
 (0)