You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

1 year ago
use std::sync::Arc;
use deadpool_postgres::{Pool, Config, Runtime};
use tokio::sync::OnceCell;
use tokio_postgres::NoTls;
use crate::config::get_config;
static CONNECTION_POOL: OnceCell<Arc<Pool>> = OnceCell::const_new();
1 year ago
static CAMPAIGN_CONNECTION_POOL: OnceCell<Arc<Pool>> = OnceCell::const_new();
1 year ago
1 year ago
async fn initialize_connection_pool() -> Arc<Pool> {
1 year ago
let config = get_config().await;
let mut pg = Config::new();
pg.host = Some(config.database.host.clone());
pg.user = Some(config.database.user.clone());
pg.password = Some(config.database.password.clone());
pg.dbname = Some(config.database.name.clone());
pg.port = Some(config.database.port as u16);
let pool = pg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap();
Arc::new(pool)
}
pub async fn get_connection_pool() -> Arc<Pool> {
1 year ago
CONNECTION_POOL.get_or_init(initialize_connection_pool)
.await
.clone()
}
async fn initialize_campaign_connection_pool() -> Arc<Pool> {
let config = get_config().await;
let mut pg = Config::new();
pg.host = Some(config.database.host.clone());
pg.user = Some(config.database.user.clone());
pg.password = Some(config.database.password.clone());
pg.dbname = Some(config.database.name.clone());
pg.port = Some(config.database.port as u16);
let pool = pg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap();
Arc::new(pool)
}
pub async fn get_campaign_connection_pool() -> Arc<Pool> {
CAMPAIGN_CONNECTION_POOL.get_or_init(initialize_campaign_connection_pool)
1 year ago
.await
.clone()
}