Skip to main content

o_sfu_core/options/
routing.rs

1use std::num::{NonZeroU64, NonZeroUsize};
2
3/// Same-room router cap and packet-loop health threshold.
4///
5/// A room's first join selects the first healthy worker in its cyclic search
6/// order or the least-delayed worker when none qualify. With more than one local
7/// router allowed, later joins prefer the least-delayed healthy assigned worker.
8/// If none qualifies, another router may be allocated on an unused healthy
9/// worker up to `max_local_routers` and the worker count. Missing delay samples
10/// and values at or above `packet_loop_delay_threshold_ms` are unhealthy. If no
11/// healthy placement exists, the least-delayed assigned worker is reused.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct RoomWorkerPolicy {
14    max_local_routers: usize,
15    packet_loop_delay_threshold_ms: u64,
16}
17
18impl RoomWorkerPolicy {
19    pub const DEFAULT_PACKET_LOOP_DELAY_THRESHOLD_MS: u64 = 20;
20
21    #[must_use]
22    pub const fn strict_single_router() -> Self {
23        Self {
24            max_local_routers: 1,
25            packet_loop_delay_threshold_ms: Self::DEFAULT_PACKET_LOOP_DELAY_THRESHOLD_MS,
26        }
27    }
28
29    #[must_use]
30    pub const fn new(
31        max_local_routers: NonZeroUsize,
32        packet_loop_delay_threshold_ms: NonZeroU64,
33    ) -> Self {
34        Self {
35            max_local_routers: max_local_routers.get(),
36            packet_loop_delay_threshold_ms: packet_loop_delay_threshold_ms.get(),
37        }
38    }
39
40    #[must_use]
41    pub const fn max_local_routers(self) -> usize {
42        self.max_local_routers
43    }
44
45    #[must_use]
46    pub const fn packet_loop_delay_threshold_ms(self) -> u64 {
47        self.packet_loop_delay_threshold_ms
48    }
49}
50
51impl Default for RoomWorkerPolicy {
52    fn default() -> Self {
53        Self::strict_single_router()
54    }
55}