use std::ops::{Deref, DerefMut};
use std::time::Duration;
use crate::TrypemaError;
pub(crate) const EPOCH_CHANGE_INTERVAL: Duration = Duration::from_secs(15);
pub(crate) fn committer_inactivity_sleep(sync_interval: Duration) -> Duration {
let min_ms = (EPOCH_CHANGE_INTERVAL * 2).as_millis() as u64;
let multiplier: u64 = 10;
Duration::from_millis(min_ms.max(sync_interval.as_millis() as u64 * multiplier)) / 2
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) enum RedisRateLimiterSignal {
Flush,
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct SyncIntervalMs(u64);
impl Default for SyncIntervalMs {
fn default() -> Self {
Self(10)
}
}
impl Deref for SyncIntervalMs {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for SyncIntervalMs {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl TryFrom<u64> for SyncIntervalMs {
type Error = TrypemaError;
fn try_from(value: u64) -> Result<Self, Self::Error> {
if value == 0 {
Err(TrypemaError::InvalidRateGroupSizeMs(
"Sync interval must be greater than 0".to_string(),
))
} else {
Ok(Self(value))
}
}
}