use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterSettings {
pub node_id: u64,
pub listen: SocketAddr,
pub seed_nodes: Vec<SocketAddr>,
#[serde(default = "default_num_groups")]
pub num_groups: u64,
#[serde(default = "default_replication_factor")]
pub replication_factor: usize,
}
fn default_num_groups() -> u64 {
4
}
fn default_replication_factor() -> usize {
3
}
impl ClusterSettings {
pub fn validate(&self) -> crate::Result<()> {
if self.node_id == 0 {
return Err(crate::Error::Config {
detail: "cluster.node_id must be non-zero".into(),
});
}
if self.seed_nodes.is_empty() {
return Err(crate::Error::Config {
detail: "cluster.seed_nodes must contain at least one address".into(),
});
}
if self.num_groups == 0 {
return Err(crate::Error::Config {
detail: "cluster.num_groups must be at least 1".into(),
});
}
if self.replication_factor == 0 {
return Err(crate::Error::Config {
detail: "cluster.replication_factor must be at least 1".into(),
});
}
Ok(())
}
}