mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 21:07:58 +08:00
refactor(scheduler): move scheduler to new crate to support workflows in multiple crates (#1681)
This commit is contained in:
59
crates/storage_impl/src/connection.rs
Normal file
59
crates/storage_impl/src/connection.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use bb8::PooledConnection;
|
||||
use common_utils::errors;
|
||||
use diesel::PgConnection;
|
||||
use error_stack::{IntoReport, ResultExt};
|
||||
|
||||
pub type PgPool = bb8::Pool<async_bb8_diesel::ConnectionManager<PgConnection>>;
|
||||
|
||||
pub type PgPooledConn = async_bb8_diesel::Connection<PgConnection>;
|
||||
|
||||
#[allow(clippy::expect_used)]
|
||||
pub async fn redis_connection(
|
||||
redis: &redis_interface::RedisSettings,
|
||||
) -> redis_interface::RedisConnectionPool {
|
||||
redis_interface::RedisConnectionPool::new(redis)
|
||||
.await
|
||||
.expect("Failed to create Redis Connection Pool")
|
||||
}
|
||||
|
||||
pub async fn pg_connection_read<T: crate::DatabaseStore>(
|
||||
store: &T,
|
||||
) -> errors::CustomResult<
|
||||
PooledConnection<'_, async_bb8_diesel::ConnectionManager<PgConnection>>,
|
||||
crate::errors::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(crate::errors::StorageError::DatabaseConnectionError)
|
||||
}
|
||||
|
||||
pub async fn pg_connection_write<T: crate::DatabaseStore>(
|
||||
store: &T,
|
||||
) -> errors::CustomResult<
|
||||
PooledConnection<'_, async_bb8_diesel::ConnectionManager<PgConnection>>,
|
||||
crate::errors::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(crate::errors::StorageError::DatabaseConnectionError)
|
||||
}
|
||||
Reference in New Issue
Block a user