Skip to main content

o_sfu/config/
user.rs

1use std::time::Duration;
2
3use anyhow::{Result, ensure};
4
5use super::{
6    DeadlineDuration, UserConfig,
7    env::{Env, positive},
8};
9use crate::core::server::room::{
10    DEFAULT_USER_OUTBOUND_QUEUE_BYTE_CAPACITY, DEFAULT_USER_OUTBOUND_QUEUE_CAPACITY,
11};
12
13/// Upper bound for duration settings. Past a day, a deadline derived from
14/// `Instant::now()` stops being operationally meaningful, and a value this
15/// large is far likelier a unit mistake than intent.
16const MAX_DURATION_SECS: u64 = 24 * 60 * 60;
17
18impl UserConfig {
19    pub(super) fn from_env(env: &Env<'_>) -> Result<Self> {
20        Ok(Self {
21            room_size: env.var("ROOM_SIZE").check(positive).default(100)?,
22            timeout: env
23                .var("USER_TIMEOUT_MS")
24                .default(DeadlineDuration::from_millis(10_000)?)?,
25            ping_interval: env
26                .var("PING_INTERVAL_MS")
27                .default(DeadlineDuration::from_millis(60_000)?)?,
28            outbound_queue_capacity: env
29                .var("USER_OUTBOUND_QUEUE_CAPACITY")
30                .check(positive)
31                .default(DEFAULT_USER_OUTBOUND_QUEUE_CAPACITY)?,
32            outbound_queue_byte_capacity: env
33                .var("USER_OUTBOUND_QUEUE_BYTE_CAPACITY")
34                .check(positive)
35                .default(DEFAULT_USER_OUTBOUND_QUEUE_BYTE_CAPACITY)?,
36            room_reservation_ttl: env
37                .var("ROOM_RESERVATION_TTL")
38                .check(bounded_duration)
39                .default(Duration::from_mins(1))?,
40            departure_grace: env
41                .var("ROOM_DEPARTURE_GRACE")
42                .check(bounded_duration)
43                .default(Duration::from_mins(1))?,
44        })
45    }
46}
47
48fn bounded_duration(key: &'static str, value: Duration) -> Result<Duration> {
49    ensure!(
50        value.as_secs() <= MAX_DURATION_SECS,
51        "{key} must not exceed {MAX_DURATION_SECS} seconds"
52    );
53    Ok(value)
54}