|
@@ -1,6 +1,5 @@
|
|
use crate::*;
|
|
use crate::*;
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn get_db_connection() -> DbPoolConnection {
|
|
pub async fn get_db_connection() -> DbPoolConnection {
|
|
if let Some(db_pool) = DB.get() {
|
|
if let Some(db_pool) = DB.get() {
|
|
return db_pool.clone();
|
|
return db_pool.clone();
|
|
@@ -11,7 +10,6 @@ pub async fn get_db_connection() -> DbPoolConnection {
|
|
db_pool
|
|
db_pool
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
#[cfg(feature = "dev")]
|
|
#[cfg(feature = "dev")]
|
|
pub async fn create_database() {
|
|
pub async fn create_database() {
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
@@ -20,7 +18,6 @@ pub async fn create_database() {
|
|
.await;
|
|
.await;
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
#[cfg(feature = "dev")]
|
|
#[cfg(feature = "dev")]
|
|
pub async fn create_table() {
|
|
pub async fn create_table() {
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
@@ -42,7 +39,6 @@ pub async fn create_table() {
|
|
.await;
|
|
.await;
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
#[cfg(feature = "dev")]
|
|
#[cfg(feature = "dev")]
|
|
pub async fn insert_records() {
|
|
pub async fn insert_records() {
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
@@ -80,7 +76,6 @@ pub async fn insert_records() {
|
|
let _ = query(&sql).execute(&db_pool).await;
|
|
let _ = query(&sql).execute(&db_pool).await;
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn init_cache() {
|
|
pub async fn init_cache() {
|
|
let mut res: Vec<QueryRow> = Vec::with_capacity(RANDOM_MAX as usize);
|
|
let mut res: Vec<QueryRow> = Vec::with_capacity(RANDOM_MAX as usize);
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
@@ -98,7 +93,6 @@ pub async fn init_cache() {
|
|
let _ = CACHE.set(res);
|
|
let _ = CACHE.set(res);
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn connection_db() -> DbPoolConnection {
|
|
pub async fn connection_db() -> DbPoolConnection {
|
|
let db_url: &str = match option_env!("POSTGRES_URL") {
|
|
let db_url: &str = match option_env!("POSTGRES_URL") {
|
|
Some(it) => it,
|
|
Some(it) => it,
|
|
@@ -112,7 +106,7 @@ pub async fn connection_db() -> DbPoolConnection {
|
|
DATABASE_NAME
|
|
DATABASE_NAME
|
|
),
|
|
),
|
|
};
|
|
};
|
|
- let pool_size: u32 = (get_thread_count() >> 2).max(10).min(100) as u32;
|
|
|
|
|
|
+ let pool_size: u32 = (get_thread_count() << 2).max(10).min(100) as u32;
|
|
let max_pool_size: u32 = option_env!("POSTGRES_MAX_POOL_SIZE")
|
|
let max_pool_size: u32 = option_env!("POSTGRES_MAX_POOL_SIZE")
|
|
.unwrap_or(&pool_size.to_string())
|
|
.unwrap_or(&pool_size.to_string())
|
|
.parse::<u32>()
|
|
.parse::<u32>()
|
|
@@ -133,7 +127,6 @@ pub async fn connection_db() -> DbPoolConnection {
|
|
pool
|
|
pool
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn get_update_data(
|
|
pub async fn get_update_data(
|
|
limit: Queries,
|
|
limit: Queries,
|
|
) -> (String, Vec<QueryRow>, Vec<Queries>, Vec<Queries>) {
|
|
) -> (String, Vec<QueryRow>, Vec<Queries>, Vec<Queries>) {
|
|
@@ -165,7 +158,6 @@ pub async fn get_update_data(
|
|
(sql, query_res_list, id_list, random_numbers)
|
|
(sql, query_res_list, id_list, random_numbers)
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn init_db() {
|
|
pub async fn init_db() {
|
|
get_db_connection().await;
|
|
get_db_connection().await;
|
|
#[cfg(feature = "dev")]
|
|
#[cfg(feature = "dev")]
|
|
@@ -177,13 +169,11 @@ pub async fn init_db() {
|
|
init_cache().await;
|
|
init_cache().await;
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn random_world_row(db_pool: &DbPoolConnection) -> QueryRow {
|
|
pub async fn random_world_row(db_pool: &DbPoolConnection) -> QueryRow {
|
|
let random_id: Queries = get_random_id();
|
|
let random_id: Queries = get_random_id();
|
|
query_world_row(db_pool, random_id).await
|
|
query_world_row(db_pool, random_id).await
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn query_world_row(db_pool: &DbPoolConnection, id: Queries) -> QueryRow {
|
|
pub async fn query_world_row(db_pool: &DbPoolConnection, id: Queries) -> QueryRow {
|
|
let sql: String = format!(
|
|
let sql: String = format!(
|
|
"SELECT id, randomNumber FROM {} WHERE id = {} LIMIT 1",
|
|
"SELECT id, randomNumber FROM {} WHERE id = {} LIMIT 1",
|
|
@@ -196,7 +186,6 @@ pub async fn query_world_row(db_pool: &DbPoolConnection, id: Queries) -> QueryRo
|
|
return QueryRow::new(id as i32, 1);
|
|
return QueryRow::new(id as i32, 1);
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn update_world_rows(limit: Queries) -> Vec<QueryRow> {
|
|
pub async fn update_world_rows(limit: Queries) -> Vec<QueryRow> {
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let (sql, data, id_list, random_numbers) = get_update_data(limit).await;
|
|
let (sql, data, id_list, random_numbers) = get_update_data(limit).await;
|
|
@@ -211,7 +200,6 @@ pub async fn update_world_rows(limit: Queries) -> Vec<QueryRow> {
|
|
data
|
|
data
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn all_world_row() -> Vec<PgRow> {
|
|
pub async fn all_world_row() -> Vec<PgRow> {
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let sql: String = format!("SELECT id, message FROM {}", TABLE_NAME_FORTUNE);
|
|
let sql: String = format!("SELECT id, message FROM {}", TABLE_NAME_FORTUNE);
|
|
@@ -219,13 +207,12 @@ pub async fn all_world_row() -> Vec<PgRow> {
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
-#[inline]
|
|
|
|
pub async fn get_some_row_id(limit: Queries, db_pool: &DbPoolConnection) -> Vec<QueryRow> {
|
|
pub async fn get_some_row_id(limit: Queries, db_pool: &DbPoolConnection) -> Vec<QueryRow> {
|
|
- let mut res: Vec<QueryRow> = Vec::with_capacity(limit as usize);
|
|
|
|
- for _ in 0..limit {
|
|
|
|
- let id: i32 = get_random_id();
|
|
|
|
- let tem: QueryRow = query_world_row(db_pool, id).await;
|
|
|
|
- res.push(tem);
|
|
|
|
- }
|
|
|
|
- res
|
|
|
|
|
|
+ let futures: Vec<_> = (0..limit)
|
|
|
|
+ .map(|_| async {
|
|
|
|
+ let id: i32 = get_random_id();
|
|
|
|
+ query_world_row(db_pool, id).await
|
|
|
|
+ })
|
|
|
|
+ .collect();
|
|
|
|
+ join_all(futures).await
|
|
}
|
|
}
|