Skip to main content

de_mls/app/
scheduler.rs

1//! Configurable steward epoch scheduling.
2//!
3//! Provides a [`StewardScheduler`] trait and a default [`IntervalScheduler`]
4//! implementation that ticks at a fixed interval.
5
6use async_trait::async_trait;
7use std::time::Duration;
8
9/// Default epoch duration (30 seconds).
10pub const DEFAULT_EPOCH_DURATION: Duration = Duration::from_secs(30);
11
12/// Configuration for steward epoch scheduling.
13pub struct StewardSchedulerConfig {
14    /// Interval between steward epochs.
15    pub epoch_interval: Duration,
16}
17
18impl Default for StewardSchedulerConfig {
19    fn default() -> Self {
20        Self {
21            epoch_interval: DEFAULT_EPOCH_DURATION,
22        }
23    }
24}
25
26/// Trait for controlling when steward epochs fire.
27///
28/// Implementations can use timers, thresholds, external triggers, etc.
29#[async_trait]
30pub trait StewardScheduler: Send + Sync {
31    /// Wait until the next epoch should begin.
32    async fn next_tick(&mut self);
33}
34
35/// A simple interval-based scheduler.
36pub struct IntervalScheduler {
37    interval: tokio::time::Interval,
38}
39
40impl IntervalScheduler {
41    pub fn new(config: StewardSchedulerConfig) -> Self {
42        Self {
43            interval: tokio::time::interval(config.epoch_interval),
44        }
45    }
46}
47
48#[async_trait]
49impl StewardScheduler for IntervalScheduler {
50    async fn next_tick(&mut self) {
51        self.interval.tick().await;
52    }
53}