Skip to content

Commit

Permalink
reconnect in newly spawned task
Browse files Browse the repository at this point in the history
  • Loading branch information
blckngm committed Apr 13, 2022
1 parent ac27e29 commit dd025a7
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions crates/p2p-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ impl ServiceHandle for SHandle {
// some just tell users that they need to pay attention
async fn handle_error(&mut self, context: &mut ServiceContext, error: ServiceError) {
log::info!("service error: {:?}", error);
// Reconnect.
if let ServiceError::DialerError { address, error: _ } = error {
tokio::time::sleep(RECONNECT_DURATION).await;
log::info!("dial {}", address);
let _ = context.dial(address, TargetProtocol::All).await;
// Reconnect in a newly spawned task so that we don't block the whole tentacle service.
let control = context.control().clone();
tokio::spawn(async move {
tokio::time::sleep(RECONNECT_DURATION).await;
log::info!("dial {}", address);
let _ = control.dial(address, TargetProtocol::All).await;
});
}
}

Expand All @@ -117,10 +120,12 @@ impl ServiceHandle for SHandle {
.take_while(|x| !matches!(x, Protocol::P2P(_)))
.collect();
if self.dial.contains(&address) {
// Reconnect.
tokio::time::sleep(RECONNECT_DURATION).await;
log::info!("dial {}", address);
let _ = context.dial(address, TargetProtocol::All).await;
let control = context.control().clone();
tokio::spawn(async move {
tokio::time::sleep(RECONNECT_DURATION).await;
log::info!("dial {}", address);
let _ = control.dial(address, TargetProtocol::All).await;
});
}
}
}
Expand All @@ -145,4 +150,4 @@ impl<F: Fn(Arc<SessionContext>, &ServiceAsyncControl, SubstreamReadPart)> Protoc
// Protocol registry: all p2p protocols should be declared here.

pub const P2P_MEM_BLOCK_SYNC_PROTOCOL: ProtocolId = ProtocolId::new(1);
pub const P2P_MEM_BLOCK_SYNC_PROTOCOL_NAME: &str = "/p2p/mem_block_sync_v1";
pub const P2P_MEM_BLOCK_SYNC_PROTOCOL_NAME: &str = "/p2p/mem_block_sync";

0 comments on commit dd025a7

Please sign in to comment.