Skip to content

Commit

Permalink
src: lib: Fix broken loop delay logic all over
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Nov 4, 2024
1 parent c1d718a commit 6c6e5bc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/lib/drivers/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ pub fn parse_query<T: serde::ser::Serialize>(message: &T) -> String {
impl Driver for Rest {
#[instrument(level = "debug", skip(self, hub_sender))]
async fn run(&self, hub_sender: broadcast::Sender<Arc<Protocol>>) -> 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();
Expand Down
6 changes: 4 additions & 2 deletions src/lib/drivers/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand Down
7 changes: 4 additions & 3 deletions src/lib/drivers/tcp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand All @@ -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;
}
};
Expand Down
6 changes: 4 additions & 2 deletions src/lib/drivers/tcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}...");
Expand Down
8 changes: 4 additions & 4 deletions src/lib/drivers/udp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}...");
Expand All @@ -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;
}
};
Expand All @@ -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;
};

Expand Down
7 changes: 4 additions & 3 deletions src/lib/drivers/udp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}...");
Expand All @@ -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;
}
};
Expand Down

0 comments on commit 6c6e5bc

Please sign in to comment.