Skip to content

Commit

Permalink
Add connection / query timeout
Browse files Browse the repository at this point in the history
Add a timeout to the connection phase and query phase. In case a
connection get stuck, this will move the server to shunned state.

Fixes: #24
  • Loading branch information
altmannmarcelo committed Nov 4, 2024
1 parent a80e710 commit 4be3d85
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/hosts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{config::Config, queries::Query};
use core::fmt;
use std::time::Duration;
use mysql::{prelude::Queryable, Conn, OptsBuilder};

#[allow(dead_code)]
Expand Down Expand Up @@ -68,7 +69,10 @@ impl Host {
.tcp_port(port)
.user(Some(config.readyset_user.clone()))
.pass(Some(config.readyset_password.clone()))
.prefer_socket(false),
.prefer_socket(false)
.read_timeout(Some(Duration::from_secs(5)))
.write_timeout(Some(Duration::from_secs(5)))
.tcp_connect_timeout(Some(Duration::from_secs(5))),
) {
Ok(conn) => conn,
Err(err) => {
Expand Down Expand Up @@ -144,14 +148,25 @@ impl Host {
pub fn check_readyset_is_ready(&mut self) -> Result<bool, mysql::Error> {
match &mut self.conn {
Some(conn) => {
let rows: Vec<(String, String)> =
conn.query("SHOW READYSET STATUS").unwrap_or(vec![]);
for (field, value) in rows {
if field == "Snapshot Status" {
return Ok(value == "Completed");
let result =
conn.query("SHOW READYSET STATUS");
match result {
Ok(rows) => {
let rows: Vec<(String, String)> = rows;
for (field, value) in rows {
if field == "Snapshot Status" {
return Ok(value == "Completed");
}
}
Ok(false)
},
Err(err) => {
Err(mysql::Error::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to execute query: {}", err),
)))
}
}
Ok(false)
}
None => Err(mysql::Error::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
Expand Down

0 comments on commit 4be3d85

Please sign in to comment.