Skip to content

Commit

Permalink
fix(redis): fix recreation on redis connection pool (#1063)
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantJoshi00 authored May 8, 2023
1 parent 3131bc8 commit 982c27f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/redis_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl RedisConnectionPool {
}

impl Drop for RedisConnectionPool {
// safety: panics when invoked without a current tokio runtime
fn drop(&mut self) {
let rt = tokio::runtime::Handle::current();
rt.block_on(self.close_connections())
Expand Down
3 changes: 1 addition & 2 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use storage_models::{enums as storage_enums, payment_method};
use crate::scheduler::metrics as scheduler_metrics;
use crate::{
configs::settings,
connection,
core::{
errors::{self, StorageErrorExt},
payment_methods::{
Expand Down Expand Up @@ -1531,7 +1530,7 @@ pub async fn list_customer_payment_method(
};
customer_pms.push(pma.to_owned());

let redis_conn = connection::redis_connection(&state.conf).await;
let redis_conn = state.store.get_redis_conn();
let key_for_hyperswitch_token = format!(
"pm_token_{}_{}_hyperswitch",
parent_payment_method_token, pma.payment_method
Expand Down
3 changes: 1 addition & 2 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use self::{
operations::{payment_complete_authorize, BoxedOperation, Operation},
};
use crate::{
connection,
core::{
errors::{self, CustomResult, RouterResponse, RouterResult},
payment_methods::vault,
Expand Down Expand Up @@ -672,7 +671,7 @@ async fn decide_payment_method_tokenize_action(
}
}
Some(token) => {
let redis_conn = connection::redis_connection(&state.conf).await;
let redis_conn = state.store.get_redis_conn();
let key = format!(
"pm_token_{}_{}_{}",
token.to_owned(),
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::{
};
use crate::{
configs::settings::Server,
connection, consts,
consts,
core::{
errors::{self, CustomResult, RouterResult, StorageErrorExt},
payment_methods::{cards, vault},
Expand Down Expand Up @@ -691,7 +691,7 @@ pub async fn make_pm_data<'a, F: Clone, R>(
let request = &payment_data.payment_method_data;
let token = payment_data.token.clone();
let hyperswitch_token = if let Some(token) = token {
let redis_conn = connection::redis_connection(&state.conf).await;
let redis_conn = state.store.get_redis_conn();
let key = format!(
"pm_token_{}_{}_hyperswitch",
token,
Expand Down
13 changes: 11 additions & 2 deletions crates/router/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use std::sync::Arc;

use futures::lock::Mutex;

use crate::{services::Store, types::storage};
use crate::{
services::{self, Store},
types::storage,
};

#[derive(PartialEq, Eq)]
pub enum StorageImpl {
Expand Down Expand Up @@ -61,10 +64,10 @@ pub trait StorageInterface:
+ refund::RefundInterface
+ reverse_lookup::ReverseLookupInterface
+ cards_info::CardsInfoInterface
+ services::RedisConnInterface
+ 'static
{
}

#[async_trait::async_trait]
impl StorageInterface for Store {}

Expand Down Expand Up @@ -117,4 +120,10 @@ where
.change_context(redis_interface::errors::RedisError::JsonDeserializationFailed)
}

impl services::RedisConnInterface for MockDb {
fn get_redis_conn(&self) -> Arc<redis_interface::RedisConnectionPool> {
self.redis.clone()
}
}

dyn_clone::clone_trait_object!(StorageInterface);
10 changes: 10 additions & 0 deletions crates/router/src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl PubSubInterface for redis_interface::RedisConnectionPool {
}
}

pub trait RedisConnInterface {
fn get_redis_conn(&self) -> Arc<redis_interface::RedisConnectionPool>;
}

#[derive(Clone)]
pub struct Store {
pub master_pool: PgPool,
Expand Down Expand Up @@ -185,3 +189,9 @@ impl Store {
.change_context(crate::core::errors::StorageError::KVError)
}
}

impl RedisConnInterface for Store {
fn get_redis_conn(&self) -> Arc<redis_interface::RedisConnectionPool> {
self.redis_conn.clone()
}
}

0 comments on commit 982c27f

Please sign in to comment.