nntp_proxy/config/
validation.rs

1//! Configuration validation
2//!
3//! This module provides validation logic for the configuration to ensure
4//! all settings are valid before the proxy starts.
5
6use anyhow::Result;
7use std::time::Duration;
8
9use super::types::{Config, ServerConfig};
10use crate::constants::pool::{MAX_RECOMMENDED_KEEPALIVE_SECS, MIN_RECOMMENDED_KEEPALIVE_SECS};
11
12const MIN_RECOMMENDED_KEEPALIVE: Duration = Duration::from_secs(MIN_RECOMMENDED_KEEPALIVE_SECS);
13const MAX_RECOMMENDED_KEEPALIVE: Duration = Duration::from_secs(MAX_RECOMMENDED_KEEPALIVE_SECS);
14
15impl Config {
16    /// Validate configuration for correctness
17    ///
18    /// Most validations are now enforced by type system (NonZero types, validated strings, etc.)
19    /// This checks remaining semantic constraints:
20    /// - At least one server configured
21    /// - Keep-alive intervals are in recommended ranges
22    pub fn validate(&self) -> Result<()> {
23        if self.servers.is_empty() {
24            return Err(anyhow::anyhow!(
25                "Configuration must have at least one server"
26            ));
27        }
28
29        for server in &self.servers {
30            validate_server(server)?;
31        }
32
33        Ok(())
34    }
35}
36
37/// Validate a single server configuration
38fn validate_server(server: &ServerConfig) -> Result<()> {
39    // Name, host, port, max_connections validations now enforced by types:
40    // - HostName/ServerName cannot be empty (validated at construction)
41    // - Port cannot be 0 (NonZeroU16)
42    // - max_connections cannot be 0 (NonZeroUsize via MaxConnections)
43
44    // Warn if connection_keepalive is outside recommended range
45    if let Some(keepalive) = server.connection_keepalive {
46        if keepalive < MIN_RECOMMENDED_KEEPALIVE {
47            tracing::warn!(
48                "Server '{}' has connection_keepalive set to {:?} (< {:?}). \
49                 This may cause excessive health check traffic and connection churn. \
50                 Consider using at least {:?} or None to disable.",
51                server.name.as_str(),
52                keepalive,
53                MIN_RECOMMENDED_KEEPALIVE,
54                MIN_RECOMMENDED_KEEPALIVE
55            );
56        } else if keepalive > MAX_RECOMMENDED_KEEPALIVE {
57            tracing::warn!(
58                "Server '{}' has connection_keepalive set to {:?} (> {:?} / 5 minutes). \
59                 This may not detect stale connections quickly enough. Consider a lower value.",
60                server.name.as_str(),
61                keepalive,
62                MAX_RECOMMENDED_KEEPALIVE
63            );
64        }
65    }
66
67    Ok(())
68}