Skip to content

Commit

Permalink
pub getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Apr 5, 2024
1 parent c78155c commit 4f12771
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/extensions/client/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::{
};

pub struct Endpoint {
pub url: String,
pub health: Arc<Health>,
url: String,
health: Arc<Health>,
client_rx: tokio::sync::watch::Receiver<Option<Arc<Client>>>,
on_client_ready: Arc<tokio::sync::Notify>,
background_tasks: Vec<tokio::task::JoinHandle<()>>,
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/extensions/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 4f12771

Please sign in to comment.