Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: add Client::wait_until_healthy method #855

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ pub use transport::websocket::{WebSocketClient, WebSocketClientDriver, WebSocket

use crate::endpoint::validators::DEFAULT_VALIDATORS_PER_PAGE;
use crate::endpoint::*;
use crate::error::Error;
use crate::paging::Paging;
use crate::query::Query;
use crate::{Order, Result, SimpleRequest};
use async_trait::async_trait;
use std::time::Duration;
use tendermint::abci::{self, Transaction};
use tendermint::block::Height;
use tendermint::evidence::Evidence;
use tendermint::Genesis;
use tokio::time;

/// Provides lightweight access to the Tendermint RPC. It gives access to all
/// endpoints with the exception of the event subscription-related ones.
Expand Down Expand Up @@ -221,6 +224,31 @@ pub trait Client {
.await
}

/// Poll the `/health` endpoint until it returns a successful result or
/// the given `timeout` has elapsed.
async fn wait_until_healthy<T>(&self, timeout: T) -> Result<()>
where
T: Into<Duration> + Send,
{
let timeout = timeout.into();
let poll_interval = Duration::from_millis(200);
let mut attempts_remaining = timeout.as_millis() / poll_interval.as_millis();

while self.health().await.is_err() {
if attempts_remaining == 0 {
return Err(Error::client_internal_error(format!(
"timed out waiting for healthy response after {}ms",
timeout.as_millis()
)));
}

attempts_remaining -= 1;
time::sleep(poll_interval).await;
}

Ok(())
}

/// Perform a request against the RPC endpoint
async fn perform<R>(&self, request: R) -> Result<R::Response>
where
Expand Down