Skip to content

Commit a56ccd4

Browse files
feat: add with_max_polling_time for ic-agent (#604)
1 parent 4642e30 commit a56ccd4

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## Unreleased
1010

11+
* Added `AgentBuilder::with_max_polling_time` to config the maximum time to wait for a response from the replica.
12+
1113
## [0.38.2] - 2024-09-30
1214

1315
* Limited the number of HTTP 429 retries. Users receiving this error should configure `with_max_concurrent_requests`.

ic-agent/src/agent/agent_config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct AgentConfig {
3131
pub max_tcp_error_retries: usize,
3232
/// See [`with_arc_http_middleware`](super::AgentBuilder::with_arc_http_middleware).
3333
pub http_service: Option<Arc<dyn HttpService>>,
34+
/// See [`with_max_polling_time`](super::AgentBuilder::with_max_polling_time).
35+
pub max_polling_time: Duration,
3436
}
3537

3638
impl Default for AgentConfig {
@@ -46,6 +48,7 @@ impl Default for AgentConfig {
4648
route_provider: None,
4749
max_response_body_size: None,
4850
max_tcp_error_retries: 0,
51+
max_polling_time: Duration::from_secs(60 * 5),
4952
}
5053
}
5154
}

ic-agent/src/agent/builder.rs

+5
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,9 @@ impl AgentBuilder {
164164
self.config.max_response_body_size = Some(max_size);
165165
self
166166
}
167+
/// Set the maximum time to wait for a response from the replica.
168+
pub fn with_max_polling_time(mut self, max_polling_time: std::time::Duration) -> Self {
169+
self.config.max_polling_time = max_polling_time;
170+
self
171+
}
167172
}

ic-agent/src/agent/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ pub struct Agent {
156156
concurrent_requests_semaphore: Arc<Semaphore>,
157157
verify_query_signatures: bool,
158158
max_response_body_size: Option<usize>,
159+
max_polling_time: Duration,
159160
#[allow(dead_code)]
160161
max_tcp_error_retries: usize,
161162
}
@@ -208,6 +209,7 @@ impl Agent {
208209
concurrent_requests_semaphore: Arc::new(Semaphore::new(config.max_concurrent_requests)),
209210
max_response_body_size: config.max_response_body_size,
210211
max_tcp_error_retries: config.max_tcp_error_retries,
212+
max_polling_time: config.max_polling_time,
211213
})
212214
}
213215

@@ -615,12 +617,12 @@ impl Agent {
615617
})
616618
}
617619

618-
fn get_retry_policy() -> ExponentialBackoff<SystemClock> {
620+
fn get_retry_policy(&self) -> ExponentialBackoff<SystemClock> {
619621
ExponentialBackoffBuilder::new()
620622
.with_initial_interval(Duration::from_millis(500))
621623
.with_max_interval(Duration::from_secs(1))
622624
.with_multiplier(1.4)
623-
.with_max_elapsed_time(Some(Duration::from_secs(60 * 5)))
625+
.with_max_elapsed_time(Some(self.max_polling_time))
624626
.build()
625627
}
626628

@@ -631,7 +633,7 @@ impl Agent {
631633
effective_canister_id: Principal,
632634
signed_request_status: Vec<u8>,
633635
) -> Result<Vec<u8>, AgentError> {
634-
let mut retry_policy = Self::get_retry_policy();
636+
let mut retry_policy = self.get_retry_policy();
635637

636638
let mut request_accepted = false;
637639
loop {
@@ -679,7 +681,7 @@ impl Agent {
679681
request_id: &RequestId,
680682
effective_canister_id: Principal,
681683
) -> Result<Vec<u8>, AgentError> {
682-
let mut retry_policy = Self::get_retry_policy();
684+
let mut retry_policy = self.get_retry_policy();
683685

684686
let mut request_accepted = false;
685687
loop {

0 commit comments

Comments
 (0)