Skip to main content

buffer_core/shm/
config.rs

1use std::path::PathBuf;
2
3use core_types::{ErrorCode, ErrorDomain, RtError};
4
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub struct ShmRateLimitConfig {
7    pub max_bytes_per_sec: u64,
8    pub burst_bytes: u64,
9}
10
11impl ShmRateLimitConfig {
12    pub fn new(max_bytes_per_sec: u64, burst_bytes: u64) -> Self {
13        Self {
14            max_bytes_per_sec,
15            burst_bytes,
16        }
17    }
18}
19
20#[derive(Clone, Debug, Eq, PartialEq)]
21pub struct ShmTransportConfig {
22    pub namespace: String,
23    pub directory: PathBuf,
24    pub segment_payload_bytes: usize,
25    pub segment_count: usize,
26    pub rate_limit: Option<ShmRateLimitConfig>,
27}
28
29impl Default for ShmTransportConfig {
30    fn default() -> Self {
31        Self {
32            namespace: "robotrt-default".to_string(),
33            directory: std::env::temp_dir().join("robotrt-shm"),
34            segment_payload_bytes: 8 * 1024 * 1024,
35            segment_count: 8,
36            rate_limit: None,
37        }
38    }
39}
40
41pub(super) fn validate_cfg(cfg: &ShmTransportConfig) -> Result<(), RtError> {
42    if cfg.segment_payload_bytes == 0 {
43        return Err(RtError::new(
44            ErrorCode::InvalidState,
45            ErrorDomain::Core,
46            false,
47            "segment_payload_bytes must be > 0",
48        ));
49    }
50    if cfg.segment_count == 0 {
51        return Err(RtError::new(
52            ErrorCode::InvalidState,
53            ErrorDomain::Core,
54            false,
55            "segment_count must be > 0",
56        ));
57    }
58    if cfg.namespace.is_empty() {
59        return Err(RtError::new(
60            ErrorCode::InvalidState,
61            ErrorDomain::Core,
62            false,
63            "namespace must not be empty",
64        ));
65    }
66
67    if let Some(limit) = &cfg.rate_limit
68        && (limit.max_bytes_per_sec == 0 || limit.burst_bytes == 0)
69    {
70        return Err(RtError::new(
71            ErrorCode::InvalidState,
72            ErrorDomain::Core,
73            false,
74            "rate limit config requires max_bytes_per_sec > 0 and burst_bytes > 0",
75        ));
76    }
77
78    Ok(())
79}