From ef1c52421ebedc3594415814e9525f21f14bd922 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Wed, 10 Apr 2024 18:20:11 +0200 Subject: [PATCH] Await healthy endpoint (#158) --- src/extensions/client/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/extensions/client/mod.rs b/src/extensions/client/mod.rs index e016467..0e20ffb 100644 --- a/src/extensions/client/mod.rs +++ b/src/extensions/client/mod.rs @@ -5,6 +5,7 @@ use std::{ use anyhow::anyhow; use async_trait::async_trait; +use futures::FutureExt as Boxed; use jsonrpsee::core::{client::Subscription, Error, JsonValue}; use opentelemetry::trace::FutureExt; use rand::{seq::SliceRandom, thread_rng}; @@ -204,13 +205,12 @@ impl Client { if let Some(exclude) = exclude { endpoints.retain(|e| e.url() != exclude.url()); } + // wait for at least one endpoint to connect + futures::future::select_all(endpoints.iter().map(|x| x.connected().boxed())).await; // Sort by health score endpoints.sort_by_key(|endpoint| std::cmp::Reverse(endpoint.health().score())); // Pick the first one - let selected_endpoint = endpoints[0].clone(); - // Ensure it's connected - selected_endpoint.connected().await; - selected_endpoint + endpoints[0].clone() }; let mut selected_endpoint = healthiest_endpoint(None).await;