Skip to content

Commit

Permalink
feat(iroh): simplify close API call
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 29, 2024
1 parent fc49925 commit aaa2d69
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion iroh/bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl EndpointSelector {
pub async fn close(self) -> Result<()> {
match self {
EndpointSelector::Iroh(endpoint) => {
endpoint.close(0u32.into(), b"").await?;
endpoint.close().await?;
}
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd")))]
EndpointSelector::Quinn(endpoint) => {
Expand Down
2 changes: 1 addition & 1 deletion iroh/examples/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ async fn main() -> anyhow::Result<()> {

// We received the last message: close all connections and allow for the close
// message to be sent.
endpoint.close(0u8.into(), b"bye").await?;
endpoint.close().await?;
Ok(())
}
3 changes: 0 additions & 3 deletions iroh/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ async fn connect_side(addr: NodeAddr) -> Result<()> {
let response = recv.read_to_end(1000).await?;
assert_eq!(&response, b"Hello, world!");

// Close the endpoint (and all its connections) in one:
endpoint.close(0u32.into(), b"bye!").await?;

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion iroh/examples/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async fn fetch(ticket: &str, relay_url: Option<String>) -> anyhow::Result<()> {
// We received the last message: close all connections and allow for the close
// message to be sent.
tokio::time::timeout(Duration::from_secs(3), async move {
let res = endpoint.close(0u8.into(), b"bye").await;
let res = endpoint.close().await;
if res.is_err() {
println!("failed to close connection: {res:#?}");
}
Expand Down
22 changes: 19 additions & 3 deletions iroh/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,22 @@ impl Endpoint {

// # Methods for terminating the endpoint.

/// Closes the QUIC endpoint and the magic socket.
///
/// This will close all open QUIC connections.
///
/// It will then wait for all connections to actually be shutdown, and afterwards close
/// the magic socket. Be aware however that the underlying UDP sockets are only closed
/// on [`Drop`], bearing in mind the [`Endpoint`] is only dropped once all the clones
/// are dropped.
///
/// Returns an error if closing the magic socket failed.
/// TODO: Document error cases.
pub async fn close(&self) -> Result<()> {
self.close_with_code(1u16.into(), b"shutting down").await?;
Ok(())
}

/// Closes the QUIC endpoint and the magic socket.
///
/// This will close all open QUIC connections with the provided error_code and
Expand All @@ -964,7 +980,7 @@ impl Endpoint {
///
/// Returns an error if closing the magic socket failed.
/// TODO: Document error cases.
pub async fn close(&self, error_code: VarInt, reason: &[u8]) -> Result<()> {
pub async fn close_with_code(&self, error_code: VarInt, reason: &[u8]) -> Result<()> {
if self.is_closed() {
return Ok(());
}
Expand Down Expand Up @@ -1603,7 +1619,7 @@ mod tests {

info!("closing endpoint");
// close the endpoint and restart it
endpoint.close(0u32.into(), b"done").await.unwrap();
endpoint.close().await.unwrap();

info!("restarting endpoint");
// now restart it and check the addressing info of the peer
Expand Down Expand Up @@ -1702,7 +1718,7 @@ mod tests {
send.stopped().await.unwrap();
recv.read_to_end(0).await.unwrap();
info!("client finished");
ep.close(0u32.into(), &[]).await.unwrap();
ep.close().await.unwrap();
info!("client closed");
}
.instrument(error_span!("client", %i))
Expand Down
4 changes: 2 additions & 2 deletions iroh/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3266,8 +3266,8 @@ mod tests {
println!("closing endpoints");
let msock1 = m1.endpoint.magic_sock();
let msock2 = m2.endpoint.magic_sock();
m1.endpoint.close(0u32.into(), b"done").await?;
m2.endpoint.close(0u32.into(), b"done").await?;
m1.endpoint.close().await?;
m2.endpoint.close().await?;

assert!(msock1.msock.is_closed());
assert!(msock2.msock.is_closed());
Expand Down
7 changes: 1 addition & 6 deletions iroh/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,10 @@ impl RouterBuilder {

/// Shutdown the different parts of the router concurrently.
async fn shutdown(endpoint: &Endpoint, protocols: Arc<ProtocolMap>) {
let error_code = 1u16;

// We ignore all errors during shutdown.
let _ = tokio::join!(
// Close the endpoint.
// Closing the Endpoint is the equivalent of calling Connection::close on all
// connections: Operations will immediately fail with ConnectionError::LocallyClosed.
// All streams are interrupted, this is not graceful.
endpoint.close(error_code.into(), b"provider terminating"),
endpoint.close(),
// Shutdown protocol handlers.
protocols.shutdown(),
);
Expand Down

0 comments on commit aaa2d69

Please sign in to comment.