/// Waits for the node to get at least one connection.
/// This is useful for testing and is not typically be needed in application code.
Waits for the node to get at least one connection.
This is useful for testing and is not typically be needed in application code.
Waits for the node to get at least one connection . This is useful for testing and is not typically be needed in application code .
pub async fn wait_for_connectivity(&mut self, timeout: Duration) -> Result<(), ConnectivityError> {
        let mut connectivity_events = self.get_event_subscription();
        let status = self.get_connectivity_status().await?;
        if status.is_online() {
            return Ok(());
        }
        let start = Instant::now();
        let mut remaining = timeout;
        let mut last_known_peer_count = status.num_connected_nodes();
        loop {
            debug!(target: LOG_TARGET, "Waiting for connectivity event");
            let recv_result = time::timeout(remaining, connectivity_events.next())
                .await
                .map_err(|_| ConnectivityError::OnlineWaitTimeout(last_known_peer_count))?
                .ok_or(ConnectivityError::ConnectivityEventStreamClosed)?;
            remaining = timeout
                .checked_sub(start.elapsed())
                .ok_or(ConnectivityError::OnlineWaitTimeout(last_known_peer_count))?;
            match recv_result {
                Ok(event) => match &*event {
                    ConnectivityEvent::ConnectivityStateOnline(_) => {
                        info!(target: LOG_TARGET, "Connectivity is ONLINE.");
                        break Ok(());
                    },
                    ConnectivityEvent::ConnectivityStateDegraded(n) => {
                        warn!(target: LOG_TARGET, "Connectivity is DEGRADED ({} peer(s))", n);
                        last_known_peer_count = *n;
                    },
                    ConnectivityEvent::ConnectivityStateOffline => {
                        warn!(
                            target: LOG_TARGET,
                            "Connectivity is OFFLINE. Waiting for connections..."
                        );
                        last_known_peer_count = 0;
                    },
                    event => {
                        debug!(
                            target: LOG_TARGET,
                            "Received event while waiting for connectivity: {:?}", event
                        );
                    },
                },
                Err(broadcast::RecvError::Closed) => {
                    error!(
                        target: LOG_TARGET,
                        "Connectivity event stream closed unexpectedly. System may be shutting down."
                    );
                    break Err(ConnectivityError::ConnectivityEventStreamClosed);
                },
                Err(broadcast::RecvError::Lagged(n)) => {
                    warn!(target: LOG_TARGET, "Lagging behind on {} connectivity event(s)", n);
                    // We lagged, so could have missed the state change. Check it explicitly.
                    let status = self.get_connectivity_status().await?;
                    if status.is_online() {
                        break Ok(());
                    }
                },
            }
        }
    }
pub async fn wait_for_connectivity ( & mut self , timeout : Duration ) -> Result < ( ) , ConnectivityError > { let mut connectivity_events = self . get_event_subscription ( ) ; let status = self . get_connectivity_status ( ) . await ? ; if status . is_online ( ) { return Ok ( ( ) ) ; } let start = Instant :: now ( ) ; let mut remaining = timeout ; let mut last_known_peer_count = status . num_connected_nodes ( ) ; loop { debug ! ( target LOG_TARGET " " ) ; let recv_result = time :: timeout ( remaining , connectivity_events . next ( ) ) . await . map_err ( | _ | ConnectivityError :: OnlineWaitTimeout ( last_known_peer_count ) ) ? . ok_or ( ConnectivityError :: ConnectivityEventStreamClosed ) ? ; remaining = timeout . checked_sub ( start . elapsed ( ) ) . ok_or ( ConnectivityError :: OnlineWaitTimeout ( last_known_peer_count ) ) ? ; match recv_result { Ok ( event ) => match & * event { ConnectivityEvent :: ConnectivityStateOnline ( _ ) => { info ! ( target LOG_TARGET " " ) ; break Ok ( ( ) ) ; } , ConnectivityEvent :: ConnectivityStateDegraded ( n ) => { warn ! ( target LOG_TARGET " " n ) ; last_known_peer_count = * n ; } , ConnectivityEvent :: ConnectivityStateOffline => { warn ! ( target LOG_TARGET " " ) ; last_known_peer_count = 0 ; } , event => { debug ! ( target LOG_TARGET " " event ) ; } , } , Err ( broadcast :: RecvError :: Closed ) => { error ! ( target LOG_TARGET " " ) ; break Err ( ConnectivityError :: ConnectivityEventStreamClosed ) ; } , Err ( broadcast :: RecvError :: Lagged ( n ) ) => { warn ! ( target LOG_TARGET " " n ) ; // We lagged, so could have missed the state change. Check it explicitly. let status = self . get_connectivity_status ( ) . await ? ; if status . is_online ( ) { break Ok ( ( ) ) ; } } , } } }
Waits for the node to get at least one connection.
Waits for the node to get at least one connection .
&mut self
timeout
Duration