Skip to content

Commit

Permalink
feat(retry): enable request retries on wasm, too
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Sep 27, 2023
1 parent ec9e6e3 commit 1f25aa3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
4 changes: 2 additions & 2 deletions crates/oro-client/Cargo.toml
Expand Up @@ -21,6 +21,8 @@ indexmap = { workspace = true }
miette = { workspace = true }
percent-encoding = { workspace = true }
reqwest = { workspace = true, features = ["json", "gzip", "stream"] }
reqwest-middleware = { workspace = true }
reqwest-retry = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
Expand All @@ -29,8 +31,6 @@ url = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
http-cache-reqwest = { workspace = true }
reqwest-middleware = { workspace = true }
reqwest-retry = { workspace = true }

[dev-dependencies]
async-std = { workspace = true, features = ["attributes", "tokio1"] }
Expand Down
67 changes: 29 additions & 38 deletions crates/oro-client/src/client.rs
Expand Up @@ -4,14 +4,13 @@ use std::sync::Arc;

#[cfg(not(target_arch = "wasm32"))]
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache};
#[cfg(target_arch = "wasm32")]
use reqwest::Client;
#[cfg(not(target_arch = "wasm32"))]
use reqwest::ClientBuilder;
#[cfg(not(target_arch = "wasm32"))]
use reqwest::{NoProxy, Proxy};
#[cfg(not(target_arch = "wasm32"))]
use reqwest_middleware::ClientWithMiddleware;
#[cfg(not(target_arch = "wasm32"))]
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use url::Url;

Expand All @@ -21,6 +20,7 @@ use crate::OroClientError;
#[derive(Clone, Debug)]
pub struct OroClientBuilder {
registry: Url,
fetch_retries: u32,
#[cfg(not(target_arch = "wasm32"))]
cache: Option<PathBuf>,
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -29,8 +29,6 @@ pub struct OroClientBuilder {
proxy_url: Option<Proxy>,
#[cfg(not(target_arch = "wasm32"))]
no_proxy_domain: Option<String>,
#[cfg(not(target_arch = "wasm32"))]
fetch_retries: u32,
}

impl Default for OroClientBuilder {
Expand All @@ -45,9 +43,9 @@ impl Default for OroClientBuilder {
proxy_url: None,
#[cfg(not(target_arch = "wasm32"))]
no_proxy_domain: None,
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
#[cfg(not(test))]
fetch_retries: 2,
#[cfg(all(not(target_arch = "wasm32"), test))]
#[cfg(test)]
fetch_retries: 0,
}
}
Expand Down Expand Up @@ -110,35 +108,32 @@ impl OroClientBuilder {

pub fn build(self) -> OroClient {
#[cfg(target_arch = "wasm32")]
let client_uncached = Client::new();
let client_raw = Client::new();

#[cfg(not(target_arch = "wasm32"))]
let mut client_core = ClientBuilder::new()
.user_agent("orogene")
.pool_max_idle_per_host(20)
.timeout(std::time::Duration::from_secs(60 * 5));
let client_raw = {
let mut client_core = ClientBuilder::new()
.user_agent("orogene")
.pool_max_idle_per_host(20)
.timeout(std::time::Duration::from_secs(60 * 5));

if let Some(url) = self.proxy_url {
client_core = client_core.proxy(url);
}

#[cfg(not(target_arch = "wasm32"))]
if let Some(url) = self.proxy_url {
client_core = client_core.proxy(url);
}
if !self.proxy {
client_core = client_core.no_proxy();
}

#[cfg(not(target_arch = "wasm32"))]
if !self.proxy {
client_core = client_core.no_proxy();
}
client_core.build().expect("Fail to build HTTP client.")
};

#[cfg(not(target_arch = "wasm32"))]
let client_uncached = client_core.build().expect("Fail to build HTTP client.");

#[cfg(not(target_arch = "wasm32"))]
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(self.fetch_retries);
#[cfg(not(target_arch = "wasm32"))]
let retry_strategy = RetryTransientMiddleware::new_with_policy(retry_policy);

#[cfg(not(target_arch = "wasm32"))]
#[allow(unused_mut)]
let mut client_builder =
reqwest_middleware::ClientBuilder::new(client_uncached.clone()).with(retry_strategy);
reqwest_middleware::ClientBuilder::new(client_raw.clone()).with(retry_strategy);

#[cfg(not(target_arch = "wasm32"))]
if let Some(cache_loc) = self.cache {
Expand All @@ -151,14 +146,16 @@ impl OroClientBuilder {
}));
}

let retry_policy = ExponentialBackoff::builder().build_with_max_retries(self.fetch_retries);
let retry_strategy = RetryTransientMiddleware::new_with_policy(retry_policy);

let client_uncached_builder =
reqwest_middleware::ClientBuilder::new(client_raw).with(retry_strategy);

OroClient {
registry: Arc::new(self.registry),
#[cfg(not(target_arch = "wasm32"))]
client: client_builder.build(),
// wasm client is never cached
#[cfg(target_arch = "wasm32")]
client: client_uncached.clone(),
client_uncached,
client_uncached: client_uncached_builder.build(),
}
}

Expand All @@ -177,14 +174,8 @@ impl OroClientBuilder {
#[derive(Clone, Debug)]
pub struct OroClient {
pub(crate) registry: Arc<Url>,
#[cfg(not(target_arch = "wasm32"))]
pub(crate) client: ClientWithMiddleware,
#[cfg(not(target_arch = "wasm32"))]
pub(crate) client_uncached: Client,
#[cfg(target_arch = "wasm32")]
pub(crate) client: Client,
#[cfg(target_arch = "wasm32")]
pub(crate) client_uncached: Client,
pub(crate) client_uncached: ClientWithMiddleware,
}

impl OroClient {
Expand Down
1 change: 0 additions & 1 deletion crates/oro-client/src/error.rs
Expand Up @@ -65,7 +65,6 @@ pub enum OroClientError {

/// A generic request middleware error happened while making a request.
/// Refer to the error message for more details.
#[cfg(not(target_arch = "wasm32"))]
#[error(transparent)]
#[diagnostic(code(oro_client::request_middleware_error), url(docsrs))]
RequestMiddlewareError(#[from] reqwest_middleware::Error),
Expand Down

0 comments on commit 1f25aa3

Please sign in to comment.