make database connection timeout configurable

This commit is contained in:
Oystein Kristoffer Tveit 2024-07-09 21:34:59 +02:00
parent 4dedde5edb
commit 2b056942eb
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
2 changed files with 23 additions and 34 deletions

View File

@ -5,3 +5,4 @@ host = "localhost"
port = 3306 port = 3306
username = "root" username = "root"
password = "secret" password = "secret"
timeout = 2 # seconds

View File

@ -17,8 +17,12 @@ pub struct MysqlConfig {
pub port: Option<u16>, pub port: Option<u16>,
pub username: String, pub username: String,
pub password: String, pub password: String,
pub timeout: Option<u64>,
} }
const DEFAULT_PORT: u16 = 3306;
const DEFAULT_TIMEOUT: u64 = 2;
#[derive(Parser)] #[derive(Parser)]
pub struct GlobalConfigArgs { pub struct GlobalConfigArgs {
/// Path to the configuration file. /// Path to the configuration file.
@ -28,45 +32,29 @@ pub struct GlobalConfigArgs {
value_name = "PATH", value_name = "PATH",
global = true, global = true,
hide_short_help = true, hide_short_help = true,
default_value = "/etc/mysqladm/config.toml", default_value = "/etc/mysqladm/config.toml"
)] )]
config_file: String, config_file: String,
/// Hostname of the MySQL server. /// Hostname of the MySQL server.
#[arg( #[arg(long, value_name = "HOST", global = true, hide_short_help = true)]
long,
value_name = "HOST",
global = true,
hide_short_help = true,
)]
mysql_host: Option<String>, mysql_host: Option<String>,
/// Port of the MySQL server. /// Port of the MySQL server.
#[arg( #[arg(long, value_name = "PORT", global = true, hide_short_help = true)]
long,
value_name = "PORT",
global = true,
hide_short_help = true,
)]
mysql_port: Option<u16>, mysql_port: Option<u16>,
/// Username to use for the MySQL connection. /// Username to use for the MySQL connection.
#[arg( #[arg(long, value_name = "USER", global = true, hide_short_help = true)]
long,
value_name = "USER",
global = true,
hide_short_help = true,
)]
mysql_user: Option<String>, mysql_user: Option<String>,
/// Path to a file containing the MySQL password. /// Path to a file containing the MySQL password.
#[arg( #[arg(long, value_name = "PATH", global = true, hide_short_help = true)]
long,
value_name = "PATH",
global = true,
hide_short_help = true,
)]
mysql_password_file: Option<String>, mysql_password_file: Option<String>,
/// Seconds to wait for the MySQL connection to be established.
#[arg(long, value_name = "SECONDS", global = true, hide_short_help = true)]
mysql_connect_timeout: Option<u64>,
} }
pub fn get_config(args: GlobalConfigArgs) -> anyhow::Result<Config> { pub fn get_config(args: GlobalConfigArgs) -> anyhow::Result<Config> {
@ -98,6 +86,7 @@ pub fn get_config(args: GlobalConfigArgs) -> anyhow::Result<Config> {
port: args.mysql_port.or(mysql.port), port: args.mysql_port.or(mysql.port),
username: args.mysql_user.unwrap_or(mysql.username.to_owned()), username: args.mysql_user.unwrap_or(mysql.username.to_owned()),
password, password,
timeout: args.mysql_connect_timeout.or(mysql.timeout),
}; };
Ok(Config { Ok(Config {
@ -105,15 +94,14 @@ pub fn get_config(args: GlobalConfigArgs) -> anyhow::Result<Config> {
}) })
} }
/// TODO: Make timeout configurable
pub async fn mysql_connection_from_config(config: Config) -> anyhow::Result<MySqlConnection> { pub async fn mysql_connection_from_config(config: Config) -> anyhow::Result<MySqlConnection> {
match tokio::time::timeout( match tokio::time::timeout(
Duration::from_secs(2), Duration::from_secs(config.mysql.timeout.unwrap_or(DEFAULT_TIMEOUT)),
MySqlConnectOptions::new() MySqlConnectOptions::new()
.host(&config.mysql.host) .host(&config.mysql.host)
.username(&config.mysql.username) .username(&config.mysql.username)
.password(&config.mysql.password) .password(&config.mysql.password)
.port(config.mysql.port.unwrap_or(3306)) .port(config.mysql.port.unwrap_or(DEFAULT_PORT))
.database("mysql") .database("mysql")
.connect(), .connect(),
) )