use std::io;
use std::time::Duration;
use openraft::Config;
use openraft::SnapshotPolicy;
#[derive(Clone, Debug)]
pub struct EzConfig {
pub heartbeat_interval: Duration,
pub snapshot_interval: u64,
}
impl Default for EzConfig {
fn default() -> Self {
Self {
heartbeat_interval: Duration::from_millis(500),
snapshot_interval: 500,
}
}
}
impl EzConfig {
pub(crate) fn to_raft_config(&self) -> Result<Config, io::Error> {
let heartbeat_ms = self.heartbeat_interval.as_millis() as u64;
let config = Config {
heartbeat_interval: heartbeat_ms,
election_timeout_min: heartbeat_ms * 3,
election_timeout_max: heartbeat_ms * 6,
snapshot_policy: SnapshotPolicy::LogsSinceLast(self.snapshot_interval),
max_in_snapshot_log_to_keep: self.snapshot_interval / 5,
replication_lag_threshold: self.snapshot_interval,
..Default::default()
};
config.validate().map_err(io::Error::other)
}
}