mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 04:04:55 +08:00
Signed-off-by: chikke srujan <121822803+srujanchikke@users.noreply.github.com> Co-authored-by: Mani Chandra <84711804+ThisIsMani@users.noreply.github.com> Co-authored-by: Arjun Karthik <m.arjunkarthik@gmail.com> Co-authored-by: Sai Harsha Vardhan <56996463+sai-harsha-vardhan@users.noreply.github.com> Co-authored-by: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com> Co-authored-by: chikke srujan <121822803+srujanchikke@users.noreply.github.com> Co-authored-by: Prasunna Soppa <prasunna.soppa@juspay.in> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: DEEPANSHU BANSAL <41580413+deepanshu-iiitu@users.noreply.github.com> Co-authored-by: Arvind Patel <52006565+arvindpatel24@users.noreply.github.com> Co-authored-by: Jagan Elavarasan <jaganelavarasan@gmail.com> Co-authored-by: arvindpatel24 <arvind.patel@juspay.in> Co-authored-by: anji-reddy-j <125157119+anji-reddy-j@users.noreply.github.com> Co-authored-by: Hrithikesh <61539176+hrithikesh026@users.noreply.github.com> Co-authored-by: Apoorv Dixit <64925866+apoorvdixit88@users.noreply.github.com> Co-authored-by: Pa1NarK <69745008+pixincreate@users.noreply.github.com>
81 lines
2.5 KiB
Rust
81 lines
2.5 KiB
Rust
use bb8::PooledConnection;
|
|
use data_models::errors::StorageError;
|
|
use diesel::PgConnection;
|
|
use error_stack::{IntoReport, ResultExt};
|
|
|
|
use crate::{metrics, DatabaseStore};
|
|
|
|
pub async fn pg_connection_read<T: DatabaseStore>(
|
|
store: &T,
|
|
) -> error_stack::Result<
|
|
PooledConnection<'_, async_bb8_diesel::ConnectionManager<PgConnection>>,
|
|
StorageError,
|
|
> {
|
|
// If only OLAP is enabled get replica pool.
|
|
#[cfg(all(feature = "olap", not(feature = "oltp")))]
|
|
let pool = store.get_replica_pool();
|
|
|
|
// If either one of these are true we need to get master pool.
|
|
// 1. Only OLTP is enabled.
|
|
// 2. Both OLAP and OLTP is enabled.
|
|
// 3. Both OLAP and OLTP is disabled.
|
|
#[cfg(any(
|
|
all(not(feature = "olap"), feature = "oltp"),
|
|
all(feature = "olap", feature = "oltp"),
|
|
all(not(feature = "olap"), not(feature = "oltp"))
|
|
))]
|
|
let pool = store.get_master_pool();
|
|
|
|
pool.get()
|
|
.await
|
|
.into_report()
|
|
.change_context(StorageError::DatabaseConnectionError)
|
|
}
|
|
|
|
pub async fn pg_connection_write<T: DatabaseStore>(
|
|
store: &T,
|
|
) -> error_stack::Result<
|
|
PooledConnection<'_, async_bb8_diesel::ConnectionManager<PgConnection>>,
|
|
StorageError,
|
|
> {
|
|
// Since all writes should happen to master DB only choose master DB.
|
|
let pool = store.get_master_pool();
|
|
|
|
pool.get()
|
|
.await
|
|
.into_report()
|
|
.change_context(StorageError::DatabaseConnectionError)
|
|
}
|
|
|
|
pub async fn try_redis_get_else_try_database_get<F, RFut, DFut, T>(
|
|
redis_fut: RFut,
|
|
database_call_closure: F,
|
|
) -> error_stack::Result<T, StorageError>
|
|
where
|
|
F: FnOnce() -> DFut,
|
|
RFut: futures::Future<Output = error_stack::Result<T, redis_interface::errors::RedisError>>,
|
|
DFut: futures::Future<Output = error_stack::Result<T, StorageError>>,
|
|
{
|
|
let redis_output = redis_fut.await;
|
|
match redis_output {
|
|
Ok(output) => Ok(output),
|
|
Err(redis_error) => match redis_error.current_context() {
|
|
redis_interface::errors::RedisError::NotFound => {
|
|
metrics::KV_MISS.add(&metrics::CONTEXT, 1, &[]);
|
|
database_call_closure().await
|
|
}
|
|
_ => Err(redis_error.change_context(StorageError::KVError)),
|
|
},
|
|
}
|
|
}
|
|
|
|
/// Generates hscan field pattern. Suppose the field is pa_1234 it will generate
|
|
/// pa_*
|
|
pub fn generate_hscan_pattern_for_attempt(sk: &str) -> String {
|
|
sk.split('_')
|
|
.take(1)
|
|
.chain(["*"])
|
|
.collect::<Vec<&str>>()
|
|
.join("_")
|
|
}
|