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

chore(env): add ttl as env variable (#2653)

Kartikeya Hegde8b1499e12167
diff --git a/config/config.example.toml b/config/config.example.toml
index 5d95566f16..59083d6c71 100644
--- a/config/config.example.toml
+++ b/config/config.example.toml
@@ -435,4 +435,9 @@ apple_pay_merchant_cert_key = "APPLE_PAY_MERCHNAT_CERTIFICATE_KEY" #Private
[payment_link]
-sdk_url = "http://localhost:9090/dist/HyperLoader.js"
\ No newline at end of file
+sdk_url = "http://localhost:9090/dist/HyperLoader.js"
+
+# Config for KV setup
+[kv_config]
+# TTL for KV in seconds
+ttl = 900
diff --git a/config/development.toml b/config/development.toml
index 7cb2d0f869..5e74eafcb4 100644
--- a/config/development.toml
+++ b/config/development.toml
@@ -448,4 +448,7 @@ sdk_url = "http://localhost:9090/dist/HyperLoader.js"
[lock_settings]
redis_lock_expiry_seconds = 180 # 3 * 60 seconds
-delay_between_retries_in_milliseconds = 500
\ No newline at end of file
+delay_between_retries_in_milliseconds = 500
+
+[kv_config]
+ttl = 900 # 15 * 60 seconds
diff --git a/config/docker_compose.toml b/config/docker_compose.toml
index c01888ea5b..20ca175ceb 100644
--- a/config/docker_compose.toml
+++ b/config/docker_compose.toml
@@ -315,4 +315,7 @@ supported_connectors = "braintree"
[lock_settings]
redis_lock_expiry_seconds = 180 # 3 * 60 seconds
-delay_between_retries_in_milliseconds = 500
\ No newline at end of file
+delay_between_retries_in_milliseconds = 500
+
+[kv_config]
+ttl = 900 # 15 * 60 seconds
diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.toml
index fd8b21520b..81b23314ff 100644
--- a/crates/router/Cargo.toml
+++ b/crates/router/Cargo.toml
@@ -15,7 +15,7 @@ kms = ["external_services/kms", "dep:aws-config"]
email = ["external_services/email", "dep:aws-config"]
basilisk = ["kms"]
stripe = ["dep:serde_qs"]
-release = ["kms", "stripe", "basilisk", "s3", "email","accounts_cache"]
+release = ["kms", "stripe","basilisk","s3", "email","accounts_cache","kv_store"]
olap = ["data_models/olap", "storage_impl/olap", "scheduler/olap"]
oltp = ["data_models/oltp", "storage_impl/oltp"]
kv_store = ["scheduler/kv_store"]
diff --git a/crates/router/src/configs/defaults.rs b/crates/router/src/configs/defaults.rs
index 3bb1c31d18..8d58037343 100644
--- a/crates/router/src/configs/defaults.rs
+++ b/crates/router/src/configs/defaults.rs
@@ -103,6 +103,13 @@ impl Default for super::settings::DrainerSettings {
}
}
+#[cfg(feature = "kv_store")]
+impl Default for super::settings::KvConfig {
+ fn default() -> Self {
+ Self { ttl: 900 }
+ }
+}
+
use super::settings::{
Mandates, SupportedConnectorsForMandate, SupportedPaymentMethodTypesForMandate,
SupportedPaymentMethodsForMandate,
diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs
index b2c27ed398..204060b37a 100644
--- a/crates/router/src/configs/settings.rs
+++ b/crates/router/src/configs/settings.rs
@@ -101,6 +101,13 @@ pub struct Settings {
pub lock_settings: LockSettings,
pub temp_locker_enable_config: TempLockerEnableConfig,
pub payment_link: PaymentLink,
+ #[cfg(feature = "kv_store")]
+ pub kv_config: KvConfig,
+}
+
+#[derive(Debug, Deserialize, Clone)]
+pub struct KvConfig {
+ pub ttl: u32,
}
#[derive(Debug, Deserialize, Clone, Default)]
diff --git a/crates/router/src/services.rs b/crates/router/src/services.rs
index d216d61642..631e9a5c18 100644
--- a/crates/router/src/services.rs
+++ b/crates/router/src/services.rs
@@ -90,6 +90,7 @@ pub async fn get_store(
store,
config.drainer.stream_name.clone(),
config.drainer.num_partitions,
+ config.kv_config.ttl,
);
Ok(store)
diff --git a/crates/storage_impl/src/consts.rs b/crates/storage_impl/src/consts.rs
deleted file mode 100644
index 04eab6176f..0000000000
--- a/crates/storage_impl/src/consts.rs
+++ /dev/null
@@ -1,2 +0,0 @@
-// TTL for KV setup
-pub(crate) const KV_TTL: u32 = 300;
diff --git a/crates/storage_impl/src/lib.rs b/crates/storage_impl/src/lib.rs
index dd8d71fc70..17d432c793 100644
--- a/crates/storage_impl/src/lib.rs
+++ b/crates/storage_impl/src/lib.rs
@@ -9,7 +9,6 @@ mod address;
pub mod config;
pub mod connection;
mod connector_response;
-mod consts;
pub mod database;
pub mod errors;
mod lookup;
@@ -138,6 +137,7 @@ pub struct KVRouterStore<T: DatabaseStore> {
router_store: RouterStore<T>,
drainer_stream_name: String,
drainer_num_partitions: u8,
+ ttl_for_kv: u32,
}
#[async_trait::async_trait]
@@ -146,13 +146,14 @@ where
RouterStore<T>: DatabaseStore,
T: DatabaseStore,
{
- type Config = (RouterStore<T>, String, u8);
+ type Config = (RouterStore<T>, String, u8, u32);
async fn new(config: Self::Config, _test_transaction: bool) -> StorageResult<Self> {
- let (router_store, drainer_stream_name, drainer_num_partitions) = config;
+ let (router_store, drainer_stream_name, drainer_num_partitions, ttl_for_kv) = config;
Ok(Self::from_store(
router_store,
drainer_stream_name,
drainer_num_partitions,
+ ttl_for_kv,
))
}
fn get_master_pool(&self) -> &PgPool {
@@ -176,11 +177,13 @@ impl<T: DatabaseStore> KVRouterStore<T> {
store: RouterStore<T>,
drainer_stream_name: String,
drainer_num_partitions: u8,
+ ttl_for_kv: u32,
) -> Self {
Self {
router_store: store,
drainer_stream_name,
drainer_num_partitions,
+ ttl_for_kv,
}
}
diff --git a/crates/storage_impl/src/redis/kv_store.rs b/crates/storage_impl/src/redis/kv_store.rs
index 52f58f6a75..0c615d74f8 100644
--- a/crates/storage_impl/src/redis/kv_store.rs
+++ b/crates/storage_impl/src/redis/kv_store.rs
@@ -6,7 +6,7 @@ use router_derive::TryGetEnumVariant;
use router_env::logger;
use serde::de;
-use crate::{consts, metrics, store::kv::TypedSql, KVRouterStore};
+use crate::{metrics, store::kv::TypedSql, KVRouterStore};
pub trait KvStorePartition {
fn partition_number(key: PartitionKey<'_>, num_partitions: u8) -> u32 {
@@ -102,6 +102,8 @@ where
let type_name = std::any::type_name::<T>();
let operation = op.to_string();
+ let ttl = store.ttl_for_kv;
+
let partition_key = PartitionKey::MerchantIdPaymentIdCombination { combination: key };
let result = async {
@@ -109,9 +111,7 @@ where
KvOperation::Hset(value, sql) => {
logger::debug!(kv_operation= %operation, value = ?value);
- redis_conn
- .set_hash_fields(key, value, Some(consts::KV_TTL))
- .await?;
+ redis_conn.set_hash_fields(key, value, Some(ttl)).await?;
store
.push_to_drainer_stream::<S>(sql, partition_key)
@@ -136,12 +136,7 @@ where
logger::debug!(kv_operation= %operation, value = ?value);
let result = redis_conn
- .serialize_and_set_hash_field_if_not_exist(
- key,
- field,
- value,
- Some(consts::KV_TTL),
- )
+ .serialize_and_set_hash_field_if_not_exist(key, field, value, Some(ttl))
.await?;
if matches!(result, redis_interface::HsetnxReply::KeySet) {
@@ -156,7 +151,7 @@ where
logger::debug!(kv_operation= %operation, value = ?value);
let result = redis_conn
- .serialize_and_set_key_if_not_exist(key, value, Some(consts::KV_TTL.into()))
+ .serialize_and_set_key_if_not_exist(key, value, Some(ttl.into()))
.await?;
if matches!(result, redis_interface::SetnxReply::KeySet) {
diff --git a/loadtest/config/development.toml b/loadtest/config/development.toml
index c2ecd68dc8..7cdbc8dd6f 100644
--- a/loadtest/config/development.toml
+++ b/loadtest/config/development.toml
@@ -234,3 +234,6 @@ card.debit = {connector_list = "stripe,adyen,authorizedotnet,globalpay,worldpay,
bank_debit.ach = { connector_list = "gocardless"}
bank_debit.becs = { connector_list = "gocardless"}
bank_debit.sepa = { connector_list = "gocardless"}
+
+[kv_config]
+ttl = 300 # 5 * 60 seconds