From 6c6e5bcb22eb553150d16549c698919e88d2a092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Cardoso?= Date: Mon, 4 Nov 2024 18:31:42 -0300 Subject: [PATCH] src: lib: Fix broken loop delay logic all over --- src/lib/drivers/rest/mod.rs | 6 ++++-- src/lib/drivers/serial/mod.rs | 6 ++++-- src/lib/drivers/tcp/client.rs | 7 ++++--- src/lib/drivers/tcp/server.rs | 6 ++++-- src/lib/drivers/udp/client.rs | 8 ++++---- src/lib/drivers/udp/server.rs | 7 ++++--- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/lib/drivers/rest/mod.rs b/src/lib/drivers/rest/mod.rs index 302ed617..c2b5a3a4 100644 --- a/src/lib/drivers/rest/mod.rs +++ b/src/lib/drivers/rest/mod.rs @@ -162,11 +162,13 @@ pub fn parse_query(message: &T) -> String { impl Driver for Rest { #[instrument(level = "debug", skip(self, hub_sender))] async fn run(&self, hub_sender: broadcast::Sender>) -> Result<()> { + let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); let mut first = true; loop { - if !first { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + if first { first = false; + } else { + interval.tick().await; } let hub_sender = hub_sender.clone(); diff --git a/src/lib/drivers/serial/mod.rs b/src/lib/drivers/serial/mod.rs index 8778e0cd..7c2a7d38 100644 --- a/src/lib/drivers/serial/mod.rs +++ b/src/lib/drivers/serial/mod.rs @@ -86,11 +86,13 @@ impl Driver for Serial { stats: self.stats.clone(), }; + let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); let mut first = true; loop { - if !first { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + if first { first = false; + } else { + interval.tick().await; } debug!("Trying to connect..."); diff --git a/src/lib/drivers/tcp/client.rs b/src/lib/drivers/tcp/client.rs index 5f8d95f8..6b667b3a 100644 --- a/src/lib/drivers/tcp/client.rs +++ b/src/lib/drivers/tcp/client.rs @@ -89,11 +89,13 @@ impl Driver for TcpClient { stats: self.stats.clone(), }; + let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); let mut first = true; loop { - if !first { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + if first { first = false; + } else { + interval.tick().await; } debug!("Trying to connect..."); @@ -102,7 +104,6 @@ impl Driver for TcpClient { Ok(stream) => stream, Err(error) => { error!("Failed connecting: {error:?}"); - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; continue; } }; diff --git a/src/lib/drivers/tcp/server.rs b/src/lib/drivers/tcp/server.rs index 907964b6..c7be989e 100644 --- a/src/lib/drivers/tcp/server.rs +++ b/src/lib/drivers/tcp/server.rs @@ -111,11 +111,13 @@ impl Driver for TcpServer { stats: self.stats.clone(), }; + let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); let mut first = true; loop { - if !first { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + if first { first = false; + } else { + interval.tick().await; } debug!("Trying to bind to address {local_addr:?}..."); diff --git a/src/lib/drivers/udp/client.rs b/src/lib/drivers/udp/client.rs index 8c156de0..0717c532 100644 --- a/src/lib/drivers/udp/client.rs +++ b/src/lib/drivers/udp/client.rs @@ -87,11 +87,13 @@ impl Driver for UdpClient { stats: self.stats.clone(), }; + let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); let mut first = true; loop { - if !first { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + if first { first = false; + } else { + interval.tick().await; } debug!("Trying to bind to address {local_addr:?}..."); @@ -100,7 +102,6 @@ impl Driver for UdpClient { Ok(socket) => socket, Err(error) => { error!("Failed binding UdpClient to address {local_addr:?}: {error:?}"); - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; continue; } }; @@ -109,7 +110,6 @@ impl Driver for UdpClient { if let Err(error) = socket.connect(&remote_addr).await { error!("Failed connecting UdpClient to {remote_addr:?}: {error:?}"); - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; continue; }; diff --git a/src/lib/drivers/udp/server.rs b/src/lib/drivers/udp/server.rs index 8e212ad1..d6e372fe 100644 --- a/src/lib/drivers/udp/server.rs +++ b/src/lib/drivers/udp/server.rs @@ -95,11 +95,13 @@ impl Driver for UdpServer { stats: self.stats.clone(), }; + let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); let mut first = true; loop { - if !first { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + if first { first = false; + } else { + interval.tick().await; } debug!("Trying to bind to address {local_addr:?}..."); @@ -108,7 +110,6 @@ impl Driver for UdpServer { Ok(socket) => Arc::new(socket), Err(error) => { error!("Failed binding UdpServer to address {local_addr:?}: {error:?}"); - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; continue; } };