From 72f0fff4692f9ce1a8a709f5aba7354841d481b4 Mon Sep 17 00:00:00 2001 From: Nikolay Komarevskiy Date: Wed, 29 May 2024 09:50:04 +0200 Subject: [PATCH] fix: wasm build --- .../agent/http_transport/reqwest_transport.rs | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/ic-agent/src/agent/http_transport/reqwest_transport.rs b/ic-agent/src/agent/http_transport/reqwest_transport.rs index 7bcdad91..3823f90d 100644 --- a/ic-agent/src/agent/http_transport/reqwest_transport.rs +++ b/ic-agent/src/agent/http_transport/reqwest_transport.rs @@ -28,6 +28,7 @@ pub struct ReqwestTransport { route_provider: Arc, client: Client, max_response_body_size: Option, + #[allow(dead_code)] max_tcp_error_retries: usize, } @@ -95,33 +96,50 @@ impl ReqwestTransport { endpoint: &str, body: Option>, ) -> Result<(StatusCode, HeaderMap, Vec), 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 { 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))); } } };