@@ -309,11 +309,7 @@ async fn handle_swarm_event(server: &mut P2PServer, event: SwarmEvent<BehaviourE
309309
310310 // Schedule redial if this is a bootnode
311311 if server. bootnode_addrs . contains_key ( & peer_id) {
312- let retry_tx = server. retry_tx . clone ( ) ;
313- tokio:: spawn ( async move {
314- tokio:: time:: sleep ( Duration :: from_secs ( PEER_REDIAL_INTERVAL_SECS ) ) . await ;
315- let _ = retry_tx. send ( RetryMessage :: PeerRedial ( peer_id) ) ;
316- } ) ;
312+ schedule_peer_redial ( server. retry_tx . clone ( ) , peer_id) ;
317313 info ! ( %peer_id, "Scheduled bootnode redial in {}s" , PEER_REDIAL_INTERVAL_SECS ) ;
318314 }
319315 }
@@ -333,11 +329,7 @@ async fn handle_swarm_event(server: &mut P2PServer, event: SwarmEvent<BehaviourE
333329 && server. bootnode_addrs . contains_key ( & pid)
334330 && !server. connected_peers . contains ( & pid)
335331 {
336- let retry_tx = server. retry_tx . clone ( ) ;
337- tokio:: spawn ( async move {
338- tokio:: time:: sleep ( Duration :: from_secs ( PEER_REDIAL_INTERVAL_SECS ) ) . await ;
339- let _ = retry_tx. send ( RetryMessage :: PeerRedial ( pid) ) ;
340- } ) ;
332+ schedule_peer_redial ( server. retry_tx . clone ( ) , pid) ;
341333 info ! ( %pid, "Scheduled bootnode redial after connection error" ) ;
342334 }
343335 }
@@ -402,15 +394,19 @@ async fn handle_peer_redial(server: &mut P2PServer, peer_id: PeerId) {
402394 if let Err ( e) = server. swarm . dial ( addr. clone ( ) ) {
403395 warn ! ( %peer_id, %e, "Failed to redial bootnode, will retry" ) ;
404396 // Schedule another redial attempt
405- let retry_tx = server. retry_tx . clone ( ) ;
406- tokio:: spawn ( async move {
407- tokio:: time:: sleep ( Duration :: from_secs ( PEER_REDIAL_INTERVAL_SECS ) ) . await ;
408- let _ = retry_tx. send ( RetryMessage :: PeerRedial ( peer_id) ) ;
409- } ) ;
397+ schedule_peer_redial ( server. retry_tx . clone ( ) , peer_id) ;
410398 }
411399 }
412400}
413401
402+ /// Schedules a peer redial after the configured delay interval.
403+ pub ( crate ) fn schedule_peer_redial ( retry_tx : mpsc:: UnboundedSender < RetryMessage > , peer_id : PeerId ) {
404+ tokio:: spawn ( async move {
405+ tokio:: time:: sleep ( Duration :: from_secs ( PEER_REDIAL_INTERVAL_SECS ) ) . await ;
406+ let _ = retry_tx. send ( RetryMessage :: PeerRedial ( peer_id) ) ;
407+ } ) ;
408+ }
409+
414410pub struct Bootnode {
415411 ip : IpAddr ,
416412 quic_port : u16 ,
0 commit comments