style: apply cargo fmt across the crate (A20)
The repo never enforced rustfmt, so formatting had drifted broadly. This is a single mechanical `cargo fmt` pass over the whole crate (no behavioral change; lib suite green, 493 passed). Going forward fmt should be enforced (planned CI fmt --check step). Part of the 0.6.1 hygiene pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+71
-26
@@ -1,16 +1,16 @@
|
||||
use crate::network::{NetworkTransport, NetError, ConnEvent};
|
||||
use iroh::{Endpoint, EndpointId};
|
||||
use iroh::endpoint::{Connection, ConnectionError, VarInt};
|
||||
use crate::network::{ConnEvent, NetError, NetworkTransport};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use iroh::endpoint::{Connection, ConnectionError, VarInt};
|
||||
use iroh::{Endpoint, EndpointId};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::{Arc, Mutex as StdMutex};
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
use std::sync::{Arc, Mutex as StdMutex};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::time::Duration;
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::protocol::{AUDIO_ALPN, FILES_ALPN};
|
||||
use crate::files::{AttachmentId, ChatAttachment};
|
||||
use crate::protocol::{AUDIO_ALPN, FILES_ALPN};
|
||||
|
||||
/// Per-peer datagram send queue depth. Audio is real-time, so a backlog is
|
||||
/// useless latency — keep it shallow and drop the oldest frame when full.
|
||||
@@ -111,7 +111,13 @@ impl Shared {
|
||||
let shared = self.clone();
|
||||
let supervisor = tokio::spawn(supervise(shared, peer_id, inbound_rx));
|
||||
let inbound_tx_ret = inbound_tx.clone();
|
||||
peers.insert(peer_id, PeerHandle { supervisor, inbound_tx });
|
||||
peers.insert(
|
||||
peer_id,
|
||||
PeerHandle {
|
||||
supervisor,
|
||||
inbound_tx,
|
||||
},
|
||||
);
|
||||
crate::log_msg(&format!("Transport: supervising peer {:?}", peer_id));
|
||||
inbound_tx_ret
|
||||
}
|
||||
@@ -123,7 +129,10 @@ impl Shared {
|
||||
self.addrs.lock().unwrap().remove(&peer_id);
|
||||
if let Some(handle) = self.peers.lock().await.remove(&peer_id) {
|
||||
handle.supervisor.abort();
|
||||
crate::log_msg(&format!("Transport: stopped supervising peer {:?}", peer_id));
|
||||
crate::log_msg(&format!(
|
||||
"Transport: stopped supervising peer {:?}",
|
||||
peer_id
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,12 +217,15 @@ async fn supervise(
|
||||
let mut backoff = INITIAL_BACKOFF;
|
||||
|
||||
// Show "connecting" until the first link is actually up.
|
||||
let _ = shared.conn_events_tx.try_send(ConnEvent::Connecting(peer_id));
|
||||
let _ = shared
|
||||
.conn_events_tx
|
||||
.try_send(ConnEvent::Connecting(peer_id));
|
||||
|
||||
let mut conn = match obtain_conn(&shared, peer_id, is_dialer, &mut inbound_rx, &mut backoff).await {
|
||||
Some(conn) => conn,
|
||||
None => return, // retired before we ever connected
|
||||
};
|
||||
let mut conn =
|
||||
match obtain_conn(&shared, peer_id, is_dialer, &mut inbound_rx, &mut backoff).await {
|
||||
Some(conn) => conn,
|
||||
None => return, // retired before we ever connected
|
||||
};
|
||||
|
||||
loop {
|
||||
// A healthy link resets the dialer's backoff for the next outage.
|
||||
@@ -223,8 +235,14 @@ async fn supervise(
|
||||
shared.senders.lock().unwrap().insert(peer_id, send_tx);
|
||||
// Publish the live connection so an intentional leave can close it with
|
||||
// the goodbye code.
|
||||
shared.live_conns.lock().unwrap().insert(peer_id, conn.clone());
|
||||
let _ = shared.conn_events_tx.try_send(ConnEvent::Connected(peer_id));
|
||||
shared
|
||||
.live_conns
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(peer_id, conn.clone());
|
||||
let _ = shared
|
||||
.conn_events_tx
|
||||
.try_send(ConnEvent::Connected(peer_id));
|
||||
crate::log_msg(&format!("Transport: peer {:?} link up", peer_id));
|
||||
|
||||
// Run until the link dies, a replacement arrives, or we're retired. The
|
||||
@@ -272,21 +290,36 @@ async fn supervise(
|
||||
match wake {
|
||||
Wake::Shutdown => return,
|
||||
Wake::Replacement(new_conn) => {
|
||||
crate::log_msg(&format!("Transport: peer {:?} replaced with new inbound link", peer_id));
|
||||
let _ = shared.conn_events_tx.try_send(ConnEvent::Connecting(peer_id));
|
||||
crate::log_msg(&format!(
|
||||
"Transport: peer {:?} replaced with new inbound link",
|
||||
peer_id
|
||||
));
|
||||
let _ = shared
|
||||
.conn_events_tx
|
||||
.try_send(ConnEvent::Connecting(peer_id));
|
||||
conn = new_conn;
|
||||
}
|
||||
Wake::Closed(reason) => {
|
||||
// A graceful application close means the peer left on purpose —
|
||||
// don't reconnect; tell the core to evict it now.
|
||||
if is_graceful_leave(&reason) {
|
||||
crate::log_msg(&format!("Transport: peer {:?} left gracefully ({:?})", peer_id, reason));
|
||||
crate::log_msg(&format!(
|
||||
"Transport: peer {:?} left gracefully ({:?})",
|
||||
peer_id, reason
|
||||
));
|
||||
let _ = shared.conn_events_tx.try_send(ConnEvent::Left(peer_id));
|
||||
return;
|
||||
}
|
||||
crate::log_msg(&format!("Transport: peer {:?} link dropped; reconnecting", peer_id));
|
||||
let _ = shared.conn_events_tx.try_send(ConnEvent::Connecting(peer_id));
|
||||
conn = match obtain_conn(&shared, peer_id, is_dialer, &mut inbound_rx, &mut backoff).await {
|
||||
crate::log_msg(&format!(
|
||||
"Transport: peer {:?} link dropped; reconnecting",
|
||||
peer_id
|
||||
));
|
||||
let _ = shared
|
||||
.conn_events_tx
|
||||
.try_send(ConnEvent::Connecting(peer_id));
|
||||
conn = match obtain_conn(&shared, peer_id, is_dialer, &mut inbound_rx, &mut backoff)
|
||||
.await
|
||||
{
|
||||
Some(conn) => conn,
|
||||
None => return, // retired while reconnecting
|
||||
};
|
||||
@@ -405,7 +438,10 @@ impl iroh::protocol::ProtocolHandler for AudioRouter {
|
||||
// only happens if links are churning, and the supervisor gets the next one.
|
||||
let inbound_tx = shared.ensure_supervisor(peer_id).await;
|
||||
if inbound_tx.try_send(connection).is_err() {
|
||||
crate::log_msg(&format!("Transport: dropped inbound link from {:?} (queue full)", peer_id));
|
||||
crate::log_msg(&format!(
|
||||
"Transport: dropped inbound link from {:?} (queue full)",
|
||||
peer_id
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -545,7 +581,14 @@ impl IrohTransport {
|
||||
/// all supervisors so none linger redialing the about-to-close endpoint.
|
||||
/// Call this before shutting the router down.
|
||||
pub async fn leave(&self) {
|
||||
let conns: Vec<Connection> = self.shared.live_conns.lock().unwrap().drain().map(|(_, c)| c).collect();
|
||||
let conns: Vec<Connection> = self
|
||||
.shared
|
||||
.live_conns
|
||||
.lock()
|
||||
.unwrap()
|
||||
.drain()
|
||||
.map(|(_, c)| c)
|
||||
.collect();
|
||||
for conn in &conns {
|
||||
conn.close(VarInt::from_u32(GOODBYE_CODE), b"leave");
|
||||
}
|
||||
@@ -639,7 +682,9 @@ impl IrohTransport {
|
||||
.map_err(|_| NetError::Other("file fetch: read timed out".to_string()))?
|
||||
.map_err(|e| NetError::Other(format!("file fetch: read failed: {e}")))?;
|
||||
if bytes.is_empty() {
|
||||
return Err(NetError::Other("file fetch: sender no longer has the file".to_string()));
|
||||
return Err(NetError::Other(
|
||||
"file fetch: sender no longer has the file".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user