Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instant response when no endpoints available #151

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/erc20_rpc_pool/src/rpc_pool/eth_generic_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ impl Web3RpcPool {
let mut loop_no = 0;
const LOOP_COUNT: usize = 4;
loop {
let idx_vec = self.clone().choose_best_endpoints().await;
let resp = self.clone().choose_best_endpoints().await;
if resp.allowed_endpoints.is_empty() && !resp.is_resolving {
log::warn!("No valid endpoints found for chain id {}, wait until next check. Call yagna payment driver rpc --verify for details", self.chain_id);
return Err(web3::Error::Unreachable);
}
let idx_vec = resp.allowed_endpoints;
if let Some(idx_chosen) = idx_vec.first() {
self.mark_rpc_chosen(*idx_chosen);
}
Expand Down
27 changes: 22 additions & 5 deletions crates/erc20_rpc_pool/src/rpc_pool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ pub async fn resolve_txt_record_to_string_array(record: &str) -> std::io::Result
.collect::<Vec<_>>())
}

pub struct ChooseBestEndpointsResult {
pub allowed_endpoints: Vec<Index>,
pub is_resolving: bool,
}

impl Web3RpcPool {
pub fn new(
chain_id: u64,
Expand Down Expand Up @@ -327,11 +332,14 @@ impl Web3RpcPool {
});
}

pub async fn choose_best_endpoints(self: Arc<Self>) -> Vec<Index> {
self.external_sources_resolver
pub async fn choose_best_endpoints(self: Arc<Self>) -> ChooseBestEndpointsResult {
let task = self
.external_sources_resolver
.clone()
.start_resolve_if_needed(self.clone(), false);

let is_resolving = task.is_some();

let endpoints_copy = self
.endpoints
.try_lock_for(Duration::from_secs(5))
Expand Down Expand Up @@ -379,7 +387,10 @@ impl Web3RpcPool {
.clone()
.start_verify_if_needed(self.clone(), false);

allowed_endpoints
ChooseBestEndpointsResult {
allowed_endpoints,
is_resolving,
}
} else {
let self_cloned = self.clone();
self_cloned
Expand Down Expand Up @@ -408,7 +419,10 @@ impl Web3RpcPool {
})
.map(|(idx, _element)| idx)
{
return vec![el];
return ChooseBestEndpointsResult {
allowed_endpoints: vec![el],
is_resolving,
};
}

if is_finished {
Expand All @@ -417,7 +431,10 @@ impl Web3RpcPool {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
//no endpoint could be selected
vec![]
ChooseBestEndpointsResult {
allowed_endpoints: vec![],
is_resolving,
}
}
}

Expand Down
Loading