Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: Add connection limits
Alexis Sellier committed 2 years ago
commit 786ce40f9563d9ee8d8946708ed39f9b5a70b87f
parent 5bdc2ad1fba5346005e6e97ae0161d602d890163
4 files changed +52 -1
modified radicle-cli/examples/rad-config.md
@@ -43,6 +43,10 @@ $ rad config
          "fillRate": 1.0,
          "capacity": 64
        }
+
      },
+
      "connection": {
+
        "inbound": 128,
+
        "outbound": 16
      }
    },
    "policy": "block",
modified radicle-httpd/src/api/v1/profile.rs
@@ -105,6 +105,10 @@ mod routes {
                        "fillRate": 1.0,
                        "capacity": 64
                      }
+
                    },
+
                    "connection": {
+
                      "inbound": 128,
+
                      "outbound": 16
                    }
                  },
                  "policy": "block",
modified radicle-node/src/service.rs
@@ -1059,10 +1059,15 @@ where

    /// Inbound connection attempt.
    pub fn accepted(&mut self, addr: Address) -> bool {
-
        // Always accept trusted connections.
+
        // Always accept trusted connections, even if we already reached
+
        // our inbound connection limit.
        if addr.is_trusted() {
            return true;
        }
+
        // Check for inbound connection limit.
+
        if self.sessions.inbound().count() >= self.config.limits.connection.inbound {
+
            return false;
+
        }
        let host: HostName = addr.into();

        if self
@@ -1912,6 +1917,10 @@ where
            error!(target: "service", "Attempted connection to self");
            return false;
        }
+
        if self.sessions.outbound().count() >= self.config.limits.connection.outbound {
+
            error!(target: "service", "Outbound connection limit reached when attempting {nid} ({addr})");
+
            return false;
+
        }
        let persistent = self.config.is_persistent(&nid);
        let time = self.time();

@@ -2345,6 +2354,16 @@ impl Sessions {
            })
    }

+
    /// Iterator over connected inbound peers.
+
    pub fn inbound(&self) -> impl Iterator<Item = (&NodeId, &Session)> + Clone {
+
        self.connected().filter(|(_, s)| s.link.is_inbound())
+
    }
+

+
    /// Iterator over outbound peers.
+
    pub fn outbound(&self) -> impl Iterator<Item = (&NodeId, &Session)> + Clone {
+
        self.connected().filter(|(_, s)| s.link.is_outbound())
+
    }
+

    /// Iterator over mutable fully connected peers.
    pub fn connected_mut(&mut self) -> impl Iterator<Item = (&NodeId, &mut Session)> {
        self.0.iter_mut().filter(move |(_, s)| s.is_connected())
modified radicle/src/node/config.rs
@@ -93,6 +93,9 @@ pub struct Limits {
    /// Rate limitter settings.
    #[serde(default)]
    pub rate: RateLimits,
+
    /// Connection limits.
+
    #[serde(default)]
+
    pub connection: ConnectionLimits,
}

impl Default for Limits {
@@ -104,6 +107,27 @@ impl Default for Limits {
            fetch_concurrency: 1,
            max_open_files: 4096,
            rate: RateLimits::default(),
+
            connection: ConnectionLimits::default(),
+
        }
+
    }
+
}
+

+
/// Connection limits.
+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+
#[serde(rename_all = "camelCase")]
+
pub struct ConnectionLimits {
+
    /// Max inbound connections.
+
    pub inbound: usize,
+
    /// Max outbound connections. Note that this is higher than the *target* number
+
    /// in [`TARGET_OUTBOUND_PEERS`].
+
    pub outbound: usize,
+
}
+

+
impl Default for ConnectionLimits {
+
    fn default() -> Self {
+
        Self {
+
            inbound: 128,
+
            outbound: 16,
        }
    }
}