-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support observe backend health status #225 #325
Conversation
@eaufavor |
#[async_trait] | ||
pub trait HealthObserve { | ||
/// This function is called When health status changes | ||
async fn health_check_callback(&self, _target: &Backend, _healthy: bool); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind removing the underscores in the parameters?
@@ -24,13 +24,24 @@ use pingora_http::{RequestHeader, ResponseHeader}; | |||
use std::sync::Arc; | |||
use std::time::Duration; | |||
|
|||
#[async_trait] | |||
pub trait HealthObserve { | |||
/// This function is called When health status changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the doc comment to be /// Observes the health of a [Backend], can be used for monitoring purposes.
.
#[async_trait] | ||
pub trait HealthObserve { | ||
/// This function is called When health status changes | ||
async fn health_check_callback(&self, _target: &Backend, _healthy: bool); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename this function from health_check_callback
to observe
.
/// [HealthCheck] is the interface to implement health check for backends | ||
#[async_trait] | ||
pub trait HealthCheck { | ||
/// Check the given backend. | ||
/// | ||
/// `Ok(())`` if the check passes, otherwise the check fails. | ||
async fn check(&self, target: &Backend) -> Result<()>; | ||
|
||
/// This function is called When health status changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update doc comment to /// Called when the health changes for a [Backend].
.
@@ -56,6 +67,7 @@ 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>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be pub
and renamed to health_changed_callback
. Doc comment can be /// A callback that is invoked when the `healthy` status changes for a [Backend].
@@ -93,6 +106,11 @@ 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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove this if we change the callback to be pub
.
} | ||
} | ||
|
||
/// Replace the internal http connector with the given [HttpConnector] | ||
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove this if we change the callback
to be pub
.
@@ -147,6 +171,7 @@ 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>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be pub
and renamed to health_changed_callback
. Doc comment can be /// A callback that is invoked when the `healthy` status changes for a [Backend].
..
/// This function is called When health status changes | ||
async fn health_check_callback(&self, _target: &Backend, _healthy: bool); | ||
} | ||
pub type HealthCheckCallback = Box<dyn HealthObserve + Send + Sync>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename this to HealthObserveCallback
and add doc comment /// Provided to a [HealthCheck] to observe changes to [Backend] health.
pingora-load-balancing/src/lib.rs
Outdated
@@ -226,8 +226,10 @@ impl Backends { | |||
h.observe_health(errored.is_none(), check.health_threshold(errored.is_none())); | |||
if flipped { | |||
if let Some(e) = errored { | |||
check.health_status_change(backend, false).await; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check.health_status_change(backend, errored.is_none()).await;
before the if let
check, then we can remove the redundant call.
@andrewhavck I have modified the code, please review. |
@vicanso thanks for addressing the review comments, looks like there are some merge conflicts now? Once those are resolved I'll get this in internal review. |
@andrewhavck Is any adjustment needed? |
--- test: add test for upstream health observe --- renamed the function and added doc to make it intelligible --- fix clippy error --- Merge branch 'main' into main --- test: fix test for backend do update Co-authored-by: Tree Xie <tree.xie@outlook.com> Includes-commit: 1421c26 Includes-commit: 695d549 Includes-commit: 6a09b52 Includes-commit: 72d6ee0 Includes-commit: e6c2af0 Includes-commit: fb62869 Replicated-from: #325
--- test: add test for upstream health observe --- renamed the function and added doc to make it intelligible --- fix clippy error --- Merge branch 'main' into main --- test: fix test for backend do update Co-authored-by: Tree Xie <tree.xie@outlook.com> Includes-commit: 1421c26 Includes-commit: 695d549 Includes-commit: 6a09b52 Includes-commit: 72d6ee0 Includes-commit: e6c2af0 Includes-commit: fb62869 Replicated-from: #325
@vicanso thanks for your contribution, this was included in our latest sync! |
--- test: add test for upstream health observe --- renamed the function and added doc to make it intelligible --- fix clippy error --- Merge branch 'main' into main --- test: fix test for backend do update Co-authored-by: Tree Xie <tree.xie@outlook.com> Includes-commit: 1421c26 Includes-commit: 695d549 Includes-commit: 6a09b52 Includes-commit: 72d6ee0 Includes-commit: e6c2af0 Includes-commit: fb62869 Replicated-from: cloudflare#325
This PR supports add callback function for observing upstream backend health status.
Issue: #225