Skip to content

Commit

Permalink
fix endpoint selection in case when all endpoints are dead
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdeafbeef committed Aug 27, 2024
1 parent 274aab7 commit c8a19c0
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum RpcClient {
}

impl RpcClient {
pub async fn new<I: IntoIterator<Item = Url> + Send + Clone>(
pub async fn new<I: IntoIterator<Item=Url> + Send + Clone>(
endpoints: I,
options: ClientOptions,
) -> Result<Self> {
Expand Down Expand Up @@ -265,7 +265,7 @@ pub trait Client<T>: Send + Sync + Sized
where
T: Connection + Ord + Clone + 'static,
{
async fn new<I: IntoIterator<Item = Url> + Send>(
async fn new<I: IntoIterator<Item=Url> + Send>(
endpoints: I,
options: ClientOptions,
) -> Result<Self> {
Expand All @@ -285,7 +285,7 @@ where
}

/// `endpoints` - full URLs of the RPC endpoints.
async fn with_client<I: IntoIterator<Item = Url> + Send>(
async fn with_client<I: IntoIterator<Item=Url> + Send>(
client: reqwest::Client,
endpoints: I,
options: ClientOptions,
Expand Down Expand Up @@ -602,7 +602,7 @@ impl<'a, T: Serialize + Send + Sync> RpcRequest<'a, T> {

pub enum RpcResponse<D>
where
for<'de> D: Deserialize<'de>,
for<'de> D: Deserialize<'de>,
{
JRPC(Answer<D>),
PROTO(Answer<rpc::Response>),
Expand Down Expand Up @@ -708,6 +708,9 @@ impl ChooseStrategy {
fn choose<T: Connection + Ord + Clone>(&self, endpoints: &[T]) -> Option<T> {
use rand::prelude::SliceRandom;

if endpoints.is_empty() {
return None;
}
match self {
ChooseStrategy::Random => endpoints.choose(&mut rand::thread_rng()).cloned(),
ChooseStrategy::RoundRobin => {
Expand Down

0 comments on commit c8a19c0

Please sign in to comment.