ih_muse/timing.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
//! # Timing Module
//!
//! Provides constants and functions for managing durations and intervals.
//!
//! ## Usage
//! Use predefined constants or functions to calculate intervals dynamically:
//! ```rust
//! use ih_muse::prelude::*;
//! use ih_muse::timing::element_registration_interval;
//! let interval = element_registration_interval(TimestampResolution::Milliseconds);
//! println!("Interval: {:?}", interval);
//! ```
use ih_muse_proto::TimestampResolution;
use std::time::Duration;
/// Default flush interval for recordings.
pub const RECORDING_FLUSH_INTERVAL: Duration = Duration::from_secs(1);
/// Default initialization interval.
pub const INITIALIZATION_INTERVAL: Duration = Duration::from_secs(1);
/// Default cluster monitoring interval.
pub const CLUSTER_MONITOR_INTERVAL: Duration = Duration::from_secs(60);
/// Modifier for element registration interval.
pub const ELEMENT_REGISTRATION_RESOLUTION_MODIFIER: f64 = 0.25;
/// Modifier for metric sending interval.
pub const METRIC_SENDING_RESOLUTION_MODIFIER: f64 = 1.0;
/// Adjusts a `Duration` by a percentage modifier.
pub fn adjust_duration_by_modifier(duration: Duration, modifier: f64) -> Duration {
Duration::from_nanos((duration.as_nanos() as f64 * modifier).max(1.0) as u64)
}
/// Interval for element registration.
pub fn element_registration_interval(finest_resolution: TimestampResolution) -> Duration {
adjust_duration_by_modifier(
finest_resolution.to_duration(),
ELEMENT_REGISTRATION_RESOLUTION_MODIFIER,
)
}
/// Interval for metric sending.
pub fn metric_sending_interval(finest_resolution: TimestampResolution) -> Duration {
adjust_duration_by_modifier(
finest_resolution.to_duration(),
METRIC_SENDING_RESOLUTION_MODIFIER,
)
}