Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
protocol: fix borrow issues in keepalive/reconnect
Quaylyn Rimer committed 3 months ago
commit b896746f33195c77bf98c6e090b158195c220eff
parent 57988f0f7655595f93ecf91fb2a3f40243ae2989
1 file changed +13 -9
modified crates/radicle-protocol/src/service.rs
@@ -702,8 +702,6 @@ where
        debug!(target: "service", "Init @{}", time.as_millis());
        assert_ne!(time, LocalTime::default());

-
        let nid = self.node_id();
-

        self.clock = time;
        self.started_at = Some(time);
        self.last_online_at = match self.db.gossip().last() {
@@ -1407,6 +1405,8 @@ where

    pub fn disconnected(&mut self, remote: NodeId, link: Link, reason: &DisconnectReason) {
        let since = self.local_time();
+
        let min_reconnect = self.min_reconnection_delta();
+
        let max_reconnect = self.max_reconnection_delta();
        let Some(session) = self.sessions.get_mut(&remote) else {
            // Since we sometimes disconnect the service eagerly, it's not unusual to get a second
            // disconnection event once the transport is dropped.
@@ -1445,7 +1445,7 @@ where
        // Attempt to re-connect to persistent peers.
        if self.config.peer(&remote).is_some() {
            let delay = LocalDuration::from_secs(2u64.saturating_pow(session.attempts() as u32))
-
                .clamp(self.min_reconnection_delta(), self.max_reconnection_delta());
+
                .clamp(min_reconnect, max_reconnect);

            // Nb. We always try to reconnect to persistent peers, even when the error appears
            // to not be transient.
@@ -2549,13 +2549,17 @@ where

    /// Ensure connection health by pinging connected peers.
    fn keep_alive(&mut self, now: &LocalTime) {
-
        let inactive_sessions = self
+
        let keep_alive_delta = self.keep_alive_delta();
+
        let to_ping = self
            .sessions
-
            .connected_mut()
-
            .filter(|(_, session)| *now - session.last_active >= self.keep_alive_delta())
-
            .map(|(_, session)| session);
-
        for session in inactive_sessions {
-
            session.ping(self.clock, &mut self.outbox).ok();
+
            .connected()
+
            .filter(|(_, session)| *now - session.last_active >= keep_alive_delta)
+
            .map(|(nid, _)| *nid)
+
            .collect::<Vec<_>>();
+
        for nid in to_ping {
+
            if let Some(session) = self.sessions.get_mut(&nid) {
+
                session.ping(self.clock, &mut self.outbox).ok();
+
            }
        }
    }