Add timeout to mysql connection
This commit is contained in:
parent
2f039c0b1d
commit
561241d589
|
@ -1,6 +1,6 @@
|
||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf, time::Duration};
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::{anyhow, Context};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{mysql::MySqlConnectOptions, ConnectOptions, MySqlConnection};
|
use sqlx::{mysql::MySqlConnectOptions, ConnectOptions, MySqlConnection};
|
||||||
|
@ -110,13 +110,19 @@ pub fn get_config(args: ConfigOverrideArgs) -> anyhow::Result<Config> {
|
||||||
|
|
||||||
/// TODO: Add timeout.
|
/// TODO: Add timeout.
|
||||||
pub async fn mysql_connection_from_config(config: Config) -> anyhow::Result<MySqlConnection> {
|
pub async fn mysql_connection_from_config(config: Config) -> anyhow::Result<MySqlConnection> {
|
||||||
MySqlConnectOptions::new()
|
match tokio::time::timeout(
|
||||||
.host(&config.mysql.host)
|
Duration::from_secs(2),
|
||||||
.username(&config.mysql.username)
|
MySqlConnectOptions::new()
|
||||||
.password(&config.mysql.password)
|
.host(&config.mysql.host)
|
||||||
.port(config.mysql.port.unwrap_or(3306))
|
.username(&config.mysql.username)
|
||||||
.database("mysql")
|
.password(&config.mysql.password)
|
||||||
.connect()
|
.port(config.mysql.port.unwrap_or(3306))
|
||||||
.await
|
.database("mysql")
|
||||||
.context("Failed to connect to MySQL")
|
.connect(),
|
||||||
|
).await {
|
||||||
|
Ok(conn) => conn.context("Failed to connect to MySQL"),
|
||||||
|
Err(_) => {
|
||||||
|
Err(anyhow!("Timed out after 2 seconds")).context("Failed to connect to MySQL")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue