diff options
author | Matthias Beyer <matthias.beyer@atos.net> | 2021-04-23 12:51:57 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-04-26 14:46:22 +0200 |
commit | 1ebfa387fa5d47e3200b990f63f55bbe36adeff1 (patch) | |
tree | 52496b7406e2b90150358e0ad247e769e3f3b702 | |
parent | 155b9001a2db30588acf148f9c755f4b187e8e3a (diff) |
Add support for database connection timeout
This patch adds support for a database connection timeout, giving the user the
option to configure one, but hardcoding a default timeout to 30 seconds.
Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
-rw-r--r-- | config.toml | 4 | ||||
-rw-r--r-- | src/cli.rs | 7 | ||||
-rw-r--r-- | src/config/not_validated.rs | 4 | ||||
-rw-r--r-- | src/db/connection.rs | 14 |
4 files changed, 27 insertions, 2 deletions
diff --git a/config.toml b/config.toml index 79f341a..a5a0024 100644 --- a/config.toml +++ b/config.toml @@ -127,6 +127,10 @@ database_user = "pgdev" database_password = "password" database_name = "butido" +# Set a database connection timeout +# If not set, this defaults to 30 +#database_connection_timeout = 30 + # Phases which can be configured in the packages @@ -69,6 +69,13 @@ pub fn cli<'a>() -> App<'a> { .value_name("NAME") .about("Overwrite the database name set via configuration. Can also be overriden via environment, but this setting has presendence.") ) + .arg(Arg::new("database_connection_timeout") + .required(false) + .multiple(false) + .long("db-timeout") + .value_name("TIMEOUT") + .about("Overwrite the database connection timeout set via configuration. Can also be overriden via environment, but this setting has presendence.") + ) .subcommand(App::new("generate-completions") .version(crate_version!()) diff --git a/src/config/not_validated.rs b/src/config/not_validated.rs index 451a3d2..14399c4 100644 --- a/src/config/not_validated.rs +++ b/src/config/not_validated.rs @@ -97,6 +97,10 @@ pub struct NotValidatedConfiguration { database_name: String, #[getset(get = "pub")] + #[serde(rename = "database_connection_timeout")] + database_connection_timeout: Option<u16>, + + #[getset(get = "pub")] docker: DockerConfig, #[getset(get = "pub")] diff --git a/src/db/connection.rs b/src/db/connection.rs index 33de9e7..fb18e32 100644 --- a/src/db/connection.rs +++ b/src/db/connection.rs @@ -34,17 +34,21 @@ pub struct DbConnectionConfig { #[getset(get = "pub")] database_name: String, + + #[getset(get = "pub")] + database_connection_timeout: String, } impl Into<String> for DbConnectionConfig { fn into(self) -> String { format!( - "postgres://{user}:{password}@{host}:{port}/{name}", + "postgres://{user}:{password}@{host}:{port}/{name}?connect_timeout={timeout}", host = self.database_host, port = self.database_port, user = self.database_user, password = self.database_password, - name = self.database_name + name = self.database_name, + timeout = self.database_connection_timeout, ) } } @@ -66,6 +70,11 @@ pub fn parse_db_connection_config(config: &Configuration, cli: &ArgMatches) -> D config.database_password().to_string() }); let database_name = find_value(cli, "database_name", || config.database_name().to_string()); + let database_connection_timeout = find_value(cli, "database_connection_timeout", + || { + // hardcoded default of 30 seconds database timeout + config.database_connection_timeout().unwrap_or(30).to_string() + }); DbConnectionConfig { database_host, @@ -73,6 +82,7 @@ pub fn parse_db_connection_config(config: &Configuration, cli: &ArgMatches) -> D database_user, database_password, database_name, + database_connection_timeout, } } |