use std::time::Duration;
#[cfg(feature = "tokio-runtime")]
use tokio_util::sync::CancellationToken;
const DEFAULT_SWEEP_INTERVAL: Duration = Duration::from_secs(10);
const DEFAULT_EVICTION_THRESHOLD: u32 = 30;
#[derive(Clone)]
pub struct SweepConfig {
pub interval: Duration,
pub eviction_threshold: u32,
}
impl Default for SweepConfig {
fn default() -> Self {
Self {
interval: DEFAULT_SWEEP_INTERVAL,
eviction_threshold: DEFAULT_EVICTION_THRESHOLD,
}
}
}
impl SweepConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_interval(mut self, interval: Duration) -> Self {
self.interval = interval;
self
}
pub fn with_eviction_threshold(mut self, threshold: u32) -> Self {
self.eviction_threshold = threshold;
self
}
}
#[cfg(feature = "tokio-runtime")]
pub async fn run<F>(config: SweepConfig, cancel: CancellationToken, mut sweep_fn: F)
where
F: FnMut(u32) -> usize,
{
use tokio::time::MissedTickBehavior;
log::info!(
"Starting stale-series sweeper, interval={}s, eviction_threshold={}",
config.interval.as_secs(),
config.eviction_threshold
);
let mut interval = tokio::time::interval(config.interval);
interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
interval.tick().await;
loop {
tokio::select! {
_ = interval.tick() => {}
_ = cancel.cancelled() => {
log::info!("Stale-series sweeper shutting down");
return;
}
}
let evicted = sweep_fn(config.eviction_threshold);
if evicted > 0 {
log::debug!("Evicted {evicted} stale metric series");
}
}
}
#[cfg(feature = "monoio")]
pub async fn run_monoio<F>(config: SweepConfig, cancel: CancellationToken, mut sweep_fn: F)
where
F: FnMut(u32) -> usize,
{
use monoio::time::MissedTickBehavior;
log::info!(
"Starting monoio stale-series sweeper, interval={}s, eviction_threshold={}",
config.interval.as_secs(),
config.eviction_threshold
);
let mut interval = monoio::time::interval(config.interval);
interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
interval.tick().await;
loop {
monoio::select! {
_ = interval.tick() => {}
_ = cancel.cancelled() => {
log::info!("monoio stale-series sweeper shutting down");
return;
}
}
let evicted = sweep_fn(config.eviction_threshold);
if evicted > 0 {
log::debug!("Evicted {evicted} stale metric series");
}
}
}
#[cfg(feature = "compio")]
pub async fn run_compio<F>(
config: SweepConfig,
cancel: impl std::future::Future<Output = ()>,
mut sweep_fn: F,
) where
F: FnMut(u32) -> usize,
{
use futures_util::{FutureExt as _, select};
log::info!(
"Starting compio stale-series sweeper, interval={}s, eviction_threshold={}",
config.interval.as_secs(),
config.eviction_threshold
);
let mut interval = compio::time::interval(config.interval);
interval.tick().await;
let cancel = cancel.fuse();
let mut cancel = std::pin::pin!(cancel);
loop {
let tick = interval.tick();
let tick = std::pin::pin!(tick);
select! {
_ = tick.fuse() => {},
_ = cancel.as_mut() => {
log::info!("compio stale-series sweeper shutting down");
return;
}
}
let evicted = sweep_fn(config.eviction_threshold);
if evicted > 0 {
log::debug!("Evicted {evicted} stale metric series");
}
}
}