Skip to content

Commit

Permalink
fix: wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay-komarevskiy committed May 29, 2024
1 parent e046887 commit 72f0fff
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions ic-agent/src/agent/http_transport/reqwest_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct ReqwestTransport {
route_provider: Arc<dyn RouteProvider>,
client: Client,
max_response_body_size: Option<usize>,
#[allow(dead_code)]
max_tcp_error_retries: usize,
}

Expand Down Expand Up @@ -95,33 +96,50 @@ impl ReqwestTransport {
endpoint: &str,
body: Option<Vec<u8>>,
) -> Result<(StatusCode, HeaderMap, Vec<u8>), AgentError> {
let mut retry_count = 0;

let response = loop {
// RouteProvider generates urls dynamically. Some of these urls can be potentially unhealthy.
// TCP related errors (host unreachable, connection refused, connection timed out, connection reset) can be safely retried with a newly generated url.
let create_request_with_generated_url = || -> Result<Request, AgentError> {
let url = self.route_provider.route()?.join(endpoint)?;

let mut http_request = Request::new(method.clone(), url);

http_request
.headers_mut()
.insert(CONTENT_TYPE, "application/cbor".parse().unwrap());

*http_request.body_mut() = body.as_ref().cloned().map(Body::from);
Ok(http_request)
};

match self.client.execute(http_request).await {
Ok(response) => break response,
Err(err) => {
// Network-related errors can be retried.
if err.is_connect() {
if retry_count >= self.max_tcp_error_retries {
// Dispatch request with a retry logic only for non-wasm builds.
let response = {
#[cfg(target_family = "wasm")]
{
let http_request = create_request_with_generated_url()?;
match self.client.execute(http_request).await {
Ok(response) => response,
Err(err) => return Err(AgentError::TransportError(Box::new(err))),
}
}
#[cfg(not(target_family = "wasm"))]
{
// RouteProvider generates urls dynamically. Some of these urls can be potentially unhealthy.
// TCP related errors (host unreachable, connection refused, connection timed out, connection reset) can be safely retried with a newly generated url.

let mut retry_count = 0;

loop {
let http_request = create_request_with_generated_url()?;

match self.client.execute(http_request).await {
Ok(response) => break response,
Err(err) => {
// Network-related errors can be retried.
if err.is_connect() {
if retry_count >= self.max_tcp_error_retries {
return Err(AgentError::TransportError(Box::new(err)));
}
retry_count += 1;
continue;
}
return Err(AgentError::TransportError(Box::new(err)));
}
retry_count += 1;
continue;
}
return Err(AgentError::TransportError(Box::new(err)));
}
}
};
Expand Down

0 comments on commit 72f0fff

Please sign in to comment.