renamed the function and added doc to make it intelligible

This commit is contained in:
vicanso 2024-07-28 09:46:35 +08:00
parent e6c2af0e77
commit 6a09b52c5e
2 changed files with 19 additions and 26 deletions

View file

@ -26,10 +26,11 @@ use std::time::Duration;
#[async_trait]
pub trait HealthObserve {
/// This function is called When health status changes
async fn health_check_callback(&self, _target: &Backend, _healthy: bool);
/// Observes the health of a [Backend], can be used for monitoring purposes.
async fn observe(&self, target: &Backend, healthy: bool);
}
pub type HealthCheckCallback = Box<dyn HealthObserve + Send + Sync>;
/// Provided to a [HealthCheck] to observe changes to [Backend] health.
pub type HealthObserveCallback = Box<dyn HealthObserve + Send + Sync>;
/// [HealthCheck] is the interface to implement health check for backends
#[async_trait]
@ -39,7 +40,7 @@ pub trait HealthCheck {
/// `Ok(())`` if the check passes, otherwise the check fails.
async fn check(&self, target: &Backend) -> Result<()>;
/// This function is called When health status changes
/// Called when the health changes for a [Backend].
async fn health_status_change(&self, _target: &Backend, _healthy: bool) {}
/// This function defines how many *consecutive* checks should flip the health of a backend.
@ -67,7 +68,8 @@ pub struct TcpHealthCheck {
/// set, it will also try to establish a TLS connection on top of the TCP connection.
pub peer_template: BasicPeer,
connector: TransportConnector,
callback: Option<HealthCheckCallback>,
/// A callback that is invoked when the `healthy` status changes for a [Backend].
pub health_changed_callback: Option<HealthObserveCallback>,
}
impl Default for TcpHealthCheck {
@ -79,7 +81,7 @@ impl Default for TcpHealthCheck {
consecutive_failure: 1,
peer_template,
connector: TransportConnector::new(None),
callback: None,
health_changed_callback: None,
}
}
}
@ -106,11 +108,6 @@ impl TcpHealthCheck {
pub fn set_connector(&mut self, connector: TransportConnector) {
self.connector = connector;
}
/// Set the health observe callback function.
pub fn set_callback(&mut self, callback: HealthCheckCallback) {
self.callback = Some(callback);
}
}
#[async_trait]
@ -130,8 +127,8 @@ impl HealthCheck for TcpHealthCheck {
}
async fn health_status_change(&self, target: &Backend, healthy: bool) {
if let Some(callback) = &self.callback {
callback.health_check_callback(target, healthy).await;
if let Some(callback) = &self.health_changed_callback {
callback.observe(target, healthy).await;
}
}
}
@ -171,7 +168,8 @@ pub struct HttpHealthCheck {
/// Sometimes the health check endpoint lives one a different port than the actual backend.
/// Setting this option allows the health check to perform on the given port of the backend IP.
pub port_override: Option<u16>,
callback: Option<HealthCheckCallback>,
/// A callback that is invoked when the `healthy` status changes for a [Backend].
pub health_changed_callback: Option<HealthObserveCallback>,
}
impl HttpHealthCheck {
@ -199,7 +197,7 @@ impl HttpHealthCheck {
req,
validator: None,
port_override: None,
callback: None,
health_changed_callback: None,
}
}
@ -207,10 +205,6 @@ impl HttpHealthCheck {
pub fn set_connector(&mut self, connector: HttpConnector) {
self.connector = connector;
}
/// Set the health observe callback function.
pub fn set_callback(&mut self, callback: HealthCheckCallback) {
self.callback = Some(callback);
}
}
#[async_trait]
@ -266,8 +260,8 @@ impl HealthCheck for HttpHealthCheck {
Ok(())
}
async fn health_status_change(&self, target: &Backend, healthy: bool) {
if let Some(callback) = &self.callback {
callback.health_check_callback(target, healthy).await;
if let Some(callback) = &self.health_changed_callback {
callback.observe(target, healthy).await;
}
}
}
@ -430,7 +424,7 @@ mod test {
}
#[async_trait]
impl HealthObserve for Observe {
async fn health_check_callback(&self, _target: &Backend, healthy: bool) {
async fn observe(&self, _target: &Backend, healthy: bool) {
if !healthy {
self.unhealthy_count.fetch_add(1, Ordering::Relaxed);
}
@ -453,7 +447,7 @@ mod test {
};
let bob = Box::new(ob);
let mut tcp_check = TcpHealthCheck::default();
tcp_check.set_callback(bob);
tcp_check.health_changed_callback = Some(bob);
let discovery = discovery::Static::default();
let mut backends = Backends::new(Box::new(discovery));
@ -479,7 +473,7 @@ mod test {
let bob = Box::new(ob);
let mut https_check = HttpHealthCheck::new("one.one.one.one", true);
https_check.set_callback(bob);
https_check.health_changed_callback = Some(bob);
let discovery = discovery::Static::default();
let mut backends = Backends::new(Box::new(discovery));

View file

@ -225,11 +225,10 @@ impl Backends {
let flipped =
h.observe_health(errored.is_none(), check.health_threshold(errored.is_none()));
if flipped {
check.health_status_change(backend, errored.is_none()).await;
if let Some(e) = errored {
check.health_status_change(backend, false).await;
warn!("{backend:?} becomes unhealthy, {e}");
} else {
check.health_status_change(backend, true).await;
info!("{backend:?} becomes healthy");
}
}