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(router): eliminate duplicate DB connection pools from building each tenant's store twice (#13395)

Sai Harsha Vardhan77ebe4474086
diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs
index 022af98461..0377b30d55 100644
--- a/crates/router/src/configs/settings.rs
+++ b/crates/router/src/configs/settings.rs
@@ -469,6 +469,10 @@ pub struct DecisionConfig {
pub base_url: String,
}
+type StorageInterfaceMap = HashMap<id_type::TenantId, Box<dyn app::StorageInterface>>;
+type AccountsStorageInterfaceMap =
+ HashMap<id_type::TenantId, Box<dyn app::AccountsStorageInterface>>;
+
#[derive(Debug, Clone, Default)]
pub struct TenantConfig(pub HashMap<id_type::TenantId, Tenant>);
@@ -476,72 +480,47 @@ impl TenantConfig {
/// # Panics
///
/// Panics if Failed to create event handler
- pub async fn get_store_interface_map(
+ pub async fn get_store_interface_maps(
&self,
storage_impl: &app::StorageImpl,
conf: &configs::Settings,
cache_store: Arc<storage_impl::redis::RedisStore>,
testable: bool,
- ) -> HashMap<id_type::TenantId, Box<dyn app::StorageInterface>> {
+ ) -> (StorageInterfaceMap, AccountsStorageInterfaceMap) {
#[allow(clippy::expect_used)]
let event_handler = conf
.events
.get_event_handler()
.await
.expect("Failed to create event handler");
- futures::future::join_all(self.0.iter().map(|(tenant_name, tenant)| async {
- let store = Box::pin(AppState::get_store_interface(
- storage_impl,
- &event_handler,
- conf,
- tenant,
- conf.master_database.clone().into_inner(),
- conf.accounts_database_config(),
- cache_store.clone(),
- testable,
- ))
- .await
- .get_storage_interface();
- (tenant_name.clone(), store)
- }))
- .await
- .into_iter()
- .collect()
- }
- /// # Panics
- ///
- /// Panics if Failed to create event handler
- pub async fn get_accounts_store_interface_map(
- &self,
- storage_impl: &app::StorageImpl,
- conf: &configs::Settings,
- cache_store: Arc<storage_impl::redis::RedisStore>,
- testable: bool,
- ) -> HashMap<id_type::TenantId, Box<dyn app::AccountsStorageInterface>> {
- #[allow(clippy::expect_used)]
- let event_handler = conf
- .events
- .get_event_handler()
- .await
- .expect("Failed to create event handler");
- futures::future::join_all(self.0.iter().map(|(tenant_name, tenant)| async {
- let store = Box::pin(AppState::get_store_interface(
- storage_impl,
- &event_handler,
- conf,
- tenant,
- conf.master_database.clone().into_inner(),
- conf.accounts_database_config(),
- cache_store.clone(),
- testable,
- ))
- .await
- .get_accounts_storage_interface();
- (tenant_name.clone(), store)
- }))
- .await
- .into_iter()
- .collect()
+ let tenant_stores =
+ futures::future::join_all(self.0.iter().map(|(tenant_name, tenant)| async {
+ let store = Box::pin(AppState::get_store_interface(
+ storage_impl,
+ &event_handler,
+ conf,
+ tenant,
+ conf.master_database.clone().into_inner(),
+ conf.accounts_database_config(),
+ cache_store.clone(),
+ testable,
+ ))
+ .await;
+ (tenant_name.clone(), store)
+ }))
+ .await;
+
+ let stores = tenant_stores
+ .iter()
+ .map(|(tenant_name, store)| (tenant_name.clone(), store.get_storage_interface()))
+ .collect();
+ let accounts_store = tenant_stores
+ .iter()
+ .map(|(tenant_name, store)| {
+ (tenant_name.clone(), store.get_accounts_storage_interface())
+ })
+ .collect();
+ (stores, accounts_store)
}
#[cfg(feature = "olap")]
pub async fn get_pools_map(
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index 32504a3c97..992fc4730a 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -491,20 +491,10 @@ impl AppState {
.tenants
.get_pools_map(conf.analytics.get_inner())
.await;
- let stores = conf
+ let (stores, accounts_store) = conf
.multitenancy
.tenants
- .get_store_interface_map(&storage_impl, &conf, Arc::clone(&cache_store), testable)
- .await;
- let accounts_store = conf
- .multitenancy
- .tenants
- .get_accounts_store_interface_map(
- &storage_impl,
- &conf,
- Arc::clone(&cache_store),
- testable,
- )
+ .get_store_interface_maps(&storage_impl, &conf, Arc::clone(&cache_store), testable)
.await;
#[cfg(feature = "email")]