Skip to content
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

Refactor(core): interpolate success_based_routing config params with their specific values #6448

Merged
merged 23 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions crates/external_services/src/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{fmt::Debug, sync::Arc};

#[cfg(feature = "dynamic_routing")]
use dynamic_routing::{DynamicRoutingClientConfig, RoutingStrategy};
use router_env::logger;
use serde;

/// Struct contains all the gRPC Clients
Expand Down Expand Up @@ -38,8 +37,6 @@ impl GrpcClientSettings {
.await
.expect("Failed to establish a connection with the Dynamic Routing Server");

logger::info!("Connection established with gRPC Server");

Arc::new(GrpcClients {
#[cfg(feature = "dynamic_routing")]
dynamic_routing: dynamic_routing_connection,
Expand Down
2 changes: 2 additions & 0 deletions crates/external_services/src/grpc_client/dynamic_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use success_rate::{
CurrentBlockThreshold as DynamicCurrentThreshold, LabelWithStatus,
UpdateSuccessRateWindowConfig, UpdateSuccessRateWindowRequest, UpdateSuccessRateWindowResponse,
};
use router_env::logger;
use tonic::transport::Channel;
#[allow(
missing_docs,
Expand Down Expand Up @@ -72,6 +73,7 @@ impl DynamicRoutingClientConfig {
Self::Enabled { host, port } => {
let uri = format!("http://{}:{}", host, port);
let channel = tonic::transport::Endpoint::new(uri)?.connect().await?;
logger::info!("Connection established with gRPC Server");
prajjwalkumar17 marked this conversation as resolved.
Show resolved Hide resolved
Some(SuccessRateCalculatorClient::new(channel))
}
Self::Disabled => None,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license.workspace = true

[features]
default = ["common_default", "v1"]
common_default = ["kv_store", "stripe", "oltp", "olap", "accounts_cache", "dummy_connector", "payouts", "payout_retry", "retry", "frm", "tls", "partial-auth", "km_forward_x_request_id"]
common_default = ["dynamic_routing", "kv_store", "stripe", "oltp", "olap", "accounts_cache", "dummy_connector", "payouts", "payout_retry", "retry", "frm", "tls", "partial-auth", "km_forward_x_request_id"]
olap = ["hyperswitch_domain_models/olap", "storage_impl/olap", "scheduler/olap", "api_models/olap", "dep:analytics"]
tls = ["actix-web/rustls-0_22"]
email = ["external_services/email", "scheduler/email", "olap"]
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ pub enum RoutingError {
MetadataParsingError,
#[error("Unable to retrieve success based routing config")]
SuccessBasedRoutingConfigError,
#[error("Params not found in success based routing config")]
SuccessBasedRoutingParamsNotFoundError,
#[error("Unable to calculate success based routing config from dynamic routing service")]
SuccessRateCalculationError,
#[error("Success rate client from dynamic routing gRPC service not initialized")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
);
tokio::spawn(
async move {
routing_helpers::push_metrics_for_success_based_routing(
routing_helpers::push_metrics_with_update_window_for_success_based_routing(
&state,
&payment_attempt,
routable_connectors,
Expand Down
6 changes: 5 additions & 1 deletion crates/router/src/core/payments/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,11 @@ pub async fn perform_success_based_routing(
.attach_printable("unable to fetch success_rate based dynamic routing configs")?;

let success_based_routing_config_params = success_based_routing_config_params_interpolator
.get_string_val(success_based_routing_configs.params.as_ref());
.get_string_val(
success_based_routing_configs
.params.as_ref()
.ok_or(errors::RoutingError::SuccessBasedRoutingParamsNotFoundError)
.attach_printable("params not found in success based routing config")?);
prajjwalkumar17 marked this conversation as resolved.
Show resolved Hide resolved

let tenant_business_profile_id = routing::helpers::generate_tenant_business_profile_id(
&state.tenant.redis_key_prefix,
Expand Down
73 changes: 38 additions & 35 deletions crates/router/src/core/routing/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ pub async fn fetch_success_based_routing_configs(
/// metrics for success based dynamic routing
#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
#[instrument(skip_all)]
pub async fn push_metrics_for_success_based_routing(
pub async fn push_metrics_with_update_window_for_success_based_routing(
state: &SessionState,
payment_attempt: &storage::PaymentAttempt,
routable_connectors: Vec<routing_types::RoutableConnectorChoice>,
Expand Down Expand Up @@ -699,7 +699,12 @@ pub async fn push_metrics_for_success_based_routing(
);

let success_based_routing_config_params = success_based_routing_config_params_interpolator
.get_string_val(success_based_routing_configs.params.as_ref());
.get_string_val(
success_based_routing_configs
.params.as_ref()
.ok_or(errors::RoutingError::SuccessBasedRoutingParamsNotFoundError)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("params not found in success based routing config")?);
prajjwalkumar17 marked this conversation as resolved.
Show resolved Hide resolved

let success_based_connectors = client
.calculate_success_rate(
Expand Down Expand Up @@ -997,42 +1002,40 @@ impl SuccessBasedRoutingConfigParamsInterpolator {

pub fn get_string_val(
&self,
params: Option<&Vec<routing_types::SuccessBasedRoutingConfigParams>>,
params: &Vec<routing_types::SuccessBasedRoutingConfigParams>,
) -> String {
let mut parts: Vec<String> = Vec::new();
if let Some(params) = params {
for param in params {
let val = match param {
routing_types::SuccessBasedRoutingConfigParams::PaymentMethod => self
.payment_method
.as_ref()
.map_or(String::new(), |pm| pm.to_string()),
routing_types::SuccessBasedRoutingConfigParams::PaymentMethodType => self
.payment_method_type
.as_ref()
.map_or(String::new(), |pmt| pmt.to_string()),
routing_types::SuccessBasedRoutingConfigParams::AuthenticationType => self
.authentication_type
.as_ref()
.map_or(String::new(), |at| at.to_string()),
routing_types::SuccessBasedRoutingConfigParams::Currency => self
.currency
.as_ref()
.map_or(String::new(), |cur| cur.to_string()),
routing_types::SuccessBasedRoutingConfigParams::Country => self
.country
.as_ref()
.map_or(String::new(), |cn| cn.to_string()),
routing_types::SuccessBasedRoutingConfigParams::CardNetwork => {
self.card_network.clone().unwrap_or_default()
}
routing_types::SuccessBasedRoutingConfigParams::CardBin => {
self.card_bin.clone().unwrap_or_default()
}
};
if !val.is_empty() {
parts.push(val);
for param in params {
let val = match param {
routing_types::SuccessBasedRoutingConfigParams::PaymentMethod => self
.payment_method
.as_ref()
.map_or(String::new(), |pm| pm.to_string()),
routing_types::SuccessBasedRoutingConfigParams::PaymentMethodType => self
.payment_method_type
.as_ref()
.map_or(String::new(), |pmt| pmt.to_string()),
routing_types::SuccessBasedRoutingConfigParams::AuthenticationType => self
.authentication_type
.as_ref()
.map_or(String::new(), |at| at.to_string()),
routing_types::SuccessBasedRoutingConfigParams::Currency => self
.currency
.as_ref()
.map_or(String::new(), |cur| cur.to_string()),
routing_types::SuccessBasedRoutingConfigParams::Country => self
.country
.as_ref()
.map_or(String::new(), |cn| cn.to_string()),
routing_types::SuccessBasedRoutingConfigParams::CardNetwork => {
self.card_network.clone().unwrap_or_default()
}
routing_types::SuccessBasedRoutingConfigParams::CardBin => {
self.card_bin.clone().unwrap_or_default()
}
};
if !val.is_empty() {
parts.push(val);
}
}
parts.join(":")
Expand Down
Loading