Skip to main content

halephant_core/config/cluster/pool/
user.rs

1use std::collections::HashMap;
2
3use serde::Deserialize;
4
5use crate::config::cluster::pool::ConnectionLimits;
6
7#[derive(Debug, Deserialize)]
8pub struct UserConfig {
9    /// This user is an alias for another PostgreSQL role. When set, halephant
10    /// authenticates using the aliased role's credentials and connects upstream
11    /// as that role.
12    pub alias: Option<String>,
13
14    /// Maximum connections for this user, split by primary (rw) and replica (ro).
15    pub max_connections: Option<ConnectionLimits>,
16
17    /// Minimum idle connections to maintain for this user at startup.
18    #[serde(default)]
19    pub min_connections: ConnectionLimits,
20
21    /// PostgreSQL connection parameters sent in the StartupMessage.
22    #[serde(default)]
23    pub parameters: UserParameters,
24}
25
26impl UserConfig {
27    /// The effective upstream user — the `alias` target if set, otherwise the
28    /// TOML key (the client-facing username) is used by the caller.
29    pub fn upstream_name<'a>(&'a self, key: &'a str) -> &'a str {
30        self.alias.as_deref().unwrap_or(key)
31    }
32
33    /// Whether this user is read-only (no rw capacity).
34    pub fn is_read_only(&self) -> bool {
35        self.max_connections
36            .as_ref()
37            .is_some_and(|c| c.primary == 0 && c.replica > 0)
38    }
39}
40
41/// PostgreSQL parameters sent in the StartupMessage.
42#[derive(Clone, Debug, Default, Deserialize)]
43pub struct UserParameters {
44    /// Application name visible in `pg_stat_activity`.
45    pub application_name: Option<String>,
46
47    /// GUC settings passed via the `options` connection parameter as `-c`
48    /// key-value pairs (for example, `search_path`, `statement_timeout`).
49    #[serde(default)]
50    pub options: HashMap<String, String>,
51}