Skip to content

Commit

Permalink
Improved retry code
Browse files Browse the repository at this point in the history
  • Loading branch information
ElFantasma committed Mar 11, 2024
1 parent 446ed33 commit 14259dd
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions node/tools/src/k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use kube::{
core::ObjectMeta,
Api, Client,
};
use std::collections::BTreeMap;
use std::net::SocketAddr;
use std::{collections::BTreeMap, net::SocketAddr, time::Duration};
use tokio::time;
use tracing::log::info;
use zksync_consensus_roles::node;
use zksync_protobuf::serde::Serde;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl ConsensusNode {
) -> anyhow::Result<Pod> {
let pods: Api<Pod> = Api::namespaced(client.clone(), namespace);
// Wait until the pod is running, otherwise we get an error.
retry(15, 1000, || async {
retry(15, Duration::from_millis(1000), || async {
get_running_pod(&pods, &self.id).await
})
.await
Expand Down Expand Up @@ -355,22 +355,19 @@ fn get_cli_args(peers: &[NodeAddr]) -> Vec<String> {
}
}

async fn retry<T, Fut, F>(retries: usize, delay: usize, mut f: F) -> anyhow::Result<T>
async fn retry<T, Fut, F>(retries: usize, delay: Duration, mut f: F) -> anyhow::Result<T>
where
F: FnMut() -> Fut,
Fut: std::future::Future<Output = anyhow::Result<T>>,
{
let delay = std::time::Duration::from_millis(delay.try_into()?);
let mut interval = time::interval(delay);
let mut count = 0;
loop {
let result = f().await;
if result.is_ok() {
return result;
}
interval.tick().await;
count += 1;
if count > retries {
let result = f().await;
if result.is_ok() || count > retries {
return result;
}
std::thread::sleep(delay);
}
}

0 comments on commit 14259dd

Please sign in to comment.