From 71d99332204ddfbb3cf305c7d3bc8840d508bf47 Mon Sep 17 00:00:00 2001 From: Prajjwal Kumar Date: Tue, 5 Nov 2024 16:01:56 +0530 Subject: [PATCH] fix: lazy connection pools for dynamic routing service (#6437) Co-authored-by: Nishant Joshi Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com> --- Cargo.lock | 7 ++++--- crates/external_services/Cargo.toml | 4 +++- .../src/grpc_client/dynamic_routing.rs | 20 +++++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc65432209..760d2865ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3126,8 +3126,10 @@ dependencies = [ "dyn-clone", "error-stack", "hex", + "http-body-util", "hyper 0.14.30", "hyper-proxy", + "hyper-util", "hyperswitch_interfaces", "masking", "once_cell", @@ -3959,9 +3961,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.7" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cde7055719c54e36e95e8719f95883f22072a48ede39db7fc17a4e1d5281e9b9" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" dependencies = [ "bytes 1.7.1", "futures-channel", @@ -3972,7 +3974,6 @@ dependencies = [ "pin-project-lite", "socket2", "tokio 1.40.0", - "tower", "tower-service", "tracing", ] diff --git a/crates/external_services/Cargo.toml b/crates/external_services/Cargo.toml index 735ccc5ebf..12b9dd3b0f 100644 --- a/crates/external_services/Cargo.toml +++ b/crates/external_services/Cargo.toml @@ -13,7 +13,7 @@ email = ["dep:aws-config"] aws_s3 = ["dep:aws-config", "dep:aws-sdk-s3"] hashicorp-vault = ["dep:vaultrs"] v1 = ["hyperswitch_interfaces/v1", "common_utils/v1"] -dynamic_routing = ["dep:prost", "dep:tonic", "dep:tonic-reflection", "dep:tonic-types", "dep:api_models", "tokio/macros", "tokio/rt-multi-thread", "dep:tonic-build", "dep:router_env"] +dynamic_routing = ["dep:prost", "dep:tonic", "dep:tonic-reflection", "dep:tonic-types", "dep:api_models", "tokio/macros", "tokio/rt-multi-thread", "dep:tonic-build", "dep:router_env", "dep:hyper-util", "dep:http-body-util"] [dependencies] async-trait = "0.1.79" @@ -38,6 +38,8 @@ tokio = "1.37.0" tonic = { version = "0.12.2", optional = true } tonic-reflection = { version = "0.12.2", optional = true } tonic-types = { version = "0.12.2", optional = true } +hyper-util = { version = "0.1.9", optional = true } +http-body-util = { version = "0.1.2", optional = true } # First party crates diff --git a/crates/external_services/src/grpc_client/dynamic_routing.rs b/crates/external_services/src/grpc_client/dynamic_routing.rs index 2681bffb0c..343dd80e95 100644 --- a/crates/external_services/src/grpc_client/dynamic_routing.rs +++ b/crates/external_services/src/grpc_client/dynamic_routing.rs @@ -6,6 +6,9 @@ use api_models::routing::{ }; use common_utils::{errors::CustomResult, ext_traits::OptionExt, transformers::ForeignTryFrom}; use error_stack::ResultExt; +use http_body_util::combinators::UnsyncBoxBody; +use hyper::body::Bytes; +use hyper_util::client::legacy::connect::HttpConnector; use serde; use success_rate::{ success_rate_calculator_client::SuccessRateCalculatorClient, CalSuccessRateConfig, @@ -13,7 +16,7 @@ use success_rate::{ CurrentBlockThreshold as DynamicCurrentThreshold, LabelWithStatus, UpdateSuccessRateWindowConfig, UpdateSuccessRateWindowRequest, UpdateSuccessRateWindowResponse, }; -use tonic::transport::Channel; +use tonic::Status; #[allow( missing_docs, unused_qualifications, @@ -40,11 +43,13 @@ pub enum DynamicRoutingError { SuccessRateBasedRoutingFailure(String), } +type Client = hyper_util::client::legacy::Client>; + /// Type that consists of all the services provided by the client #[derive(Debug, Clone)] pub struct RoutingStrategy { /// success rate service for Dynamic Routing - pub success_rate_client: Option>, + pub success_rate_client: Option>, } /// Contains the Dynamic Routing Client Config @@ -68,11 +73,14 @@ impl DynamicRoutingClientConfig { pub async fn get_dynamic_routing_connection( self, ) -> Result> { + let client = + hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()) + .http2_only(true) + .build_http(); let success_rate_client = match self { Self::Enabled { host, port } => { - let uri = format!("http://{}:{}", host, port); - let channel = tonic::transport::Endpoint::new(uri)?.connect().await?; - Some(SuccessRateCalculatorClient::new(channel)) + let uri = format!("http://{}:{}", host, port).parse::()?; + Some(SuccessRateCalculatorClient::with_origin(client, uri)) } Self::Disabled => None, }; @@ -102,7 +110,7 @@ pub trait SuccessBasedDynamicRouting: dyn_clone::DynClone + Send + Sync { } #[async_trait::async_trait] -impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient { +impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient { async fn calculate_success_rate( &self, id: String,