Skip to content

Commit

Permalink
enable retry logic for with_http_client too
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Sep 23, 2024
1 parent 1972776 commit 7a8fc56
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 6 additions & 1 deletion ic-agent/src/agent/agent_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use reqwest::Client;

use crate::{
agent::{NonceFactory, NonceGenerator},
identity::{anonymous::AnonymousIdentity, Identity},
Expand All @@ -16,7 +18,7 @@ pub struct AgentConfig {
/// See [`with_ingress_expiry`](super::AgentBuilder::with_ingress_expiry).
pub ingress_expiry: Option<Duration>,
/// See [`with_http_client`](super::AgentBuilder::with_http_client).
pub client: Option<Arc<dyn HttpService>>,
pub client: Option<Client>,
/// See [`with_route_provider`](super::AgentBuilder::with_route_provider).
pub route_provider: Option<Arc<dyn RouteProvider>>,
/// See [`verify_query_signatures`](super::AgentBuilder::with_verify_query_signatures).
Expand All @@ -27,6 +29,8 @@ pub struct AgentConfig {
pub max_response_body_size: Option<usize>,
/// See [`with_max_tcp_error_retries`](super::AgentBuilder::with_max_tcp_error_retries).
pub max_tcp_error_retries: usize,
/// See [`with_arc_http_middleware`](super::AgentBuilder::with_arc_http_middleware).
pub http_service: Option<Arc<dyn HttpService>>,
}

impl Default for AgentConfig {
Expand All @@ -36,6 +40,7 @@ impl Default for AgentConfig {
identity: Arc::new(AnonymousIdentity {}),
ingress_expiry: None,
client: None,
http_service: None,
verify_query_signatures: true,
max_concurrent_requests: 50,
route_provider: None,
Expand Down
16 changes: 12 additions & 4 deletions ic-agent/src/agent/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,25 @@ impl AgentBuilder {

/// Provide a pre-configured HTTP client to use. Use this to set e.g. HTTP timeouts or proxy configuration.
pub fn with_http_client(mut self, client: reqwest::Client) -> Self {
self.config.client = Some(Arc::new(client));
assert!(
self.config.http_service.is_none(),
"with_arc_http_middleware cannot be called with with_http_client"
);
self.config.client = Some(client);
self
}

/// Provide a custom `reqwest`-compatible HTTP service, e.g. to add per-request headers for custom boundary nodes.
/// Most users will not need this and should use `with_http_client`.
/// Most users will not need this and should use `with_http_client`. Cannot be called with `with_http_client`.
///
/// The trait is automatically implemented for any `tower::Service` impl matching the one `reqwest::Client` uses,
/// including `reqwest-middleware`. Implementations must provide all automatic retry logic.
/// including `reqwest-middleware`. This is a low-level interface, and implementations must provide all automatic retry logic.
pub fn with_arc_http_middleware(mut self, service: Arc<dyn HttpService>) -> Self {
self.config.client = Some(service);
assert!(
self.config.client.is_none(),
"with_arc_http_middleware cannot be called with with_http_client"
);
self.config.http_service = Some(service);
self
}

Expand Down
6 changes: 3 additions & 3 deletions ic-agent/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ impl Agent {
identity: config.identity,
ingress_expiry: config.ingress_expiry.unwrap_or(DEFAULT_INGRESS_EXPIRY),
root_key: Arc::new(RwLock::new(IC_ROOT_KEY.to_vec())),
client: config.client.unwrap_or_else(|| {
client: config.http_service.unwrap_or_else(|| {
Arc::new(DefaultRetryLogic {
_max_tcp_error_retries: config.max_tcp_error_retries,
client: {
client: config.client.unwrap_or_else(|| {
#[cfg(not(target_family = "wasm"))]
{
Client::builder()
Expand All @@ -197,7 +197,7 @@ impl Agent {
{
Client::new()
}
},
}),
})
}),
route_provider: config
Expand Down

0 comments on commit 7a8fc56

Please sign in to comment.