Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(payments): sum connector latency across retry attempts (#14333)

Prasunna Soppa9b3b15f840b1
diff --git a/crates/router/src/core/payments/retry.rs b/crates/router/src/core/payments/retry.rs
index 7dec040a94..fba36b1d1f 100644
--- a/crates/router/src/core/payments/retry.rs
+++ b/crates/router/src/core/payments/retry.rs
@@ -32,6 +32,15 @@ use crate::{
types::{self, api, domain, storage, transformers::ForeignFrom},
};
+/// Sums the outbound-call time of two attempts, keeping whichever side is present.
+#[cfg(feature = "v1")]
+fn accumulate_external_latency(total: Option<u128>, attempt: Option<u128>) -> Option<u128> {
+ match (total, attempt) {
+ (Some(total), Some(attempt)) => Some(total + attempt),
+ (total, attempt) => total.or(attempt),
+ }
+}
+
#[instrument(skip_all)]
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "v1")]
@@ -102,6 +111,10 @@ where
false
};
+ // Each retry builds a fresh `RouterData` starting at `None`, so accumulate here to keep
+ // earlier attempts' connector time from being billed to Hyperswitch as `latency - hs_latency`.
+ let mut external_latency_total = router_data.external_latency;
+
if should_step_up {
router_data = Box::pin(do_retry(
&state.clone(),
@@ -123,6 +136,9 @@ where
feature_config,
))
.await?;
+
+ external_latency_total =
+ accumulate_external_latency(external_latency_total, router_data.external_latency);
}
// Step up is not applicable so proceed with auto retries flow
else {
@@ -231,6 +247,11 @@ where
))
.await?;
+ external_latency_total = accumulate_external_latency(
+ external_latency_total,
+ router_data.external_latency,
+ );
+
retries = retries.map(|i| i - 1);
}
storage_enums::GsmDecision::DoDefault => break,
@@ -238,6 +259,10 @@ where
initial_gsm = None;
}
}
+
+ // Report the whole payment's connector time, not just the last attempt's.
+ router_data.external_latency = external_latency_total;
+
Ok(router_data)
}
diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs
index b941338806..33868975ca 100644
--- a/crates/router/src/services/api.rs
+++ b/crates/router/src/services/api.rs
@@ -368,6 +368,12 @@ where
);
let auth_user_id = auth_type.get_user_id();
+
+ // The header still holds raw connector time here; it is only turned into Hyperswitch's own
+ // overhead later, on the wire.
+ let hs_latency =
+ overhead_latency.map(|overhead_latency| request_duration.saturating_sub(overhead_latency));
+
let api_event = ApiEvent::new(
tenant_id,
Some(merchant_id.clone()),
@@ -377,7 +383,7 @@ where
status_code,
serialized_request,
serialized_response,
- overhead_latency,
+ hs_latency,
auth_type,
auth_user_id,
error,
@@ -642,7 +648,9 @@ pub fn http_response_json_with_headers<T: body::MessageBody + 'static>(
if header_name == X_HS_LATENCY {
if let Some(request_duration) = request_duration {
if let Ok(external_latency) = header_value.parse::<u128>() {
- let updated_duration = request_duration.as_millis() - external_latency;
+ let updated_duration = request_duration
+ .as_millis()
+ .saturating_sub(external_latency);
header_value = updated_duration.to_string();
}
}