From ef11b0f217f8dcc4bb2403f8c18822cce6db1e91 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 19 Oct 2023 22:15:31 +0200 Subject: [PATCH] Add a constant for socket timeout. Increase timeout from 5 s to 7 s. --- src/main.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 967eb5b..c7791a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ const FILE_CONF: &str = "config.ron"; const PING_PERIOD: Duration = Duration::from_secs(5); // 5 s. const EMAIL_RESEND_PERIOD: Duration = Duration::from_secs(2 * 60 * 60); // 2 h. const STATE_PRINT_PERIOD: Duration = Duration::from_secs(15 * 60); // 15 min. +const SOCKET_TIMEOUT: Duration = Duration::from_secs(7); fn main() -> Result<()> { println!("Staking Watchdog Watchdog"); @@ -44,12 +45,9 @@ fn main() -> Result<()> { let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); socket.connect("192.168.2.102:8739").unwrap(); - socket - .set_read_timeout(Some(Duration::from_secs(5))) - .unwrap(); - socket - .set_write_timeout(Some(Duration::from_secs(5))) - .unwrap(); + socket.set_nonblocking(false).unwrap(); + socket.set_read_timeout(Some(SOCKET_TIMEOUT)).unwrap(); + socket.set_write_timeout(Some(SOCKET_TIMEOUT)).unwrap(); loop { let time_beginning_loop = time::Instant::now(); @@ -81,7 +79,7 @@ fn main() -> Result<()> { // Send e-mail. println!("Sending email..."); match send_email( - "Watchdog ERROR", + "Watchdog Watchdog ERROR", &format!("Error: {:?}", error), &config.smtp_login, &config.smtp_password, @@ -107,7 +105,8 @@ fn main() -> Result<()> { #[derive(Debug)] enum PingError { - SocketError(std::io::Error), + SocketReceiveError(std::io::Error), + SocketSendError(std::io::Error), WrongMessageReceived(String), } @@ -136,10 +135,10 @@ fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result return Err(PingError::SocketError(error)), + Err(error) => return Err(PingError::SocketReceiveError(error)), } } - Err(error) => return Err(PingError::SocketError(error)), + Err(error) => return Err(PingError::SocketSendError(error)), } Ok(time::Instant::now() - now) @@ -148,7 +147,7 @@ fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result Result<()> { let email = Message::builder() .message_id(None) - .from("Staking Watchdog ".parse()?) + .from("Staking Watchdog Watchdog ".parse()?) .to("Greg Burri ".parse()?) .subject(title) .header(ContentType::TEXT_PLAIN) -- 2.45.1