o_sfu/config/deadline.rs
1use std::time::Duration;
2
3use anyhow::{Result, ensure};
4
5/// Positive runtime timeout or interval of at most 24 hours.
6///
7/// The operational limit rejects unit mistakes and unbounded durations before
8/// they reach runtime deadline arithmetic.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct DeadlineDuration(Duration);
11
12impl DeadlineDuration {
13 /// Constructs a duration from integer milliseconds.
14 ///
15 /// # Errors
16 /// Returns [`anyhow::Error`] unless `milliseconds` is in `1..=86_400_000`.
17 pub fn from_millis(milliseconds: u64) -> Result<Self> {
18 ensure!(
19 (1..=86_400_000).contains(&milliseconds),
20 "must be between 1 and 86400000 milliseconds"
21 );
22 Ok(Self(Duration::from_millis(milliseconds)))
23 }
24
25 /// Returns the validated duration for runtime timers.
26 #[must_use]
27 pub const fn as_duration(self) -> Duration {
28 self.0
29 }
30}