Skip to content

Commit 50f1c1b

Browse files
Add connection / query timeout
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
1 parent b1c14a8 commit 50f1c1b

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/hosts.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{config::Config, queries::Query};
22
use core::fmt;
3+
use std::time::Duration;
34
use mysql::{prelude::Queryable, Conn, OptsBuilder};
45

56
#[allow(dead_code)]
@@ -68,7 +69,10 @@ impl Host {
6869
.tcp_port(port)
6970
.user(Some(config.readyset_user.clone()))
7071
.pass(Some(config.readyset_password.clone()))
71-
.prefer_socket(false),
72+
.prefer_socket(false)
73+
.read_timeout(Some(Duration::from_secs(5)))
74+
.write_timeout(Some(Duration::from_secs(5)))
75+
.tcp_connect_timeout(Some(Duration::from_secs(5))),
7276
) {
7377
Ok(conn) => conn,
7478
Err(err) => {
@@ -144,14 +148,25 @@ impl Host {
144148
pub fn check_readyset_is_ready(&mut self) -> Result<bool, mysql::Error> {
145149
match &mut self.conn {
146150
Some(conn) => {
147-
let rows: Vec<(String, String)> =
148-
conn.query("SHOW READYSET STATUS").unwrap_or(vec![]);
149-
for (field, value) in rows {
150-
if field == "Snapshot Status" {
151-
return Ok(value == "Completed");
151+
let result =
152+
conn.query("SHOW READYSET STATUS");
153+
match result {
154+
Ok(rows) => {
155+
let rows: Vec<(String, String)> = rows;
156+
for (field, value) in rows {
157+
if field == "Snapshot Status" {
158+
return Ok(value == "Completed");
159+
}
160+
}
161+
Ok(false)
162+
},
163+
Err(err) => {
164+
Err(mysql::Error::IoError(std::io::Error::new(
165+
std::io::ErrorKind::Other,
166+
format!("Failed to execute query: {}", err),
167+
)))
152168
}
153169
}
154-
Ok(false)
155170
}
156171
None => Err(mysql::Error::IoError(std::io::Error::new(
157172
std::io::ErrorKind::Other,

0 commit comments

Comments
 (0)