Skip to content

Commit

Permalink
chore(env): add ttl as env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dracarys18 committed Oct 20, 2023
1 parent cc0b422 commit 0059db8
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 20 deletions.
7 changes: 6 additions & 1 deletion config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,9 @@ apple_pay_merchant_cert_key = "APPLE_PAY_MERCHNAT_CERTIFICATE_KEY" #Private


[payment_link]
sdk_url = "http://localhost:9090/dist/HyperLoader.js"
sdk_url = "http://localhost:9090/dist/HyperLoader.js"

# Config for KV setup
[kv_config]
# TTL for KV in seconds
ttl = 900
5 changes: 4 additions & 1 deletion config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,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
delay_between_retries_in_milliseconds = 500

[kv_config]
ttl = 900 # 15 * 60 seconds
5 changes: 4 additions & 1 deletion config/docker_compose.toml
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,7 @@ supported_connectors = "braintree"

[lock_settings]
redis_lock_expiry_seconds = 180 # 3 * 60 seconds
delay_between_retries_in_milliseconds = 500
delay_between_retries_in_milliseconds = 500

[kv_config]
ttl = 900 # 15 * 60 seconds
2 changes: 1 addition & 1 deletion crates/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ s3 = ["dep:aws-sdk-s3", "dep:aws-config"]
kms = ["external_services/kms", "dep:aws-config"]
email = ["external_services/email", "dep:aws-config"]
stripe = ["dep:serde_qs"]
release = ["kms", "stripe", "s3", "email","accounts_cache"]
release = ["kms", "stripe", "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"]
Expand Down
7 changes: 7 additions & 0 deletions crates/router/src/configs/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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,
Expand Down
7 changes: 7 additions & 0 deletions crates/router/src/configs/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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)]
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions crates/storage_impl/src/consts.rs

This file was deleted.

9 changes: 6 additions & 3 deletions crates/storage_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand All @@ -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 {
Expand All @@ -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,
}
}

Expand Down
17 changes: 6 additions & 11 deletions crates/storage_impl/src/redis/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -102,16 +102,16 @@ 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 {
match op {
KvOperation::Hset(value, sql) => {
logger::debug!("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)
Expand All @@ -136,12 +136,7 @@ where
logger::debug!("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) {
Expand All @@ -156,7 +151,7 @@ where
logger::debug!("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) {
Expand Down
3 changes: 3 additions & 0 deletions loadtest/config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,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

0 comments on commit 0059db8

Please sign in to comment.