feat: Added grpc based health check (#6441)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Sarthak Soni
2024-11-26 18:54:55 +05:30
committed by GitHub
parent 8fbb766308
commit e922f96cee
13 changed files with 285 additions and 17 deletions

View File

@ -1,11 +1,28 @@
/// Dyanimc Routing Client interface implementation
#[cfg(feature = "dynamic_routing")]
pub mod dynamic_routing;
/// gRPC based Heath Check Client interface implementation
#[cfg(feature = "dynamic_routing")]
pub mod health_check_client;
use std::{fmt::Debug, sync::Arc};
#[cfg(feature = "dynamic_routing")]
use dynamic_routing::{DynamicRoutingClientConfig, RoutingStrategy};
#[cfg(feature = "dynamic_routing")]
use health_check_client::HealthCheckClient;
#[cfg(feature = "dynamic_routing")]
use http_body_util::combinators::UnsyncBoxBody;
#[cfg(feature = "dynamic_routing")]
use hyper::body::Bytes;
#[cfg(feature = "dynamic_routing")]
use hyper_util::client::legacy::connect::HttpConnector;
use serde;
#[cfg(feature = "dynamic_routing")]
use tonic::Status;
#[cfg(feature = "dynamic_routing")]
/// Hyper based Client type for maintaining connection pool for all gRPC services
pub type Client = hyper_util::client::legacy::Client<HttpConnector, UnsyncBoxBody<Bytes, Status>>;
/// Struct contains all the gRPC Clients
#[derive(Debug, Clone)]
@ -13,6 +30,9 @@ pub struct GrpcClients {
/// The routing client
#[cfg(feature = "dynamic_routing")]
pub dynamic_routing: RoutingStrategy,
/// Health Check client for all gRPC services
#[cfg(feature = "dynamic_routing")]
pub health_client: HealthCheckClient,
}
/// Type that contains the configs required to construct a gRPC client with its respective services.
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, Default)]
@ -29,17 +49,30 @@ impl GrpcClientSettings {
/// This function will be called at service startup.
#[allow(clippy::expect_used)]
pub async fn get_grpc_client_interface(&self) -> Arc<GrpcClients> {
#[cfg(feature = "dynamic_routing")]
let client =
hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new())
.http2_only(true)
.build_http();
#[cfg(feature = "dynamic_routing")]
let dynamic_routing_connection = self
.dynamic_routing_client
.clone()
.get_dynamic_routing_connection()
.get_dynamic_routing_connection(client.clone())
.await
.expect("Failed to establish a connection with the Dynamic Routing Server");
#[cfg(feature = "dynamic_routing")]
let health_client = HealthCheckClient::build_connections(self, client)
.await
.expect("Failed to build gRPC connections");
Arc::new(GrpcClients {
#[cfg(feature = "dynamic_routing")]
dynamic_routing: dynamic_routing_connection,
#[cfg(feature = "dynamic_routing")]
health_client,
})
}
}