picodata_rust/
config.rs

1use std::{net::IpAddr, time::Duration};
2
3use pg::config::Host;
4
5const DEFAULT_POOL_SIZE: usize = 10;
6const DEFAULT_DISCOVERY_INTERVAL: Duration = Duration::from_millis(500);
7
8/// Picodata config
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct Config {
11    /// Config to initialize connection.
12    pub postgres: pg::Config,
13    /// Max size of connection pool for one instance.
14    pub pool_max_size: usize,
15    /// Interval for making topology requests.
16    pub discorery_interval: Duration,
17    /// Path to cert file.
18    pub ssl_cert_file: Option<String>,
19    /// Path to key file.
20    pub ssl_key_file: Option<String>,
21    /// Path to ca file.
22    pub ssl_ca_file: Option<String>,
23    /// Tiers that are not used to create connections to.
24    pub excluded_tiers: Option<Vec<String>>,
25}
26
27impl Config {
28    /// Get hosts of instance.
29    ///
30    /// # Returns
31    ///
32    /// Hosts if it was set.
33    #[must_use]
34    pub fn hosts(&self) -> &[Host] {
35        self.postgres.get_hosts()
36    }
37    /// Get ports of instance.
38    ///
39    /// # Returns
40    ///
41    /// Ports if it was set.
42    #[must_use]
43    pub fn ports(&self) -> &[u16] {
44        self.postgres.get_ports()
45    }
46
47    /// Get a hostaddrs of instance.
48    ///
49    /// # Returns
50    ///
51    /// A slice of hostaddrs.
52    #[must_use]
53    pub fn hostaddrs(&self) -> &[IpAddr] {
54        self.postgres.get_hostaddrs()
55    }
56
57    /// Clones the config with resetting addr info of instance.
58    ///
59    /// # Returns
60    ///
61    /// An instance of `Config`.
62    #[must_use]
63    pub fn clone_pg_reset(&self) -> Self {
64        Self {
65            postgres: clone_pg_config(&self.postgres),
66            pool_max_size: self.pool_max_size,
67            discorery_interval: self.discorery_interval,
68            ssl_cert_file: self.ssl_cert_file.clone(),
69            ssl_key_file: self.ssl_key_file.clone(),
70            ssl_ca_file: self.ssl_ca_file.clone(),
71            excluded_tiers: self.excluded_tiers.clone(),
72        }
73    }
74}
75
76impl From<pg::Config> for Config {
77    fn from(postgres: pg::Config) -> Self {
78        Self {
79            postgres,
80            pool_max_size: DEFAULT_POOL_SIZE,
81            discorery_interval: DEFAULT_DISCOVERY_INTERVAL,
82            ssl_cert_file: None,
83            ssl_key_file: None,
84            ssl_ca_file: None,
85            excluded_tiers: None,
86        }
87    }
88}
89
90#[must_use]
91pub fn clone_pg_config(cfg: &pg::Config) -> pg::Config {
92    let mut result = pg::Config::new();
93
94    if let Some(v) = cfg.get_application_name() {
95        result.application_name(v);
96    }
97    if let Some(v) = cfg.get_user() {
98        result.user(v);
99    }
100    if let Some(v) = cfg.get_password() {
101        result.password(v);
102    }
103    if let Some(v) = cfg.get_dbname() {
104        result.dbname(v);
105    }
106    if let Some(v) = cfg.get_options() {
107        result.options(v);
108    }
109    if let Some(v) = cfg.get_connect_timeout() {
110        result.connect_timeout(*v);
111    }
112    if let Some(v) = cfg.get_tcp_user_timeout() {
113        result.tcp_user_timeout(*v);
114    }
115
116    if let Some(v) = cfg.get_keepalives_interval() {
117        result.keepalives_interval(v);
118    }
119    if let Some(v) = cfg.get_keepalives_retries() {
120        result.keepalives_retries(v);
121    }
122
123    result.ssl_mode(cfg.get_ssl_mode());
124    result.ssl_negotiation(cfg.get_ssl_negotiation());
125    result.keepalives(cfg.get_keepalives());
126    result.keepalives_idle(cfg.get_keepalives_idle());
127    result.target_session_attrs(cfg.get_target_session_attrs());
128    result.channel_binding(cfg.get_channel_binding());
129    result.load_balance_hosts(cfg.get_load_balance_hosts());
130
131    result
132}