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(storage): avoid unnecessary DB connection leases in write operations (#14581)

Kashife08e085ea89e
diff --git a/crates/router/src/db/address.rs b/crates/router/src/db/address.rs
index 2e755828c3..ab72a4fd45 100644
--- a/crates/router/src/db/address.rs
+++ b/crates/router/src/db/address.rs
@@ -441,7 +441,6 @@ mod storage {
key_store: &domain::MerchantKeyStore,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<domain::PaymentAddress, errors::StorageError> {
- let conn = connection::pg_connection_write(self).await?;
let address = Conversion::convert(this)
.await
.change_context(errors::StorageError::EncryptionError)?;
@@ -459,6 +458,7 @@ mod storage {
.await;
match storage_scheme {
MerchantStorageScheme::PostgresOnly => {
+ let conn = connection::pg_connection_write(self).await?;
address
.update(&conn, address_update.into())
.await
diff --git a/crates/router/src/db/mandate.rs b/crates/router/src/db/mandate.rs
index eb7a6b72b7..151df11f7d 100644
--- a/crates/router/src/db/mandate.rs
+++ b/crates/router/src/db/mandate.rs
@@ -219,7 +219,6 @@ mod storage {
mandate: storage_types::Mandate,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<storage_types::Mandate, errors::StorageError> {
- let conn = connection::pg_connection_write(self).await?;
let key = PartitionKey::MerchantIdMandateId {
merchant_id,
mandate_id,
@@ -233,6 +232,7 @@ mod storage {
.await;
match storage_scheme {
MerchantStorageScheme::PostgresOnly => {
+ let conn = connection::pg_connection_write(self).await?;
storage_types::Mandate::update_by_merchant_id_mandate_id(
&conn,
merchant_id,
@@ -318,7 +318,6 @@ mod storage {
mut mandate: storage_types::MandateNew,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<storage_types::Mandate, errors::StorageError> {
- let conn = connection::pg_connection_write(self).await?;
let storage_scheme = Box::pin(decide_storage_scheme::<_, diesel_models::Mandate>(
self,
storage_scheme,
@@ -327,10 +326,13 @@ mod storage {
.await;
mandate.update_storage_scheme(storage_scheme);
match storage_scheme {
- MerchantStorageScheme::PostgresOnly => mandate
- .insert(&conn)
- .await
- .map_err(|error| report!(errors::StorageError::from(error))),
+ MerchantStorageScheme::PostgresOnly => {
+ let conn = connection::pg_connection_write(self).await?;
+ mandate
+ .insert(&conn)
+ .await
+ .map_err(|error| report!(errors::StorageError::from(error)))
+ }
MerchantStorageScheme::RedisKv => {
let mandate_id = mandate.mandate_id.clone();
let merchant_id = &mandate.merchant_id.to_owned();
diff --git a/crates/storage_impl/src/customers.rs b/crates/storage_impl/src/customers.rs
index 4192eee615..16aa95430f 100644
--- a/crates/storage_impl/src/customers.rs
+++ b/crates/storage_impl/src/customers.rs
@@ -1,6 +1,7 @@
use common_utils::{id_type, pii};
#[cfg(feature = "v2")]
use diesel_models::customers;
+use diesel_models::errors::DatabaseError;
use error_stack::ResultExt;
use futures::future::try_join_all;
use hyperswitch_domain_models::{
@@ -190,7 +191,6 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
key_store: &MerchantKeyStore,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<domain::Customer, StorageError> {
- let conn = pg_connection_write(self).await?;
let customer = Conversion::convert(customer)
.await
.change_context(StorageError::EncryptionError)?;
@@ -205,22 +205,40 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
};
let field = format!("cust_{}", customer_id.get_string_repr());
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = customer_update_internal.generate_drainer_update_query(
- &mut query_gen_conn,
- customer_id.clone(),
- merchant_id.clone(),
- );
+ let database_call = {
+ let customer_id = customer_id.clone();
+ let merchant_id = merchant_id.clone();
+ let customer_update = customer_update.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ diesel_models::Customer::update_by_customer_id_merchant_id(
+ &conn,
+ customer_id,
+ merchant_id,
+ customer_update.foreign_into(),
+ )
+ .await
+ }
+ };
+ let drainer_query_fut = {
+ let customer_id = customer_id.clone();
+ let merchant_id = merchant_id.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ customer_update_internal
+ .generate_drainer_update_query(&mut conn, customer_id, merchant_id)
+ .await
+ }
+ };
Box::pin(self.update_resource(
key_store,
storage_scheme,
- diesel_models::Customer::update_by_customer_id_merchant_id(
- &conn,
- customer_id.clone(),
- merchant_id.clone(),
- customer_update.clone().foreign_into(),
- ),
+ database_call,
updated_customer,
kv_router_store::UpdateResourceParams {
drainer_query_fut,
@@ -330,7 +348,6 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
key_store: &MerchantKeyStore,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<domain::Customer, StorageError> {
- let conn = pg_connection_write(self).await?;
let id = customer_data.id.clone();
let key = PartitionKey::GlobalId {
id: id.get_string_repr(),
@@ -357,15 +374,29 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
reverse_lookups.push(reverse_lookup_merchant_scoped_id);
}
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = new_customer
- .clone()
- .generate_drainer_insert_query(&mut query_gen_conn);
+ let create_resource_fut = {
+ let new_customer = new_customer.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ new_customer.insert(&conn).await
+ }
+ };
+ let drainer_query_fut = {
+ let new_customer = new_customer.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ new_customer.generate_drainer_insert_query(&mut conn).await
+ }
+ };
Box::pin(self.insert_resource(
key_store,
decided_storage_scheme,
- new_customer.clone().insert(&conn),
+ create_resource_fut,
new_customer.clone().into(),
kv_router_store::InsertResourceParams {
drainer_query_fut,
@@ -386,7 +417,6 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
key_store: &MerchantKeyStore,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<domain::Customer, StorageError> {
- let conn = pg_connection_write(self).await?;
let customer_id = customer_data.get_id().clone();
let key = PartitionKey::MerchantIdCustomerId {
merchant_id: &customer_data.merchant_id.clone(),
@@ -406,15 +436,29 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
new_customer.update_storage_scheme(storage_scheme);
let customer = new_customer.clone().into();
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = new_customer
- .clone()
- .generate_drainer_insert_query(&mut query_gen_conn);
+ let create_resource_fut = {
+ let new_customer = new_customer.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ new_customer.insert(&conn).await
+ }
+ };
+ let drainer_query_fut = {
+ let new_customer = new_customer.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ new_customer.generate_drainer_insert_query(&mut conn).await
+ }
+ };
Box::pin(self.insert_resource(
key_store,
storage_scheme,
- new_customer.clone().insert(&conn),
+ create_resource_fut,
customer,
kv_router_store::InsertResourceParams {
drainer_query_fut,
@@ -570,23 +614,38 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
key_store: &MerchantKeyStore,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<domain::Customer, StorageError> {
- let conn = pg_connection_write(self).await?;
let customer = Conversion::convert(customer)
.await
.change_context(StorageError::EncryptionError)?;
let customer_update_internal =
diesel_models::CustomerUpdateInternal::foreign_from(customer_update.clone());
- let database_call =
- customers::Customer::update_by_id(&conn, id.clone(), customer_update_internal.clone());
+ let database_call = {
+ let id = id.clone();
+ let customer_update_internal = customer_update_internal.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ customers::Customer::update_by_id(&conn, id, customer_update_internal).await
+ }
+ };
let key = PartitionKey::GlobalId {
id: id.get_string_repr(),
};
let field = format!("cust_{}", id.get_string_repr());
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = customer_update_internal
- .clone()
- .generate_drainer_update_query(&mut query_gen_conn, id.clone());
+ let drainer_query_fut = {
+ let id = id.clone();
+ let customer_update_internal = customer_update_internal.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ customer_update_internal
+ .generate_drainer_update_query(&mut conn, id)
+ .await
+ }
+ };
Box::pin(self.update_resource(
key_store,
diff --git a/crates/storage_impl/src/payment_method.rs b/crates/storage_impl/src/payment_method.rs
index 10b61f090c..a62cb31487 100644
--- a/crates/storage_impl/src/payment_method.rs
+++ b/crates/storage_impl/src/payment_method.rs
@@ -3,6 +3,8 @@ use std::collections::HashSet;
use common_enums::enums::MerchantStorageScheme;
use common_utils::{errors::CustomResult, id_type};
+#[cfg(feature = "v1")]
+use diesel_models::errors::DatabaseError;
pub use diesel_models::payment_method::PaymentMethod;
use diesel_models::payment_method::{PaymentMethodUpdate, PaymentMethodUpdateInternal};
use error_stack::ResultExt;
@@ -165,7 +167,6 @@ impl<T: DatabaseStore> PaymentMethodInterface for KVRouterStore<T> {
storage_scheme: MerchantStorageScheme,
compat_action: Option<PaymentMethodCompatAction>,
) -> CustomResult<DomainPaymentMethod, errors::StorageError> {
- let conn = pg_connection_write(self).await?;
let mut payment_method_new = payment_method
.construct_new()
.await
@@ -184,15 +185,31 @@ impl<T: DatabaseStore> PaymentMethodInterface for KVRouterStore<T> {
}
let payment_method = (&payment_method_new.clone()).into();
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = payment_method_new
- .clone()
- .generate_drainer_insert_query(&mut query_gen_conn);
+ let create_resource_fut = {
+ let payment_method_new = payment_method_new.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ payment_method_new.insert(&conn).await
+ }
+ };
+ let drainer_query_fut = {
+ let payment_method_new = payment_method_new.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ payment_method_new
+ .generate_drainer_insert_query(&mut conn)
+ .await
+ }
+ };
let payment_method: DomainPaymentMethod = Box::pin(self.insert_resource(
key_store,
storage_scheme,
- payment_method_new.clone().insert(&conn),
+ create_resource_fut,
payment_method,
InsertResourceParams {
drainer_query_fut,
@@ -231,36 +248,48 @@ impl<T: DatabaseStore> PaymentMethodInterface for KVRouterStore<T> {
merchant_id: &merchant_id,
customer_id: &customer_id,
};
- let conn = pg_connection_write(self).await?;
let field = format!("payment_method_id_{}", payment_method.get_id().clone());
let p_update: PaymentMethodUpdateInternal =
payment_method_update.convert_to_payment_method_update(storage_scheme);
let updated_payment_method = p_update.clone().apply_changeset(payment_method.clone());
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = p_update.clone().generate_drainer_update_query(
- &mut query_gen_conn,
- payment_method.payment_method_id.clone(),
- );
+ let update_resource_fut = {
+ let payment_method = payment_method.clone();
+ let p_update = p_update.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ Box::pin(payment_method.update_with_payment_method_id(&conn, p_update)).await
+ }
+ };
+ let drainer_query_fut = {
+ let payment_method_id = payment_method.payment_method_id.clone();
+ let p_update = p_update.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ p_update
+ .generate_drainer_update_query(&mut conn, payment_method_id)
+ .await
+ }
+ };
- let payment_method: DomainPaymentMethod = Box::pin(
- self.update_resource(
- key_store,
- storage_scheme,
- payment_method
- .clone()
- .update_with_payment_method_id(&conn, p_update.clone()),
- updated_payment_method,
- UpdateResourceParams {
- drainer_query_fut,
- operation: Op::Update(
- key.clone(),
- &field,
- payment_method.clone().updated_by.as_deref(),
- ),
- },
- ),
- )
+ let payment_method: DomainPaymentMethod = Box::pin(self.update_resource(
+ key_store,
+ storage_scheme,
+ update_resource_fut,
+ updated_payment_method,
+ UpdateResourceParams {
+ drainer_query_fut,
+ operation: Op::Update(
+ key.clone(),
+ &field,
+ payment_method.clone().updated_by.as_deref(),
+ ),
+ },
+ ))
.await?;
if let Some(compat_action) = compat_action {
diff --git a/crates/storage_impl/src/payments/payment_attempt.rs b/crates/storage_impl/src/payments/payment_attempt.rs
index 2d4c98a754..809eb700da 100644
--- a/crates/storage_impl/src/payments/payment_attempt.rs
+++ b/crates/storage_impl/src/payments/payment_attempt.rs
@@ -4,6 +4,8 @@ use common_utils::{
encryption::Encryption, errors::CustomResult, ext_traits::AsyncExt,
types::ConnectorTransactionId,
};
+#[cfg(feature = "v2")]
+use diesel_models::errors::DatabaseError;
use diesel_models::{
enums::{
MandateAmountData as DieselMandateAmountData, MandateDataType as DieselMandateType,
@@ -1148,8 +1150,6 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
};
let field = format!("{}_{}", label::CLUSTER_LABEL, this.id.get_string_repr());
- let conn = pg_connection_write(self).await?;
-
let payment_attempt_internal =
diesel_models::PaymentAttemptUpdateInternal::foreign_from(payment_attempt_update);
let updated_payment_attempt = payment_attempt_internal
@@ -1157,13 +1157,30 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
.apply_changeset(payment_attempt.clone());
let updated_by = updated_payment_attempt.updated_by.to_owned();
- let updated_payment_attempt_with_id = payment_attempt
- .clone()
- .update_with_attempt_id(&conn, payment_attempt_internal.clone());
+ let updated_payment_attempt_with_id = {
+ let payment_attempt = payment_attempt.clone();
+ let payment_attempt_internal = payment_attempt_internal.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ payment_attempt
+ .update_with_attempt_id(&conn, payment_attempt_internal)
+ .await
+ }
+ };
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = payment_attempt_internal
- .generate_drainer_update_query(&mut query_gen_conn, payment_attempt.id.clone());
+ let drainer_query_fut = {
+ let payment_attempt_id = payment_attempt.id.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ payment_attempt_internal
+ .generate_drainer_update_query(&mut conn, payment_attempt_id)
+ .await
+ }
+ };
Box::pin(self.update_resource(
merchant_key_store,
diff --git a/crates/storage_impl/src/payments/payment_intent.rs b/crates/storage_impl/src/payments/payment_intent.rs
index 87e6a6a4cf..499d7a6b72 100644
--- a/crates/storage_impl/src/payments/payment_intent.rs
+++ b/crates/storage_impl/src/payments/payment_intent.rs
@@ -8,6 +8,8 @@ use common_utils::{ext_traits::Encode, fallback_reverse_lookup_not_found};
#[cfg(feature = "olap")]
use diesel::{associations::HasTable, ExpressionMethods, JoinOnDsl, QueryDsl};
#[cfg(feature = "v1")]
+use diesel_models::errors::DatabaseError;
+#[cfg(feature = "v1")]
use diesel_models::payment_intent::PaymentIntentUpdate as DieselPaymentIntentUpdate;
#[cfg(feature = "v2")]
use diesel_models::payment_intent::PaymentIntentUpdateInternal;
@@ -88,22 +90,37 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
payment_id: &payment_id,
};
- let conn = pg_connection_write(self).await?;
let new_payment_intent = payment_intent
.construct_new()
.await
.change_context(StorageError::EncryptionError)?;
let diesel_payment_intent = DieselPaymentIntent::from(new_payment_intent.clone());
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = new_payment_intent
- .clone()
- .generate_drainer_insert_query(&mut query_gen_conn);
+ let create_resource_fut = {
+ let new_payment_intent = new_payment_intent.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ new_payment_intent.insert(&conn).await
+ }
+ };
+ let drainer_query_fut = {
+ let new_payment_intent = new_payment_intent.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ new_payment_intent
+ .generate_drainer_insert_query(&mut conn)
+ .await
+ }
+ };
Box::pin(self.insert_resource_old(
merchant_key_store,
storage_scheme,
- new_payment_intent.insert(&conn),
+ create_resource_fut,
diesel_payment_intent,
InsertResourceParams {
drainer_query_fut,
@@ -232,7 +249,6 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
payment_id: &payment_id,
};
let field = format!("pi_{}", this.get_id().get_string_repr());
- let conn = pg_connection_write(self).await?;
let diesel_intent_update = DieselPaymentIntentUpdate::from(payment_intent_update);
let origin_diesel_intent = this
.convert()
@@ -242,17 +258,36 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
.clone()
.apply_changeset(origin_diesel_intent.clone());
- let mut query_gen_conn = pg_connection_write(self).await?;
- let drainer_query_fut = diesel_intent_update.clone().generate_drainer_update_query(
- &mut query_gen_conn,
- origin_diesel_intent.payment_id.clone(),
- origin_diesel_intent.processor_merchant_id.clone(),
- );
+ let update_resource_fut = {
+ let origin_diesel_intent = origin_diesel_intent.clone();
+ let diesel_intent_update = diesel_intent_update.clone();
+ async move {
+ let conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ origin_diesel_intent
+ .update(&conn, diesel_intent_update)
+ .await
+ }
+ };
+ let drainer_query_fut = {
+ let diesel_intent_update = diesel_intent_update.clone();
+ let payment_id = origin_diesel_intent.payment_id.clone();
+ let processor_merchant_id = origin_diesel_intent.processor_merchant_id.clone();
+ async move {
+ let mut conn = pg_connection_write(self)
+ .await
+ .change_context(DatabaseError::DatabaseConnectionError)?;
+ diesel_intent_update
+ .generate_drainer_update_query(&mut conn, payment_id, processor_merchant_id)
+ .await
+ }
+ };
Box::pin(self.update_resource_old(
merchant_key_store,
storage_scheme,
- origin_diesel_intent.update(&conn, diesel_intent_update),
+ update_resource_fut,
diesel_intent,
UpdateResourceParams {
drainer_query_fut,