From 4f127713ce55e17aceee1b5facae1d004ab8b217 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Fri, 5 Apr 2024 19:50:30 +0200 Subject: [PATCH] pub getter --- src/extensions/client/endpoint.rs | 12 ++++++++++-- src/extensions/client/mod.rs | 10 +++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/extensions/client/endpoint.rs b/src/extensions/client/endpoint.rs index 8167f98..8ea351f 100644 --- a/src/extensions/client/endpoint.rs +++ b/src/extensions/client/endpoint.rs @@ -17,8 +17,8 @@ use std::{ }; pub struct Endpoint { - pub url: String, - pub health: Arc, + url: String, + health: Arc, client_rx: tokio::sync::watch::Receiver>>, on_client_ready: Arc, background_tasks: Vec>, @@ -94,6 +94,14 @@ impl Endpoint { } } + pub fn url(&self) -> &str { + &self.url + } + + pub fn health(&self) -> &Health { + self.health.as_ref() + } + pub async fn connected(&self) { if self.client_rx.borrow().is_some() { return; diff --git a/src/extensions/client/mod.rs b/src/extensions/client/mod.rs index 646ddee..08c6523 100644 --- a/src/extensions/client/mod.rs +++ b/src/extensions/client/mod.rs @@ -200,10 +200,10 @@ impl Client { let mut endpoints = endpoints.clone(); // Remove the current endpoint from the list if let Some(exclude) = exclude { - endpoints.retain(|e| e.url != exclude.url); + endpoints.retain(|e| e.url() != exclude.url()); } // Sort by health score - endpoints.sort_by(|a, b| b.health.score().cmp(&a.health.score())); + endpoints.sort_by(|a, b| b.health().score().cmp(&a.health().score())); // Pick the first one let selected_endpoint = endpoints[0].clone(); // Ensure it's connected @@ -363,12 +363,12 @@ impl Client { loop { tokio::select! { - _ = selected_endpoint.health.unhealthy() => { + _ = selected_endpoint.health().unhealthy() => { // Current selected endpoint is unhealthy, try to rotate to another one. // In case of all endpoints are unhealthy, we don't want to keep rotating but stick with the healthiest one. let new_selected_endpoint = healthiest_endpoint(None).await; - if new_selected_endpoint.url != selected_endpoint.url { - tracing::warn!("Switch to endpoint: {new_url}", new_url=new_selected_endpoint.url); + if new_selected_endpoint.url() != selected_endpoint.url() { + tracing::warn!("Switch to endpoint: {new_url}", new_url=new_selected_endpoint.url()); selected_endpoint = new_selected_endpoint; rotation_notify_bg.notify_waiters(); }