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