nexo_plugin_browser/
profile_limits.rs1const ENV_MAX_PROFILES: &str = "NEXO_PLUGIN_BROWSER_MAX_PROFILES";
22const ENV_IDLE_SECS: &str = "NEXO_PLUGIN_BROWSER_PROFILE_IDLE_SECS";
23const ENV_MULTI_PROFILE: &str = "NEXO_PLUGIN_BROWSER_MULTI_PROFILE";
24const ENV_LEGACY_PER_AGENT: &str = "NEXO_PLUGIN_BROWSER_LEGACY_PER_AGENT";
34
35const DEFAULT_MAX_PROFILES: usize = 10;
36const MIN_MAX_PROFILES: usize = 1;
37const MAX_MAX_PROFILES: usize = 64;
38
39const DEFAULT_IDLE_SECS: u64 = 900;
40const MIN_IDLE_SECS: u64 = 0;
41const MAX_IDLE_SECS: u64 = 86_400;
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub struct ProfileLimits {
47 pub max_profiles: usize,
50 pub idle_secs: u64,
53 pub multi_profile_enabled: bool,
56 pub legacy_per_agent_enabled: bool,
61}
62
63impl Default for ProfileLimits {
64 fn default() -> Self {
65 Self {
66 max_profiles: DEFAULT_MAX_PROFILES,
67 idle_secs: DEFAULT_IDLE_SECS,
68 multi_profile_enabled: true,
69 legacy_per_agent_enabled: true,
70 }
71 }
72}
73
74pub fn read_profile_limits() -> ProfileLimits {
78 let legacy = read_legacy_per_agent();
79 if legacy {
80 tracing::info!(
81 target: "plugin.browser",
82 env = ENV_LEGACY_PER_AGENT,
83 note = "deprecation: legacy per-agent profile fan-out will be opt-in in 0.4.0; declare instances in browser.yaml or set NEXO_PLUGIN_BROWSER_LEGACY_PER_AGENT=0 to refuse implicit fallback",
84 "legacy-per-agent path active"
85 );
86 }
87 ProfileLimits {
88 max_profiles: read_max_profiles(),
89 idle_secs: read_idle_secs(),
90 multi_profile_enabled: read_multi_profile(),
91 legacy_per_agent_enabled: legacy,
92 }
93}
94
95fn read_legacy_per_agent() -> bool {
96 let Some(raw) = std::env::var(ENV_LEGACY_PER_AGENT).ok() else {
97 return true; };
99 match raw.trim().to_ascii_lowercase().as_str() {
100 "0" | "false" | "no" | "off" => false,
101 "1" | "true" | "yes" | "on" => true,
102 _ => {
103 tracing::warn!(
104 env = ENV_LEGACY_PER_AGENT,
105 value = %raw,
106 "unparseable; defaulting to enabled"
107 );
108 true
109 }
110 }
111}
112
113fn read_max_profiles() -> usize {
114 let Some(raw) = std::env::var(ENV_MAX_PROFILES).ok() else {
115 return DEFAULT_MAX_PROFILES;
116 };
117 let parsed: usize = match raw.trim().parse() {
118 Ok(n) => n,
119 Err(_) => {
120 tracing::warn!(
121 env = ENV_MAX_PROFILES,
122 value = %raw,
123 default = DEFAULT_MAX_PROFILES,
124 "unparseable; using default"
125 );
126 return DEFAULT_MAX_PROFILES;
127 }
128 };
129 if parsed < MIN_MAX_PROFILES {
130 tracing::warn!(
131 env = ENV_MAX_PROFILES,
132 value = parsed,
133 min = MIN_MAX_PROFILES,
134 "below minimum; clamping"
135 );
136 return MIN_MAX_PROFILES;
137 }
138 if parsed > MAX_MAX_PROFILES {
139 tracing::warn!(
140 env = ENV_MAX_PROFILES,
141 value = parsed,
142 max = MAX_MAX_PROFILES,
143 "above maximum; clamping"
144 );
145 return MAX_MAX_PROFILES;
146 }
147 parsed
148}
149
150fn read_idle_secs() -> u64 {
151 let Some(raw) = std::env::var(ENV_IDLE_SECS).ok() else {
152 return DEFAULT_IDLE_SECS;
153 };
154 let parsed: u64 = match raw.trim().parse() {
155 Ok(n) => n,
156 Err(_) => {
157 tracing::warn!(
158 env = ENV_IDLE_SECS,
159 value = %raw,
160 default = DEFAULT_IDLE_SECS,
161 "unparseable; using default"
162 );
163 return DEFAULT_IDLE_SECS;
164 }
165 };
166 if parsed < MIN_IDLE_SECS {
167 return MIN_IDLE_SECS;
170 }
171 if parsed > MAX_IDLE_SECS {
172 tracing::warn!(
173 env = ENV_IDLE_SECS,
174 value = parsed,
175 max = MAX_IDLE_SECS,
176 "above maximum; clamping"
177 );
178 return MAX_IDLE_SECS;
179 }
180 parsed
181}
182
183fn read_multi_profile() -> bool {
184 let Some(raw) = std::env::var(ENV_MULTI_PROFILE).ok() else {
185 return true;
186 };
187 match raw.trim().to_ascii_lowercase().as_str() {
188 "1" | "true" | "yes" | "on" => true,
189 "0" | "false" | "no" | "off" => false,
190 _ => {
191 tracing::warn!(
192 env = ENV_MULTI_PROFILE,
193 value = %raw,
194 "unparseable; defaulting to true"
195 );
196 true
197 }
198 }
199}
200
201#[cfg(test)]
202mod tests {
203 use super::*;
204 use serial_test::serial;
205
206 fn clear_env() {
207 for k in [ENV_MAX_PROFILES, ENV_IDLE_SECS, ENV_MULTI_PROFILE] {
208 std::env::remove_var(k);
209 }
210 }
211
212 #[test]
213 #[serial]
214 fn defaults_when_no_env() {
215 clear_env();
216 let limits = read_profile_limits();
217 assert_eq!(limits.max_profiles, DEFAULT_MAX_PROFILES);
218 assert_eq!(limits.idle_secs, DEFAULT_IDLE_SECS);
219 assert!(limits.multi_profile_enabled);
220 }
221
222 #[test]
223 #[serial]
224 fn parses_valid_values() {
225 clear_env();
226 std::env::set_var(ENV_MAX_PROFILES, "5");
227 std::env::set_var(ENV_IDLE_SECS, "60");
228 std::env::set_var(ENV_MULTI_PROFILE, "false");
229 let limits = read_profile_limits();
230 assert_eq!(limits.max_profiles, 5);
231 assert_eq!(limits.idle_secs, 60);
232 assert!(!limits.multi_profile_enabled);
233 clear_env();
234 }
235
236 #[test]
237 #[serial]
238 fn clamps_max_profiles_below_minimum() {
239 clear_env();
240 std::env::set_var(ENV_MAX_PROFILES, "0");
241 let limits = read_profile_limits();
242 assert_eq!(limits.max_profiles, MIN_MAX_PROFILES);
243 clear_env();
244 }
245
246 #[test]
247 #[serial]
248 fn clamps_max_profiles_above_maximum() {
249 clear_env();
250 std::env::set_var(ENV_MAX_PROFILES, "10000");
251 let limits = read_profile_limits();
252 assert_eq!(limits.max_profiles, MAX_MAX_PROFILES);
253 clear_env();
254 }
255
256 #[test]
257 #[serial]
258 fn clamps_idle_secs_above_max() {
259 clear_env();
260 std::env::set_var(ENV_IDLE_SECS, "999999");
261 let limits = read_profile_limits();
262 assert_eq!(limits.idle_secs, MAX_IDLE_SECS);
263 clear_env();
264 }
265
266 #[test]
267 #[serial]
268 fn idle_zero_disables_eviction() {
269 clear_env();
270 std::env::set_var(ENV_IDLE_SECS, "0");
271 let limits = read_profile_limits();
272 assert_eq!(limits.idle_secs, 0);
273 clear_env();
274 }
275
276 #[test]
277 #[serial]
278 fn multi_profile_accepts_truthy_and_falsy_strings() {
279 clear_env();
280 std::env::set_var(ENV_MULTI_PROFILE, "yes");
281 assert!(read_profile_limits().multi_profile_enabled);
282 std::env::set_var(ENV_MULTI_PROFILE, "off");
283 assert!(!read_profile_limits().multi_profile_enabled);
284 clear_env();
285 }
286
287 #[test]
288 #[serial]
289 fn unparseable_max_profiles_falls_back_to_default() {
290 clear_env();
291 std::env::set_var(ENV_MAX_PROFILES, "abc");
292 let limits = read_profile_limits();
293 assert_eq!(limits.max_profiles, DEFAULT_MAX_PROFILES);
294 clear_env();
295 }
296}