Skip to content

Commit af594f1

Browse files
committed
Allow setting an http timeout
1 parent 20162d2 commit af594f1

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

ic-agent/src/agent/http_transport.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#![cfg(feature = "reqwest")]
33

44
use crate::{agent::agent_error::HttpErrorPayload, ic_types::Principal, AgentError, RequestId};
5-
use reqwest::Method;
6-
use std::{future::Future, pin::Pin, sync::Arc};
5+
use reqwest::{ClientBuilder, Method};
6+
use std::{future::Future, pin::Pin, sync::Arc, time::Duration};
77

88
/// Implemented by the Agent environment to cache and update an HTTP Auth password.
99
/// It returns a tuple of `(username, password)`.
@@ -30,6 +30,25 @@ pub struct ReqwestHttpReplicaV2Transport {
3030

3131
impl ReqwestHttpReplicaV2Transport {
3232
pub fn create<U: Into<String>>(url: U) -> Result<Self, AgentError> {
33+
Self::create_with_modified_http_options(url, |cb| cb)
34+
}
35+
36+
pub fn create_with_timeout<U: Into<String>>(
37+
url: U,
38+
http_timeout: Duration,
39+
) -> Result<Self, AgentError> {
40+
Self::create_with_modified_http_options(url, |cb| cb.timeout(http_timeout))
41+
}
42+
43+
/// This method allows you to create a `ReqwestHttpReplicaV2Transport`
44+
/// with arbitrary http options by using `Reqwest::ClientBuilder`.
45+
pub fn create_with_modified_http_options<
46+
U: Into<String>,
47+
F: FnOnce(ClientBuilder) -> ClientBuilder,
48+
>(
49+
url: U,
50+
modify_http_options: F,
51+
) -> Result<Self, AgentError> {
3352
let mut tls_config = rustls::ClientConfig::new();
3453

3554
// Advertise support for HTTP/2
@@ -45,10 +64,11 @@ impl ReqwestHttpReplicaV2Transport {
4564
url: reqwest::Url::parse(&url)
4665
.and_then(|url| url.join("api/v2/"))
4766
.map_err(|_| AgentError::InvalidReplicaUrl(url.clone()))?,
48-
client: reqwest::Client::builder()
49-
.use_preconfigured_tls(tls_config)
50-
.build()
51-
.expect("Could not create HTTP client."),
67+
client: modify_http_options(
68+
reqwest::Client::builder().use_preconfigured_tls(tls_config),
69+
)
70+
.build()
71+
.expect("Could not create HTTP client."),
5272
password_manager: None,
5373
})
5474
}

0 commit comments

Comments
 (0)