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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
refactor(access_token): handle network delays with expiry of access token (#4617)
Narayan Bhat0d45f854a2cc
diff --git a/crates/router/src/consts.rs b/crates/router/src/consts.rsindex 0c9e9443ac..6d9da8f7bb 100644--- a/crates/router/src/consts.rs+++ b/crates/router/src/consts.rs@@ -118,6 +118,8 @@ pub const POLL_ID_TTL: i64 = 900;pub const DEFAULT_POLL_DELAY_IN_SECS: i8 = 2;pub const DEFAULT_POLL_FREQUENCY: i8 = 5;+// Number of seconds to subtract from access token expiry+pub(crate) const REDUCE_ACCESS_TOKEN_EXPIRY_TIME: u8 = 15;pub const CONNECTOR_CREDS_TOKEN_TTL: i64 = 900;//max_amount allowed is 999999999 in minor unitsdiff --git a/crates/router/src/core/payments/access_token.rs b/crates/router/src/core/payments/access_token.rsindex 2d4ed2b6e1..0c5aa13eb2 100644--- a/crates/router/src/core/payments/access_token.rs+++ b/crates/router/src/core/payments/access_token.rs@@ -91,9 +91,26 @@ pub async fn add_access_token<connector.connector_name,access_token.expires);+ metrics::ACCESS_TOKEN_CACHE_HIT.add(+ &metrics::CONTEXT,+ 1,+ &[metrics::request::add_attributes(+ "connector",+ connector.connector_name.to_string(),+ )],+ );Ok(Some(access_token))}None => {+ metrics::ACCESS_TOKEN_CACHE_MISS.add(+ &metrics::CONTEXT,+ 1,+ &[metrics::request::add_attributes(+ "connector",+ connector.connector_name.to_string(),+ )],+ );+let cloned_router_data = router_data.clone();let refresh_token_request_data = types::AccessTokenRequestData::try_from(router_data.connector_auth_type.clone(),@@ -123,15 +140,31 @@ pub async fn add_access_token<&refresh_token_router_data,).await?- .async_map(|access_token| async {- // Store the access token in redis with expiry- // The expiry should be adjusted for network delays from the connector+ .async_map(|access_token| async move {let store = &*state.store;++ // The expiry should be adjusted for network delays from the connector+ // The access token might not have been expired when request is sent+ // But once it reaches the connector, it might expire because of the network delay+ // Subtract few seconds from the expiry in order to account for these network delays+ // This will reduce the expiry time by `REDUCE_ACCESS_TOKEN_EXPIRY_TIME` seconds+ let modified_access_token_with_expiry = types::AccessToken {+ expires: access_token+ .expires+ .saturating_sub(consts::REDUCE_ACCESS_TOKEN_EXPIRY_TIME.into()),+ ..access_token+ };++ logger::debug!(+ access_token_expiry_after_modification =+ modified_access_token_with_expiry.expires+ );+if let Err(access_token_set_error) = store.set_access_token(merchant_id,&merchant_connector_id_or_connector_name,- access_token.clone(),+ modified_access_token_with_expiry.clone(),).await.change_context(errors::ApiErrorResponse::InternalServerError)@@ -142,7 +175,7 @@ pub async fn add_access_token<// The next request will create new access token, if requiredlogger::error!(access_token_set_error=?access_token_set_error);}- Some(access_token)+ Some(modified_access_token_with_expiry)}).await}diff --git a/crates/router/src/routes/metrics.rs b/crates/router/src/routes/metrics.rsindex 780dd51bbd..1123be1a87 100644--- a/crates/router/src/routes/metrics.rs+++ b/crates/router/src/routes/metrics.rs@@ -52,7 +52,6 @@ counter_metric!(MCA_CREATE, GLOBAL_METER);// Flow Specific Metrics-counter_metric!(ACCESS_TOKEN_CREATION, GLOBAL_METER);histogram_metric!(CONNECTOR_REQUEST_TIME, GLOBAL_METER);counter_metric!(SESSION_TOKEN_CREATED, GLOBAL_METER);@@ -123,5 +122,16 @@ counter_metric!(TASKS_ADDED_COUNT, GLOBAL_METER); // Tasks added to process traccounter_metric!(TASK_ADDITION_FAILURES_COUNT, GLOBAL_METER); // Failures in task addition to process trackercounter_metric!(TASKS_RESET_COUNT, GLOBAL_METER); // Tasks reset in process tracker for requeue flow+// Access token metrics+//+// A counter to indicate the number of new access tokens created+counter_metric!(ACCESS_TOKEN_CREATION, GLOBAL_METER);++// A counter to indicate the access token cache hits+counter_metric!(ACCESS_TOKEN_CACHE_HIT, GLOBAL_METER);++// A counter to indicate the access token cache miss+counter_metric!(ACCESS_TOKEN_CACHE_MISS, GLOBAL_METER);+pub mod request;pub mod utils;