Skip to content

Commit

Permalink
Refactor dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
hmzakhalid committed Jan 6, 2025
1 parent 05dee3b commit d207422
Showing 1 changed file with 28 additions and 43 deletions.
71 changes: 28 additions & 43 deletions packages/ciphernode/net/src/dialer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ impl Dialer {
.context("no IPv4 addresses found")?;
Ok(addr.ip().to_string())
}

fn handle_connection_error(
&mut self,
conn: PendingConnection,
error: Arc<DialError>,
ctx: &mut Context<Self>,
) {
warn!("Connection error for {}: {}", conn.addr, error);
if !matches!(error.as_ref(), DialError::NoAddresses { .. }) {
if conn.attempt < BACKOFF_MAX_RETRIES {
let mut dialer = self.clone();
ctx.spawn(
async move {
dialer
.attempt_dial(conn.addr, conn.attempt + 1, conn.delay_ms * 2)
.await;
}
.into_actor(self),
);
} else {
warn!("Permanent failure for {}: {}", conn.addr, error);
}
} else {
warn!("Permanent failure for {}: {}", conn.addr, error);
}
}
}

impl Actor for Dialer {
Expand All @@ -177,7 +203,6 @@ impl Handler<NetworkPeerEvent> for Dialer {
type Result = ();

fn handle(&mut self, msg: NetworkPeerEvent, ctx: &mut Context<Self>) {
let mut dialer = self.clone();
match msg {
NetworkPeerEvent::ConnectionEstablished { connection_id } => {
if let Some(conn) = self.pending_connection.remove(&connection_id) {
Expand All @@ -189,55 +214,15 @@ impl Handler<NetworkPeerEvent> for Dialer {
error,
} => {
if let Some(conn) = self.pending_connection.remove(&connection_id) {
warn!("DialError for {}: {}", conn.addr, error);
if !matches!(error.as_ref(), DialError::NoAddresses { .. }) {
if conn.attempt < BACKOFF_MAX_RETRIES {
ctx.spawn(
async move {
dialer
.attempt_dial(
conn.addr,
conn.attempt + 1,
conn.delay_ms * 2,
)
.await;
}
.into_actor(self),
);
} else {
warn!("Permanent failure for {}: {}", conn.addr, error);
}
} else {
warn!("Permanent failure for {}: {}", conn.addr, error);
}
self.handle_connection_error(conn, error, ctx);
}
}
NetworkPeerEvent::OutgoingConnectionError {
connection_id,
error,
} => {
if let Some(conn) = self.pending_connection.remove(&connection_id) {
warn!("OutgoingConnectionError for {}: {}", conn.addr, error);
if !matches!(error.as_ref(), DialError::NoAddresses { .. }) {
if conn.attempt < BACKOFF_MAX_RETRIES {
ctx.spawn(
async move {
dialer
.attempt_dial(
conn.addr,
conn.attempt + 1,
conn.delay_ms * 2,
)
.await;
}
.into_actor(self),
);
} else {
warn!("Permanent failure for {}: {}", conn.addr, error);
}
} else {
warn!("Permanent failure for {}: {}", conn.addr, error);
}
self.handle_connection_error(conn, error, ctx);
}
}
_ => {}
Expand Down

0 comments on commit d207422

Please sign in to comment.