From c8a19c0965699f069d94c48e6b8c511830705359 Mon Sep 17 00:00:00 2001 From: Vladimir Petrzhikovskii Date: Tue, 27 Aug 2024 21:35:07 +0200 Subject: [PATCH] fix endpoint selection in case when all endpoints are dead --- client/src/lib.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index e5dfaad..526af98 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -38,7 +38,7 @@ pub enum RpcClient { } impl RpcClient { - pub async fn new + Send + Clone>( + pub async fn new + Send + Clone>( endpoints: I, options: ClientOptions, ) -> Result { @@ -265,7 +265,7 @@ pub trait Client: Send + Sync + Sized where T: Connection + Ord + Clone + 'static, { - async fn new + Send>( + async fn new + Send>( endpoints: I, options: ClientOptions, ) -> Result { @@ -285,7 +285,7 @@ where } /// `endpoints` - full URLs of the RPC endpoints. - async fn with_client + Send>( + async fn with_client + Send>( client: reqwest::Client, endpoints: I, options: ClientOptions, @@ -602,7 +602,7 @@ impl<'a, T: Serialize + Send + Sync> RpcRequest<'a, T> { pub enum RpcResponse where - for<'de> D: Deserialize<'de>, + for<'de> D: Deserialize<'de>, { JRPC(Answer), PROTO(Answer), @@ -708,6 +708,9 @@ impl ChooseStrategy { fn choose(&self, endpoints: &[T]) -> Option { use rand::prelude::SliceRandom; + if endpoints.is_empty() { + return None; + } match self { ChooseStrategy::Random => endpoints.choose(&mut rand::thread_rng()).cloned(), ChooseStrategy::RoundRobin => {