Skip to content

Commit

Permalink
feat(iroh)!: Wrap the Connection struct so we own the type (#3110)
Browse files Browse the repository at this point in the history
## Description

This makes iroh own the Connection type.  This is desirable because:

- Some: upstream APIs don't work well for us,
  e.g. Connection::remote_address is not that meaningful.  This allows
  us to remove those and replace them with more appropriate APIs.  This
  has not yet been done in this PR however.

- The connection type and changes are currently on the Endpoint, they
  also belong on the Connection.

- Connection information, like which pats are used, the latencies of
  those paths etc, also belong on the Connection.

- It is generally not great to expose types which you have no control
  over in your public API.  For the 1.0 we do not want to have any
  such structs.  This takes an important step in that direction.

As part of this change Connection::remote_node_id replaces the
get_remote_node_id function.  Also Connection::alpn now exists.

## Breaking Changes

### iroh

- `iroh::endpoint::get_remote_node_id` has been removed.  Use
  `iroh::endpoint::Connection::remote_node_id` instead.

## Notes & open questions

This does not yet clean up the existing methods in the Connection,
preserving most of them as-is.  It is easier to handle those
separately as each one involves careful decisions about the API.

Replaces #2768

## Change checklist

- [x] Self-review.
- [x] Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.
- [x] Tests if relevant.
- [x] All breaking changes documented.
  • Loading branch information
flub authored Jan 23, 2025
1 parent 2b92db4 commit 2e61ff2
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 47 deletions.
4 changes: 2 additions & 2 deletions iroh/examples/dht_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use std::str::FromStr;

use clap::Parser;
use iroh::{endpoint::get_remote_node_id, Endpoint, NodeId};
use iroh::{Endpoint, NodeId};
use tracing::warn;
use url::Url;

Expand Down Expand Up @@ -88,7 +88,7 @@ async fn chat_server(args: Args) -> anyhow::Result<()> {
};
tokio::spawn(async move {
let connection = connecting.await?;
let remote_node_id = get_remote_node_id(&connection)?;
let remote_node_id = connection.remote_node_id()?;
println!("got connection from {}", remote_node_id);
// just leave the tasks hanging. this is just an example.
let (mut writer, mut reader) = connection.accept_bi().await?;
Expand Down
2 changes: 1 addition & 1 deletion iroh/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl ProtocolHandler for Echo {
// Wait for the connection to be fully established.
let connection = connecting.await?;
// We can get the remote's node id from the connection.
let node_id = iroh::endpoint::get_remote_node_id(&connection)?;
let node_id = connection.remote_node_id()?;
println!("accepted connection from {node_id}");

// Our protocol is a simple request-response protocol, so we expect the
Expand Down
2 changes: 1 addition & 1 deletion iroh/examples/listen-unreliable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn main() -> anyhow::Result<()> {
};
let alpn = connecting.alpn().await?;
let conn = connecting.await?;
let node_id = iroh::endpoint::get_remote_node_id(&conn)?;
let node_id = conn.remote_node_id()?;
info!(
"new (unreliable) connection from {node_id} with ALPN {} (coming from {})",
String::from_utf8_lossy(&alpn),
Expand Down
2 changes: 1 addition & 1 deletion iroh/examples/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn main() -> anyhow::Result<()> {
};
let alpn = connecting.alpn().await?;
let conn = connecting.await?;
let node_id = iroh::endpoint::get_remote_node_id(&conn)?;
let node_id = conn.remote_node_id()?;
info!(
"new connection from {node_id} with ALPN {} (coming from {})",
String::from_utf8_lossy(&alpn),
Expand Down
4 changes: 2 additions & 2 deletions iroh/examples/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use anyhow::Result;
use clap::Parser;
use futures_lite::future::Boxed as BoxedFuture;
use iroh::{
endpoint::{get_remote_node_id, Connecting},
endpoint::Connecting,
protocol::{ProtocolHandler, Router},
Endpoint, NodeId,
};
Expand Down Expand Up @@ -134,7 +134,7 @@ impl ProtocolHandler for BlobSearch {
// Wait for the connection to be fully established.
let connection = connecting.await?;
// We can get the remote's node id from the connection.
let node_id = get_remote_node_id(&connection)?;
let node_id = connection.remote_node_id()?;
println!("accepted connection from {node_id}");

// Our protocol is a simple request-response protocol, so we expect the
Expand Down
2 changes: 1 addition & 1 deletion iroh/examples/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async fn provide(
}
};
let conn = connecting.await?;
let node_id = iroh::endpoint::get_remote_node_id(&conn)?;
let node_id = conn.remote_node_id()?;
info!(
"new connection from {node_id} with ALPN {} (coming from {})",
String::from_utf8_lossy(TRANSFER_ALPN),
Expand Down
Loading

0 comments on commit 2e61ff2

Please sign in to comment.