agent_tui/daemon/
config.rs

1use std::env;
2use std::time::Duration;
3
4use crate::daemon::session::DEFAULT_MAX_SESSIONS;
5
6const DEFAULT_MAX_CONNECTIONS: usize = 64;
7const DEFAULT_LOCK_TIMEOUT_SECS: u64 = 5;
8const DEFAULT_IDLE_TIMEOUT_SECS: u64 = 300;
9const DEFAULT_MAX_REQUEST_BYTES: usize = 1_048_576; // 1MB
10
11#[derive(Debug, Clone)]
12pub struct DaemonConfig {
13    pub max_connections: usize,
14    pub lock_timeout: Duration,
15    pub idle_timeout: Duration,
16    pub max_request_bytes: usize,
17    pub max_sessions: usize,
18}
19
20impl Default for DaemonConfig {
21    fn default() -> Self {
22        Self::from_env()
23    }
24}
25
26impl DaemonConfig {
27    pub fn from_env() -> Self {
28        Self {
29            max_connections: env::var("AGENT_TUI_MAX_CONNECTIONS")
30                .ok()
31                .and_then(|v| v.parse().ok())
32                .unwrap_or(DEFAULT_MAX_CONNECTIONS),
33            lock_timeout: Duration::from_secs(
34                env::var("AGENT_TUI_LOCK_TIMEOUT")
35                    .ok()
36                    .and_then(|v| v.parse().ok())
37                    .unwrap_or(DEFAULT_LOCK_TIMEOUT_SECS),
38            ),
39            idle_timeout: Duration::from_secs(
40                env::var("AGENT_TUI_IDLE_TIMEOUT")
41                    .ok()
42                    .and_then(|v| v.parse().ok())
43                    .unwrap_or(DEFAULT_IDLE_TIMEOUT_SECS),
44            ),
45            max_request_bytes: env::var("AGENT_TUI_MAX_REQUEST")
46                .ok()
47                .and_then(|v| v.parse().ok())
48                .unwrap_or(DEFAULT_MAX_REQUEST_BYTES),
49            max_sessions: env::var("AGENT_TUI_MAX_SESSIONS")
50                .ok()
51                .and_then(|v| v.parse().ok())
52                .unwrap_or(DEFAULT_MAX_SESSIONS),
53        }
54    }
55
56    pub fn with_max_connections(mut self, max: usize) -> Self {
57        self.max_connections = max;
58        self
59    }
60
61    pub fn with_lock_timeout(mut self, timeout: Duration) -> Self {
62        self.lock_timeout = timeout;
63        self
64    }
65
66    pub fn with_idle_timeout(mut self, timeout: Duration) -> Self {
67        self.idle_timeout = timeout;
68        self
69    }
70
71    pub fn with_max_request_bytes(mut self, max: usize) -> Self {
72        self.max_request_bytes = max;
73        self
74    }
75
76    pub fn with_max_sessions(mut self, max: usize) -> Self {
77        self.max_sessions = max;
78        self
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_default_config() {
88        let config = DaemonConfig::default();
89        assert_eq!(config.max_connections, DEFAULT_MAX_CONNECTIONS);
90        assert_eq!(
91            config.lock_timeout,
92            Duration::from_secs(DEFAULT_LOCK_TIMEOUT_SECS)
93        );
94        assert_eq!(
95            config.idle_timeout,
96            Duration::from_secs(DEFAULT_IDLE_TIMEOUT_SECS)
97        );
98        assert_eq!(config.max_request_bytes, DEFAULT_MAX_REQUEST_BYTES);
99        assert_eq!(config.max_sessions, DEFAULT_MAX_SESSIONS);
100    }
101
102    #[test]
103    fn test_builder_pattern() {
104        let config = DaemonConfig::default()
105            .with_max_connections(128)
106            .with_lock_timeout(Duration::from_secs(10))
107            .with_idle_timeout(Duration::from_secs(600))
108            .with_max_request_bytes(2_097_152)
109            .with_max_sessions(32);
110
111        assert_eq!(config.max_connections, 128);
112        assert_eq!(config.lock_timeout, Duration::from_secs(10));
113        assert_eq!(config.idle_timeout, Duration::from_secs(600));
114        assert_eq!(config.max_request_bytes, 2_097_152);
115        assert_eq!(config.max_sessions, 32);
116    }
117}