Skip to content

Commit cc5b089

Browse files
committed
feat: db
1 parent 0dcafc4 commit cc5b089

File tree

7 files changed

+64
-28
lines changed

7 files changed

+64
-28
lines changed

frameworks/Rust/hyperlane/Cargo.lock

Lines changed: 39 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/hyperlane/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exclude = [
1919

2020
[dependencies]
2121
hyperlane = "4.36.1"
22-
nanorand = "0.7.0"
22+
rand = "0.9.0"
2323
serde = "1.0.219"
2424
sqlx = { version = "0.8.3", features = ["runtime-tokio", "postgres"] }
2525

frameworks/Rust/hyperlane/src/constant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub static TABLE_NAME_WORLD: &str = "World";
99
pub static TABLE_NAME_FORTUNE: &str = "Fortune";
1010
pub static ROW_LIMIT: i32 = 500;
1111
pub static RANDOM_MAX: i32 = 10_000;
12+
pub static RANDOM_MAX_ADD_ONE: u32 = 10_001;
1213
pub static KEY_ID: &str = "id";
1314
pub static KEY_RANDOM_NUMBER: &str = "randomnumber";
1415
pub static KEY_MESSAGE: &str = "message";

frameworks/Rust/hyperlane/src/db.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub async fn insert_records() {
6060
let missing_count: i64 = limit - count;
6161
let mut values: Vec<String> = Vec::new();
6262
for _ in 0..missing_count {
63-
let random_number: i32 = get_random_id().await;
63+
let random_number: i32 = get_random_id();
6464
values.push(format!("(DEFAULT, {})", random_number));
6565
}
6666
let sql: String = format!(
@@ -71,7 +71,7 @@ pub async fn insert_records() {
7171
let _ = query(&sql).execute(&db_pool).await;
7272
let mut values: Vec<String> = Vec::new();
7373
for _ in 0..missing_count {
74-
let random_number: i32 = get_random_id().await;
74+
let random_number: i32 = get_random_id();
7575
values.push(format!("(DEFAULT, {})", random_number));
7676
}
7777
let sql: String = format!(
@@ -116,9 +116,19 @@ pub async fn connection_db() -> DbPoolConnection {
116116
),
117117
};
118118
let pool_size: u32 = (get_thread_count() >> 2).max(10).min(100) as u32;
119+
let max_pool_size: u32 = option_env!("POSTGRES_MAX_POOL_SIZE")
120+
.unwrap_or(&pool_size.to_string())
121+
.parse::<u32>()
122+
.unwrap_or(pool_size);
123+
let min_pool_size: u32 = option_env!("POSTGRES_MIN_POOL_SIZE")
124+
.unwrap_or(&pool_size.to_string())
125+
.parse::<u32>()
126+
.unwrap_or(pool_size);
119127
let pool: DbPoolConnection = PgPoolOptions::new()
120-
.max_connections(pool_size)
128+
.max_connections(max_pool_size)
129+
.min_connections(min_pool_size)
121130
.max_lifetime(None)
131+
.test_before_acquire(false)
122132
.idle_timeout(None)
123133
.connect(db_url)
124134
.await
@@ -137,7 +147,7 @@ pub async fn get_update_data(limit: Queries) -> (String, Vec<QueryRow>) {
137147
let mut id_in_clause: String = format!("{}", rows[0].id);
138148
let last_idx: usize = rows.len() - 1;
139149
for (i, row) in rows.iter().enumerate() {
140-
let new_random_number: Queries = get_random_id().await;
150+
let new_random_number: Queries = get_random_id();
141151
let id: i32 = row.id;
142152
id_list.push(id);
143153
value_list.push_str(&format!("WHEN {} THEN {} ", id, new_random_number));
@@ -171,14 +181,14 @@ pub async fn init_db() {
171181

172182
#[inline]
173183
pub async fn random_world_row(db_pool: &DbPoolConnection) -> QueryRow {
174-
let random_id: Queries = get_random_id().await;
184+
let random_id: Queries = get_random_id();
175185
query_world_row(db_pool, random_id).await
176186
}
177187

178188
#[inline]
179189
pub async fn query_world_row(db_pool: &DbPoolConnection, id: Queries) -> QueryRow {
180190
let sql: String = format!(
181-
"SELECT id, randomNumber FROM {} WHERE id = {}",
191+
"SELECT id, randomNumber FROM {} WHERE id = {} LIMIT 1",
182192
TABLE_NAME_WORLD, id
183193
);
184194
if let Ok(rows) = query(&sql).fetch_one(db_pool).await {
@@ -208,7 +218,7 @@ pub async fn all_world_row() -> Vec<PgRow> {
208218
pub async fn get_some_row_id(limit: Queries, db_pool: &DbPoolConnection) -> Vec<QueryRow> {
209219
let mut res: Vec<QueryRow> = Vec::with_capacity(limit as usize);
210220
for _ in 0..limit {
211-
let id: i32 = get_random_id().await;
221+
let id: i32 = get_random_id();
212222
let tem: QueryRow = query_world_row(db_pool, id).await;
213223
res.push(tem);
214224
}

frameworks/Rust/hyperlane/src/lazy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ use crate::*;
33
pub static DB: Lazy<ArcRwLock<Option<DbPoolConnection>>> =
44
Lazy::new(|| Arc::new(RwLock::new(None)));
55
pub static CACHE: Lazy<ArcRwLock<Vec<QueryRow>>> = Lazy::new(|| arc_rwlock(vec![]));
6-
pub static RAND: Lazy<ArcRwLock<WyRand>> = Lazy::new(|| arc_rwlock(WyRand::new()));

frameworks/Rust/hyperlane/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub(crate) use hyperlane::{
1818
*,
1919
};
2020
pub(crate) use lazy::*;
21-
pub(crate) use nanorand::{Rng, WyRand};
2221
pub(crate) use r#type::*;
22+
pub(crate) use rand::{rngs::SmallRng, Rng, SeedableRng};
2323
pub(crate) use request_middleware::*;
2424
pub(crate) use response_middleware::*;
2525
pub(crate) use route::*;

frameworks/Rust/hyperlane/src/utils.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rand::rng;
2+
13
use crate::*;
24

35
#[inline]
@@ -17,8 +19,8 @@ pub fn escape_html(input: &str) -> String {
1719
}
1820

1921
#[inline]
20-
pub async fn get_random_id() -> Queries {
21-
let mut rand: RwLockWriteGuard<'_, WyRand> = RAND.write().await;
22-
let random_id: u32 = rand.generate::<u32>() % RANDOM_MAX as u32 + 1;
22+
pub fn get_random_id() -> Queries {
23+
let mut rng: SmallRng = SmallRng::from_rng(&mut rng());
24+
let random_id: u32 = rng.random_range(1..RANDOM_MAX_ADD_ONE);
2325
random_id as Queries
2426
}

0 commit comments

Comments
 (0)