Skip to main content

halephant_core/config/
server.rs

1use std::path::PathBuf;
2use std::time::Duration;
3
4use serde::Deserialize;
5
6use crate::errors;
7
8#[derive(Debug, Deserialize)]
9pub struct ServerConfig {
10    #[serde(default = "default_listen")]
11    pub listen: Vec<String>,
12
13    /// Path to a `.pgpass` file for upstream password authentication. Defaults
14    /// to `~/.pgpass` or the `PGPASSFILE` environment variable.
15    pub pgpass: Option<PathBuf>,
16
17    #[serde(default)]
18    pub workers: usize,
19
20    #[serde(default = "default_shutdown_timeout", with = "humantime_serde")]
21    pub shutdown_timeout: Duration,
22
23    /// Maximum time a client waits in the per-role wait queue when every
24    /// candidate pool is at its `max_connections`. After this elapses,
25    /// the checkout fails with a classified `checkout_timeout` error.
26    #[serde(default = "default_checkout_timeout", with = "humantime_serde")]
27    pub checkout_timeout: Duration,
28
29    /// Maximum unique prepared statements tracked globally. Set to 0 for
30    /// unlimited. Lower this in memory-constrained environments.
31    #[serde(default = "default_max_prepared_statements")]
32    pub max_prepared_statements: u32,
33}
34
35impl Default for ServerConfig {
36    fn default() -> Self {
37        Self {
38            listen: default_listen(),
39            pgpass: None,
40            workers: 0,
41            shutdown_timeout: default_shutdown_timeout(),
42            checkout_timeout: default_checkout_timeout(),
43            max_prepared_statements: default_max_prepared_statements(),
44        }
45    }
46}
47
48impl ServerConfig {
49    pub(crate) fn validate(&self) -> Result<(), errors::ConfigError> {
50        if self.listen.is_empty() {
51            return Err(errors::ConfigError::Validation(
52                "server.listen must have at least one address".into(),
53            ));
54        }
55
56        Ok(())
57    }
58}
59
60// ---------------------------------------------------------------------------
61// Defaults
62// ---------------------------------------------------------------------------
63
64fn default_listen() -> Vec<String> {
65    vec!["0.0.0.0:6432".into()]
66}
67
68fn default_shutdown_timeout() -> Duration {
69    Duration::from_secs(30)
70}
71
72fn default_checkout_timeout() -> Duration {
73    Duration::from_secs(30)
74}
75
76fn default_max_prepared_statements() -> u32 {
77    0
78}