feat: retry client wasm support (#1877)

* wasm support for retry client

* fix conection err check

* fix lockfile

* remove unused comment
This commit is contained in:
Noah Citron 2022-11-22 16:16:23 -05:00 committed by GitHub
parent 6b5f130ba2
commit fa0ce0c750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -38,9 +38,7 @@ pub use quorum::{JsonRpcClientWrapper, Quorum, QuorumError, QuorumProvider, Weig
mod rw; mod rw;
pub use rw::{RwClient, RwClientError}; pub use rw::{RwClient, RwClientError};
#[cfg(not(target_arch = "wasm32"))]
mod retry; mod retry;
#[cfg(not(target_arch = "wasm32"))]
pub use retry::*; pub use retry::*;
mod mock; mod mock;

View File

@ -206,6 +206,7 @@ pub enum RetryClientError {
TimeoutError, TimeoutError,
#[error(transparent)] #[error(transparent)]
SerdeJson(serde_json::Error), SerdeJson(serde_json::Error),
TimerError,
} }
impl std::fmt::Display for RetryClientError { impl std::fmt::Display for RetryClientError {
@ -220,11 +221,13 @@ impl From<RetryClientError> for ProviderError {
RetryClientError::ProviderError(err) => err, RetryClientError::ProviderError(err) => err,
RetryClientError::TimeoutError => ProviderError::JsonRpcClientError(Box::new(src)), RetryClientError::TimeoutError => ProviderError::JsonRpcClientError(Box::new(src)),
RetryClientError::SerdeJson(err) => err.into(), RetryClientError::SerdeJson(err) => err.into(),
RetryClientError::TimerError => ProviderError::JsonRpcClientError(Box::new(src)),
} }
} }
} }
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<T> JsonRpcClient for RetryClient<T> impl<T> JsonRpcClient for RetryClient<T>
where where
T: JsonRpcClient + 'static, T: JsonRpcClient + 'static,
@ -310,6 +313,13 @@ where
next_backoff += Duration::from_secs(seconds_to_wait_for_compute_budget); next_backoff += Duration::from_secs(seconds_to_wait_for_compute_budget);
trace!("retrying and backing off for {:?}", next_backoff); trace!("retrying and backing off for {:?}", next_backoff);
#[cfg(target_arch = "wasm32")]
wasm_timer::Delay::new(next_backoff)
.await
.map_err(|_| RetryClientError::TimerError)?;
#[cfg(not(target_arch = "wasm32"))]
tokio::time::sleep(next_backoff).await; tokio::time::sleep(next_backoff).await;
} else { } else {
let err: ProviderError = err.into(); let err: ProviderError = err.into();
@ -413,9 +423,15 @@ fn compute_unit_offset_in_secs(
/// `request::Error::TimedOut` /// `request::Error::TimedOut`
fn maybe_connectivity(err: &ProviderError) -> bool { fn maybe_connectivity(err: &ProviderError) -> bool {
if let ProviderError::HTTPError(reqwest_err) = err { if let ProviderError::HTTPError(reqwest_err) = err {
if reqwest_err.is_timeout() || reqwest_err.is_connect() { if reqwest_err.is_timeout() {
return true return true
} }
#[cfg(not(target_arch = "wasm32"))]
if reqwest_err.is_connect() {
return true
}
// Error HTTP codes (5xx) are considered connectivity issues and will prompt retry // Error HTTP codes (5xx) are considered connectivity issues and will prompt retry
if let Some(status) = reqwest_err.status() { if let Some(status) = reqwest_err.status() {
let code = status.as_u16(); let code = status.as_u16();