cat_gateway/settings/
event_db.rs

1//! Command line and environment variable settings for the service
2
3use std::time::Duration;
4
5use super::str_env_var::StringEnvVar;
6
7/// Default the maximum number of connections managed by the pool.
8const EVENT_DB_MAX_CONNECTIONS_DEFAULT: u32 = 100;
9
10/// Default the timeout when creating a new connection.
11const EVENT_DB_CONN_TIMEOUT_DEFAULT: Duration = Duration::from_secs(5);
12
13/// Default the timeout for waiting for a slot to become available.
14const EVENT_DB_SLOT_WAIT_TIMEOUT_DEFAULT: Duration = Duration::from_secs(5);
15
16/// Default the timeout when for recycling a connection.
17const EVENT_DB_CONN_RECYCLE_TIMEOUT_DEFAULT: Duration = Duration::from_secs(5);
18
19/// Default Event DB URL.
20const EVENT_DB_URL_DEFAULT: &str =
21    "postgresql://postgres:postgres@localhost/catalyst_events?sslmode=disable";
22
23/// Configuration for event db.
24#[derive(Clone)]
25pub(crate) struct EnvVars {
26    /// The Address of the Event DB.
27    url: StringEnvVar,
28
29    /// The `UserName` to use for the Event DB.
30    username: Option<StringEnvVar>,
31
32    /// The Address of the Event DB.
33    password: Option<StringEnvVar>,
34
35    /// Sets the maximum number of connections managed by the pool.
36    /// Defaults to 10.
37    max_connections: u32,
38
39    /// Sets the timeout when creating a new connection.
40    /// Defaults to 5 seconds.
41    connection_creation_timeout: Duration,
42
43    /// Sets the timeout for waiting for a slot to become available.
44    /// Defaults to 5 seconds.
45    slot_wait_timeout: Duration,
46
47    /// Sets the timeout when for recycling a connection.
48    /// Defaults to 5 seconds.
49    connection_recycle_timeout: Duration,
50}
51
52impl EnvVars {
53    /// Create a config for event db.
54    pub(super) fn new() -> Self {
55        Self {
56            url: StringEnvVar::new("EVENT_DB_URL", (EVENT_DB_URL_DEFAULT, true).into()),
57            username: StringEnvVar::new_optional("EVENT_DB_USERNAME", false),
58            password: StringEnvVar::new_optional("EVENT_DB_PASSWORD", true),
59            max_connections: StringEnvVar::new_as_int(
60                "EVENT_DB_MAX_CONNECTIONS",
61                EVENT_DB_MAX_CONNECTIONS_DEFAULT,
62                0,
63                u32::MAX,
64            ),
65            connection_creation_timeout: StringEnvVar::new_as_duration(
66                "EVENT_DB_CONN_TIMEOUT",
67                EVENT_DB_CONN_TIMEOUT_DEFAULT,
68            ),
69            slot_wait_timeout: StringEnvVar::new_as_duration(
70                "EVENT_DB_SLOT_WAIT_TIMEOUT",
71                EVENT_DB_SLOT_WAIT_TIMEOUT_DEFAULT,
72            ),
73            connection_recycle_timeout: StringEnvVar::new_as_duration(
74                "EVENT_DB_CONN_RECYCLE_TIMEOUT",
75                EVENT_DB_CONN_RECYCLE_TIMEOUT_DEFAULT,
76            ),
77        }
78    }
79
80    /// Returns Event DB `url` setting
81    pub(crate) fn url(&self) -> &str {
82        self.url.as_str()
83    }
84
85    /// Returns Event DB `username` setting
86    pub(crate) fn username(&self) -> Option<&str> {
87        self.username.as_ref().map(StringEnvVar::as_str)
88    }
89
90    /// Returns Event DB `password` setting
91    pub(crate) fn password(&self) -> Option<&str> {
92        self.password.as_ref().map(StringEnvVar::as_str)
93    }
94
95    /// Returns Event DB `max_connections` setting
96    pub(crate) fn max_connections(&self) -> u32 {
97        self.max_connections
98    }
99
100    /// Returns Event DB `connection_creation_timeout` setting
101    pub(crate) fn connection_creation_timeout(&self) -> Duration {
102        self.connection_creation_timeout
103    }
104
105    /// Returns Event DB `slot_wait_timeout` setting
106    pub(crate) fn slot_wait_timeout(&self) -> Duration {
107        self.slot_wait_timeout
108    }
109
110    /// Returns Event DB `connection_recycle_timeout` setting
111    pub(crate) fn connection_recycle_timeout(&self) -> Duration {
112        self.connection_recycle_timeout
113    }
114}