Skip to content

Commit 6229f8a

Browse files
committed
refactor: make the conn manager actually work as intended
1 parent ead60b3 commit 6229f8a

File tree

3 files changed

+478
-39
lines changed

3 files changed

+478
-39
lines changed

iroh-gossip/src/net.rs

+37-39
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use futures_lite::{stream::Stream, StreamExt};
66
use futures_util::future::FutureExt;
77
use genawaiter::sync::{Co, Gen};
88
use iroh_net::{
9-
dialer::{ConnDirection, ConnManager, NewConnection},
9+
dialer::{ConnDirection, ConnInfo, ConnManager},
1010
endpoint::Connection,
1111
key::PublicKey,
1212
AddrInfo, Endpoint, NodeAddr,
@@ -382,9 +382,14 @@ impl Actor {
382382
}
383383
}
384384
}
385-
Some(new_conn) = self.conn_manager.next() => {
385+
Some(res) = self.conn_manager.next() => {
386386
trace!(?i, "tick: conn_manager");
387-
self.handle_new_connection(new_conn).await;
387+
match res {
388+
Ok(conn) => self.handle_new_connection(conn).await,
389+
Err(err) => {
390+
self.handle_in_event(InEvent::PeerDisconnected(err.node_id), Instant::now()).await?;
391+
}
392+
}
388393
}
389394
Some(res) = self.conn_tasks.join_next(), if !self.conn_tasks.is_empty() => {
390395
match res {
@@ -393,7 +398,7 @@ impl Actor {
393398
Ok((node_id, result)) => {
394399
self.conn_manager.remove(&node_id);
395400
self.conn_send_tx.remove(&node_id);
396-
self.handle_in_event(InEvent::PeerDisconnected(node_id), Instant::now()).await ?;
401+
self.handle_in_event(InEvent::PeerDisconnected(node_id), Instant::now()).await?;
397402
match result {
398403
Ok(()) => {
399404
debug!(peer=%node_id.fmt_short(), "connection closed without error");
@@ -430,11 +435,11 @@ impl Actor {
430435
async fn handle_to_actor_msg(&mut self, msg: ToActor, now: Instant) -> anyhow::Result<()> {
431436
trace!("handle to_actor {msg:?}");
432437
match msg {
433-
ToActor::AcceptConn(conn) => match self.conn_manager.accept(conn) {
434-
Err(err) => warn!(?err, "failed to accept connection"),
435-
Ok(None) => {}
436-
Ok(Some(conn)) => self.handle_new_connection(conn).await,
437-
},
438+
ToActor::AcceptConn(conn) => {
439+
if let Err(err) = self.conn_manager.accept(conn) {
440+
warn!(?err, "failed to accept connection");
441+
}
442+
}
438443
ToActor::Join(topic_id, peers, reply) => {
439444
self.handle_in_event(InEvent::Command(topic_id, Command::Join(peers)), now)
440445
.await?;
@@ -498,7 +503,7 @@ impl Actor {
498503
self.conn_manager.remove(&peer_id);
499504
}
500505
} else {
501-
if !self.conn_manager.is_dialing(&peer_id) {
506+
if !self.conn_manager.is_pending(&peer_id) {
502507
debug!(peer = ?peer_id, "dial");
503508
self.conn_manager.dial(peer_id);
504509
}
@@ -545,38 +550,31 @@ impl Actor {
545550
Ok(())
546551
}
547552

548-
async fn handle_new_connection(&mut self, new_conn: NewConnection) {
549-
let NewConnection {
553+
async fn handle_new_connection(&mut self, new_conn: ConnInfo) {
554+
let ConnInfo {
550555
conn,
551-
node_id: peer_id,
556+
node_id,
552557
direction,
553558
} = new_conn;
554-
match conn {
555-
Ok(conn) => {
556-
let (send_tx, send_rx) = mpsc::channel(SEND_QUEUE_CAP);
557-
self.conn_send_tx.insert(peer_id, send_tx.clone());
558-
559-
// Spawn a task for this connection
560-
let pending_sends = self.pending_sends.remove(&peer_id);
561-
let in_event_tx = self.in_event_tx.clone();
562-
debug!(peer=%peer_id.fmt_short(), ?direction, "connection established");
563-
self.conn_tasks.spawn(
564-
connection_loop(
565-
peer_id,
566-
conn,
567-
direction,
568-
send_rx,
569-
in_event_tx,
570-
pending_sends,
571-
)
572-
.map(move |r| (peer_id, r))
573-
.instrument(error_span!("gossip_conn", peer = %peer_id.fmt_short())),
574-
);
575-
}
576-
Err(err) => {
577-
warn!(peer=%peer_id.fmt_short(), "connecting to node failed: {err:?}");
578-
}
579-
}
559+
let (send_tx, send_rx) = mpsc::channel(SEND_QUEUE_CAP);
560+
self.conn_send_tx.insert(node_id, send_tx.clone());
561+
562+
// Spawn a task for this connection
563+
let pending_sends = self.pending_sends.remove(&node_id);
564+
let in_event_tx = self.in_event_tx.clone();
565+
debug!(peer=%node_id.fmt_short(), ?direction, "connection established");
566+
self.conn_tasks.spawn(
567+
connection_loop(
568+
node_id,
569+
conn,
570+
direction,
571+
send_rx,
572+
in_event_tx,
573+
pending_sends,
574+
)
575+
.map(move |r| (node_id, r))
576+
.instrument(error_span!("gossip_conn", peer = %node_id.fmt_short())),
577+
);
580578
}
581579

582580
fn subscribe_all(&mut self) -> broadcast::Receiver<(TopicId, Event)> {

0 commit comments

Comments
 (0)