Skip to content

Commit

Permalink
refactor(access_token): handle network delays with expiry of access t…
Browse files Browse the repository at this point in the history
…oken (#4617)
  • Loading branch information
Narayanbhat166 authored and kashif-m committed May 20, 2024
1 parent bcc32db commit 7b81af6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions crates/router/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 units
Expand Down
43 changes: 38 additions & 5 deletions crates/router/src/core/payments/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand All @@ -142,7 +175,7 @@ pub async fn add_access_token<
// The next request will create new access token, if required
logger::error!(access_token_set_error=?access_token_set_error);
}
Some(access_token)
Some(modified_access_token_with_expiry)
})
.await
}
Expand Down
12 changes: 11 additions & 1 deletion crates/router/src/routes/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -123,5 +122,16 @@ counter_metric!(TASKS_ADDED_COUNT, GLOBAL_METER); // Tasks added to process trac
counter_metric!(TASK_ADDITION_FAILURES_COUNT, GLOBAL_METER); // Failures in task addition to process tracker
counter_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;

0 comments on commit 7b81af6

Please sign in to comment.