nntp_proxy/config/
validation.rs1use 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 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
37fn validate_server(server: &ServerConfig) -> Result<()> {
39 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}