Skip to content

Commit b4a9cb0

Browse files
committed
fix: avoid division by zero when no endpoint is passed
1 parent 371ab98 commit b4a9cb0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/agent/utils/rpc_multi_client.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ impl RpcMultiClient {
7373
where
7474
F: Fn(&'a RpcClient) -> Pin<Box<dyn Future<Output = anyhow::Result<T>> + Send + 'a>>,
7575
{
76+
if self.rpc_clients.is_empty() {
77+
bail!("No RPC clients available for operation: {}", operation_name);
78+
}
79+
7680
let mut attempts = 0;
7781
// Try all endpoints twice in the worst case.
7882
let max_attempts = self.rpc_clients.len() * 2;
@@ -134,7 +138,8 @@ impl RpcMultiClient {
134138
let mut found_index = None;
135139
for _ in 0..state.endpoint_states.len() {
136140
let index = state.current_index;
137-
state.current_index = (state.current_index + 1) % state.endpoint_states.len();
141+
state.current_index =
142+
(state.current_index + 1).checked_rem(state.endpoint_states.len())?;
138143

139144
// Choose the next endpoint that is either healthy or has waited out the cooldown period.
140145
#[allow(clippy::indexing_slicing, reason = "index is checked")]
@@ -152,7 +157,7 @@ impl RpcMultiClient {
152157
// If all endpoints have failed, simply move on to the next one.
153158
if found_index.is_none() {
154159
let index = start_index;
155-
state.current_index = (start_index + 1) % state.endpoint_states.len();
160+
state.current_index = (start_index + 1).checked_rem(state.endpoint_states.len())?;
156161
found_index = Some(index);
157162
}
158163
found_index

0 commit comments

Comments
 (0)