Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle/config/node: Granular Default Values
✓ CI success Lorenz Leutgeb committed 10 months ago
commit db4ced2a8bc32bf68bd830390a8f9e5bae124121
parent 0e48723b419be95340a5d9858d76963e8e97137b
1 passed (1 total) View logs
1 file changed +124 -17
modified crates/radicle/src/node/config.rs
@@ -94,44 +94,85 @@ impl Network {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Limits {
    /// Number of routing table entries before we start pruning.
+
    #[serde(default = "default_limit_routing_max_size")]
    pub routing_max_size: usize,
+

    /// How long to keep a routing table entry before being pruned.
-
    #[serde(with = "crate::serde_ext::localtime::duration")]
+
    #[serde(
+
        default = "default_limit_routing_max_age",
+
        with = "crate::serde_ext::localtime::duration"
+
    )]
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "crate::schemars_ext::localtime::LocalDuration")
    )]
    pub routing_max_age: LocalDuration,
+

    /// How long to keep a gossip message entry before pruning it.
-
    #[serde(with = "crate::serde_ext::localtime::duration")]
+
    #[serde(
+
        default = "default_limit_gossip_max_age",
+
        with = "crate::serde_ext::localtime::duration"
+
    )]
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "crate::schemars_ext::localtime::LocalDuration")
    )]
    pub gossip_max_age: LocalDuration,
+

    /// Maximum number of concurrent fetches per peer connection.
+
    #[serde(default = "default_limit_fetch_concurrency")]
    pub fetch_concurrency: usize,
+

    /// Maximum number of open files.
+
    #[serde(default = "default_limit_max_open_files")]
    pub max_open_files: usize,
+

    /// Rate limitter settings.
    #[serde(default)]
    pub rate: RateLimits,
+

    /// Connection limits.
    #[serde(default)]
    pub connection: ConnectionLimits,
+

    /// Channel limits.
    #[serde(default)]
    pub fetch_pack_receive: FetchPackSizeLimit,
}

+
#[inline]
+
const fn default_limit_routing_max_size() -> usize {
+
    1000
+
}
+

+
#[inline]
+
const fn default_limit_routing_max_age() -> LocalDuration {
+
    LocalDuration::from_mins(7 * 24 * 60) // One week
+
}
+

+
#[inline]
+
const fn default_limit_gossip_max_age() -> LocalDuration {
+
    LocalDuration::from_mins(2 * 7 * 24 * 60) // Two weeks
+
}
+

+
#[inline]
+
const fn default_limit_fetch_concurrency() -> usize {
+
    1
+
}
+

+
#[inline]
+
const fn default_limit_max_open_files() -> usize {
+
    4096
+
}
+

impl Default for Limits {
    fn default() -> Self {
        Self {
-
            routing_max_size: 1000,
-
            routing_max_age: LocalDuration::from_mins(7 * 24 * 60), // One week
-
            gossip_max_age: LocalDuration::from_mins(2 * 7 * 24 * 60), // Two weeks
-
            fetch_concurrency: 1,
-
            max_open_files: 4096,
+
            routing_max_size: default_limit_routing_max_size(),
+
            routing_max_age: default_limit_routing_max_age(),
+
            gossip_max_age: default_limit_gossip_max_age(),
+
            fetch_concurrency: default_limit_fetch_concurrency(),
+
            max_open_files: default_limit_max_open_files(),
            rate: RateLimits::default(),
            connection: ConnectionLimits::default(),
            fetch_pack_receive: FetchPackSizeLimit::default(),
@@ -235,16 +276,28 @@ impl Default for FetchPackSizeLimit {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct ConnectionLimits {
    /// Max inbound connections.
+
    #[serde(default = "default_limit_connections_inbound")]
    pub inbound: usize,
    /// Max outbound connections. Note that this can be higher than the *target* number.
+
    #[serde(default = "default_limit_connections_outbound")]
    pub outbound: usize,
}

+
#[inline]
+
fn default_limit_connections_inbound() -> usize {
+
    128
+
}
+

+
#[inline]
+
fn default_limit_connections_outbound() -> usize {
+
    16
+
}
+

impl Default for ConnectionLimits {
    fn default() -> Self {
        Self {
-
            inbound: 128,
-
            outbound: 16,
+
            inbound: default_limit_connections_inbound(),
+
            outbound: default_limit_connections_outbound(),
        }
    }
}
@@ -263,21 +316,33 @@ pub struct RateLimit {
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct RateLimits {
+
    #[serde(default = "default_limit_rate_inbound")]
    pub inbound: RateLimit,
+
    #[serde(default = "default_limit_rate_outbound")]
    pub outbound: RateLimit,
}

+
#[inline]
+
const fn default_limit_rate_inbound() -> RateLimit {
+
    RateLimit {
+
        fill_rate: 5.0,
+
        capacity: 1024,
+
    }
+
}
+

+
#[inline]
+
const fn default_limit_rate_outbound() -> RateLimit {
+
    RateLimit {
+
        fill_rate: 10.0,
+
        capacity: 2048,
+
    }
+
}
+

impl Default for RateLimits {
    fn default() -> Self {
        Self {
-
            inbound: RateLimit {
-
                fill_rate: 5.0,
-
                capacity: 1024,
-
            },
-
            outbound: RateLimit {
-
                fill_rate: 10.0,
-
                capacity: 2048,
-
            },
+
            inbound: default_limit_rate_inbound(),
+
            outbound: default_limit_rate_outbound(),
        }
    }
}
@@ -552,3 +617,45 @@ mod defaults {
        log::Level::Info
    }
}
+

+
#[cfg(test)]
+
#[allow(clippy::unwrap_used)]
+
mod test {
+
    #[test]
+
    fn partial() {
+
        use super::Config;
+
        use serde_json::json;
+

+
        let config: Config = serde_json::from_value(json!({
+
            "alias": "example",
+
            "limits": {
+
                "connection": {
+
                    "inbound": 1337,
+
                },
+
            },
+
        }
+
        ))
+
        .unwrap();
+
        assert_eq!(config.limits.connection.inbound, 1337);
+
        assert_eq!(
+
            config.limits.connection.outbound,
+
            super::default_limit_connections_outbound()
+
        );
+

+
        let config: Config = serde_json::from_value(json!({
+
            "alias": "example",
+
            "limits": {
+
                "connection": {
+
                    "outbound": 1337,
+
                },
+
            },
+
        }
+
        ))
+
        .unwrap();
+
        assert_eq!(
+
            config.limits.connection.inbound,
+
            super::default_limit_connections_inbound()
+
        );
+
        assert_eq!(config.limits.connection.outbound, 1337);
+
    }
+
}