use std::net::IpAddr;
use std::time::Duration;
use gossip::auth::ClusterKey;
use ipnet::IpNet;
use crate::clock::{ClockDrift, NodeId};
use super::{Config, ConfigError};
impl Config {
#[must_use]
pub fn new(port: u16) -> Self {
Config::default().with_port(port)
}
#[must_use]
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}
#[must_use]
pub fn with_listen_addr(mut self, listen_addr: IpAddr) -> Self {
self.listen_addr = listen_addr;
self
}
#[must_use]
pub fn with_net(self, net: IpNet) -> Self {
match self.try_with_net(net) {
Ok(config) => config,
Err(err) => panic!("{err}"),
}
}
pub fn try_with_net(mut self, net: IpNet) -> Result<Self, ConfigError> {
let slot = self
.nets
.iter_mut()
.find(|slot| slot.is_none())
.ok_or(ConfigError::TooManyNets)?;
*slot = Some(net);
Ok(self)
}
#[must_use]
pub fn with_nets(mut self, nets: &[IpNet]) -> Self {
for &net in nets {
self = self.with_net(net);
}
self
}
#[must_use]
pub fn with_remote_interval(mut self, interval: u32) -> Self {
self.remote_interval = interval;
self
}
#[must_use]
pub fn with_remote_fanout(mut self, fanout: usize) -> Self {
self.remote_fanout = fanout;
self
}
#[must_use]
pub fn with_reconcile_interval(mut self, interval: Duration) -> Self {
self.reconcile_interval = interval;
self
}
#[must_use]
pub fn with_bulk_send_rate(mut self, bytes_per_sec: usize) -> Self {
self.bulk_send_rate = Some(bytes_per_sec);
self
}
#[must_use]
pub fn with_recv_buffer_size(mut self, size: usize) -> Self {
self.recv_buffer_size = Some(size);
self
}
#[must_use]
pub fn with_send_buffer_size(mut self, size: usize) -> Self {
self.send_buffer_size = Some(size);
self
}
#[must_use]
pub fn with_cluster_key(mut self, key: ClusterKey) -> Self {
self.cluster_key = Some(key);
self
}
#[must_use]
pub fn with_insecure_no_key(mut self) -> Self {
self.insecure_no_key = true;
self
}
pub(crate) fn check_key_or_insecure_opt_in(&self) {
assert!(
self.cluster_key.is_some() || self.insecure_no_key,
"Config::cluster_key is None: every peer this node ever discovers (any host inside \
the configured nets) would receive the entire dataset, unauthenticated, via paced \
diff dumps. Set Config::with_cluster_key, or opt in explicitly with \
Config::with_insecure_no_key() if the network is a trusted underlay. See README \
\"Security model\"."
);
}
#[must_use]
pub fn with_node_id(mut self, node_id: NodeId) -> Self {
self.node_id = Some(node_id);
self
}
#[must_use]
pub fn with_freshness_window(mut self, window: Duration) -> Self {
self.freshness_window = window;
self
}
#[must_use]
pub fn with_max_peers(mut self, max: usize) -> Self {
self.max_peers = max;
self
}
#[must_use]
pub fn with_max_concurrent_bulk_dumps(mut self, max: usize) -> Self {
self.max_concurrent_bulk_dumps = max;
self
}
#[must_use]
pub fn with_snapshot_interval(mut self, interval: Duration) -> Self {
self.snapshot_interval = interval;
self
}
#[must_use]
pub fn with_max_clock_drift(mut self, drift: ClockDrift) -> Self {
self.max_clock_drift = drift;
self
}
#[must_use]
pub fn with_coalesce_window(mut self, window: Duration) -> Self {
self.coalesce_window = window;
self
}
#[cfg(feature = "encryption")]
#[must_use]
pub fn with_encryption(mut self) -> Self {
self.encrypt = true;
self
}
}