Skip to content

Commit

Permalink
fix(routing): resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarthak1799 committed Jan 18, 2024
1 parent 5ba76d6 commit ea3b076
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
58 changes: 29 additions & 29 deletions crates/router/src/core/payments/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,14 @@ pub async fn perform_static_routing_v1<F: Clone>(

return Ok(fallback_config);
};
let key = ensure_algorithm_cached_v1(
let cached_algorithm = ensure_algorithm_cached_v1(
state,
merchant_id,
&algorithm_id,
#[cfg(feature = "business_profile_routing")]
payment_data.payment_intent.profile_id.clone(),
)
.await?;
let cached_algorithm: Arc<CachedAlgorithm> = ROUTING_CACHE
.get_val(key.as_str())
.await
.ok_or(errors::RoutingError::CacheMiss)
.into_report()
.attach_printable("Unable to retrieve cached routing algorithm even after refresh")?;

Ok(match cached_algorithm.as_ref() {
CachedAlgorithm::Single(conn) => vec![(**conn).clone()],
Expand All @@ -263,7 +257,7 @@ async fn ensure_algorithm_cached_v1(
merchant_id: &str,
algorithm_id: &str,
#[cfg(feature = "business_profile_routing")] profile_id: Option<String>,
) -> RoutingResult<String> {
) -> RoutingResult<Arc<CachedAlgorithm>> {
#[cfg(feature = "business_profile_routing")]
let key = {
let profile_id = profile_id
Expand All @@ -277,9 +271,13 @@ async fn ensure_algorithm_cached_v1(
#[cfg(not(feature = "business_profile_routing"))]
let key = format!("dsl_{merchant_id}");

let present = ROUTING_CACHE.exists(key.as_str()).await;
let cached_algorithm = ROUTING_CACHE
.get_val::<Arc<CachedAlgorithm>>(key.as_str())
.await;

if !present {
let algorithm = if let Some(algo) = cached_algorithm {
algo
} else {
refresh_routing_cache_v1(
state,
key.clone(),
Expand All @@ -288,9 +286,16 @@ async fn ensure_algorithm_cached_v1(
profile_id,
)
.await?;

ROUTING_CACHE
.get_val(key.as_str())
.await
.ok_or(errors::RoutingError::CacheMiss)
.into_report()
.attach_printable("Unable to retrieve cached routing algorithm even after refresh")?
};

Ok(key)
Ok(algorithm)
}

pub fn perform_straight_through_routing<F: Clone>(
Expand Down Expand Up @@ -452,9 +457,11 @@ pub async fn get_merchant_kgraph<'a>(
#[cfg(not(feature = "business_profile_routing"))]
let key = format!("kgraph_{}", key_store.merchant_id);

let kgraph_present = KGRAPH_CACHE.exists(key.as_str()).await;
let cached_kgraph = KGRAPH_CACHE.get_val(key.as_str()).await;

if !kgraph_present {
let kgraph = if let Some(graph) = cached_kgraph {
graph
} else {
refresh_kgraph_cache(
state,
key_store,
Expand All @@ -463,16 +470,16 @@ pub async fn get_merchant_kgraph<'a>(
profile_id,
)
.await?;
}

let cached_kgraph = KGRAPH_CACHE
.get_val(key.as_str())
.await
.ok_or(errors::RoutingError::CacheMiss)
.into_report()
.attach_printable("when retrieving kgraph")?;
KGRAPH_CACHE
.get_val(key.as_str())
.await
.ok_or(errors::RoutingError::CacheMiss)
.into_report()
.attach_printable("when retrieving kgraph")?
};

Ok(cached_kgraph)
Ok(kgraph)
}

pub async fn refresh_kgraph_cache(
Expand Down Expand Up @@ -831,7 +838,7 @@ async fn perform_session_routing_for_pm_type(
let chosen_connectors = match session_pm_input.routing_algorithm {
MerchantAccountRoutingAlgorithm::V1(algorithm_ref) => {
if let Some(ref algorithm_id) = algorithm_ref.algorithm_id {
let key = ensure_algorithm_cached_v1(
let cached_algorithm = ensure_algorithm_cached_v1(
&session_pm_input.state.clone(),
merchant_id,
algorithm_id,
Expand All @@ -840,13 +847,6 @@ async fn perform_session_routing_for_pm_type(
)
.await?;

let cached_algorithm: Arc<CachedAlgorithm> = ROUTING_CACHE
.get_val(key.as_str())
.await
.ok_or(errors::RoutingError::CacheMiss)
.into_report()
.attach_printable("unable to retrieve cached routing algorithm")?;

match cached_algorithm.as_ref() {
CachedAlgorithm::Single(conn) => vec![(**conn).clone()],
CachedAlgorithm::Priority(plist) => plist.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/db/merchant_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ async fn publish_and_redact_merchant_account_cache(
});

#[cfg(not(feature = "business_profile_routing"))]
let kgraph_key = format!("kgraph_{}", merchant_account.merchant_id.clone());
let kgraph_key = Some(format!("kgraph_{}", merchant_account.merchant_id.clone()));

let mut cache_keys = vec![CacheKind::Accounts(
merchant_account.merchant_id.as_str().into(),
Expand Down

0 comments on commit ea3b076

Please sign in to comment.