Skip to content

Commit 31ba915

Browse files
committed
feat: async
1 parent 07ad0ee commit 31ba915

File tree

8 files changed

+20
-46
lines changed

8 files changed

+20
-46
lines changed

frameworks/Rust/hyperlane/src/db.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::*;
22

3-
#[inline]
43
pub async fn get_db_connection() -> DbPoolConnection {
54
if let Some(db_pool) = DB.get() {
65
return db_pool.clone();
@@ -11,7 +10,6 @@ pub async fn get_db_connection() -> DbPoolConnection {
1110
db_pool
1211
}
1312

14-
#[inline]
1513
#[cfg(feature = "dev")]
1614
pub async fn create_database() {
1715
let db_pool: DbPoolConnection = get_db_connection().await;
@@ -20,7 +18,6 @@ pub async fn create_database() {
2018
.await;
2119
}
2220

23-
#[inline]
2421
#[cfg(feature = "dev")]
2522
pub async fn create_table() {
2623
let db_pool: DbPoolConnection = get_db_connection().await;
@@ -42,7 +39,6 @@ pub async fn create_table() {
4239
.await;
4340
}
4441

45-
#[inline]
4642
#[cfg(feature = "dev")]
4743
pub async fn insert_records() {
4844
let db_pool: DbPoolConnection = get_db_connection().await;
@@ -80,7 +76,6 @@ pub async fn insert_records() {
8076
let _ = query(&sql).execute(&db_pool).await;
8177
}
8278

83-
#[inline]
8479
pub async fn init_cache() {
8580
let mut res: Vec<QueryRow> = Vec::with_capacity(RANDOM_MAX as usize);
8681
let db_pool: DbPoolConnection = get_db_connection().await;
@@ -98,7 +93,6 @@ pub async fn init_cache() {
9893
let _ = CACHE.set(res);
9994
}
10095

101-
#[inline]
10296
pub async fn connection_db() -> DbPoolConnection {
10397
let db_url: &str = match option_env!("POSTGRES_URL") {
10498
Some(it) => it,
@@ -133,7 +127,6 @@ pub async fn connection_db() -> DbPoolConnection {
133127
pool
134128
}
135129

136-
#[inline]
137130
pub async fn get_update_data(
138131
limit: Queries,
139132
) -> (String, Vec<QueryRow>, Vec<Queries>, Vec<Queries>) {
@@ -165,7 +158,6 @@ pub async fn get_update_data(
165158
(sql, query_res_list, id_list, random_numbers)
166159
}
167160

168-
#[inline]
169161
pub async fn init_db() {
170162
get_db_connection().await;
171163
#[cfg(feature = "dev")]
@@ -177,13 +169,11 @@ pub async fn init_db() {
177169
init_cache().await;
178170
}
179171

180-
#[inline]
181172
pub async fn random_world_row(db_pool: &DbPoolConnection) -> QueryRow {
182173
let random_id: Queries = get_random_id();
183174
query_world_row(db_pool, random_id).await
184175
}
185176

186-
#[inline]
187177
pub async fn query_world_row(db_pool: &DbPoolConnection, id: Queries) -> QueryRow {
188178
let sql: String = format!(
189179
"SELECT id, randomNumber FROM {} WHERE id = {} LIMIT 1",
@@ -196,7 +186,6 @@ pub async fn query_world_row(db_pool: &DbPoolConnection, id: Queries) -> QueryRo
196186
return QueryRow::new(id as i32, 1);
197187
}
198188

199-
#[inline]
200189
pub async fn update_world_rows(limit: Queries) -> Vec<QueryRow> {
201190
let db_pool: DbPoolConnection = get_db_connection().await;
202191
let (sql, data, id_list, random_numbers) = get_update_data(limit).await;
@@ -211,21 +200,19 @@ pub async fn update_world_rows(limit: Queries) -> Vec<QueryRow> {
211200
data
212201
}
213202

214-
#[inline]
215203
pub async fn all_world_row() -> Vec<PgRow> {
216204
let db_pool: DbPoolConnection = get_db_connection().await;
217205
let sql: String = format!("SELECT id, message FROM {}", TABLE_NAME_FORTUNE);
218206
let res: Vec<PgRow> = query(&sql).fetch_all(&db_pool).await.unwrap_or_default();
219207
return res;
220208
}
221209

222-
#[inline]
223210
pub async fn get_some_row_id(limit: Queries, db_pool: &DbPoolConnection) -> Vec<QueryRow> {
224-
let mut res: Vec<QueryRow> = Vec::with_capacity(limit as usize);
225-
for _ in 0..limit {
226-
let id: i32 = get_random_id();
227-
let tem: QueryRow = query_world_row(db_pool, id).await;
228-
res.push(tem);
229-
}
230-
res
211+
let futures: Vec<_> = (0..limit)
212+
.map(|_| async {
213+
let id: i32 = get_random_id();
214+
query_world_row(db_pool, id).await
215+
})
216+
.collect();
217+
join_all(futures).await
231218
}

frameworks/Rust/hyperlane/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) mod utils;
1010

1111
pub(crate) use constant::*;
1212
pub(crate) use db::*;
13+
pub(crate) use futures::future::join_all;
1314
pub(crate) use hyperlane::{once_cell::sync::OnceCell, serde::*, serde_json::json, *};
1415
pub(crate) use lazy::*;
1516
pub(crate) use rand::{Rng, SeedableRng, rng, rngs::SmallRng};

frameworks/Rust/hyperlane/src/request_middleware.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::*;
22

3-
#[inline]
43
pub async fn request(controller_data: ControllerData) {
54
let _ = controller_data
65
.set_response_header(CONNECTION, CONNECTION_KEEP_ALIVE)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::*;
22

3-
#[inline]
43
pub async fn response(controller_data: ControllerData) {
54
let _ = controller_data.send().await;
65
}

frameworks/Rust/hyperlane/src/route.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::*;
22

3-
#[inline]
43
pub async fn json(controller_data: ControllerData) {
54
let json: serde_json::Value = json!({
65
"message": RESPONSEDATA
@@ -10,7 +9,6 @@ pub async fn json(controller_data: ControllerData) {
109
.await;
1110
}
1211

13-
#[inline]
1412
pub async fn plaintext(controller_data: ControllerData) {
1513
let _ = controller_data
1614
.set_response_header(CONTENT_TYPE, TEXT_PLAIN)
@@ -19,7 +17,6 @@ pub async fn plaintext(controller_data: ControllerData) {
1917
.await;
2018
}
2119

22-
#[inline]
2320
pub async fn db(controller_data: ControllerData) {
2421
let db_connection: DbPoolConnection = get_db_connection().await;
2522
let query_row: QueryRow = random_world_row(&db_connection).await;
@@ -28,7 +25,6 @@ pub async fn db(controller_data: ControllerData) {
2825
.await;
2926
}
3027

31-
#[inline]
3228
pub async fn queries(controller_data: ControllerData) {
3329
let queries: Queries = controller_data
3430
.get_request_query("q")
@@ -37,18 +33,17 @@ pub async fn queries(controller_data: ControllerData) {
3733
.unwrap_or_default()
3834
.min(ROW_LIMIT as Queries)
3935
.max(1);
40-
let mut data: Vec<QueryRow> = Vec::with_capacity(queries as usize);
4136
let db_pool: DbPoolConnection = get_db_connection().await;
42-
for _ in 0..queries {
43-
let row: QueryRow = random_world_row(&db_pool).await;
44-
data.push(row);
45-
}
37+
for _ in 0..queries {}
38+
let futures: Vec<_> = (0..queries)
39+
.map(|_| async { random_world_row(&db_pool).await })
40+
.collect();
41+
let data = join_all(futures).await;
4642
let _ = controller_data
4743
.set_response_body(serde_json::to_string(&data).unwrap_or_default())
4844
.await;
4945
}
5046

51-
#[inline]
5247
pub async fn fortunes(controller_data: ControllerData) {
5348
let all_rows: Vec<PgRow> = all_world_row().await;
5449
let mut fortunes_list: Vec<Fortunes> = all_rows
@@ -72,7 +67,6 @@ pub async fn fortunes(controller_data: ControllerData) {
7267
.await;
7368
}
7469

75-
#[inline]
7670
pub async fn updates(controller_data: ControllerData) {
7771
let queries: Queries = controller_data
7872
.get_request_query("q")
@@ -87,7 +81,6 @@ pub async fn updates(controller_data: ControllerData) {
8781
.await;
8882
}
8983

90-
#[inline]
9184
pub async fn cached_queries(controller_data: ControllerData) {
9285
let count: Queries = controller_data
9386
.get_request_query("c")
@@ -96,11 +89,13 @@ pub async fn cached_queries(controller_data: ControllerData) {
9689
.unwrap_or_default()
9790
.min(ROW_LIMIT as Queries)
9891
.max(1);
99-
let mut res: Vec<QueryRow> = Vec::with_capacity(count as usize);
100-
let cache: Vec<QueryRow> = CACHE.get().cloned().unwrap_or_default();
101-
for i in 0..count {
102-
res.push(cache[i as usize].clone());
103-
}
92+
let res: Vec<QueryRow> = CACHE
93+
.get()
94+
.unwrap_or(&Vec::new())
95+
.iter()
96+
.take(count as usize)
97+
.cloned()
98+
.collect();
10499
let _ = controller_data
105100
.set_response_body(serde_json::to_string(&res).unwrap_or_default())
106101
.await;

frameworks/Rust/hyperlane/src/server.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::*;
22

3-
#[inline]
43
pub async fn run_server() {
54
let server: Server = Server::new();
65
server.host("0.0.0.0").await;

frameworks/Rust/hyperlane/src/type.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub struct QueryRow {
1111
}
1212

1313
impl QueryRow {
14-
#[inline]
1514
pub fn new(id: Queries, random_number: Queries) -> Self {
1615
Self {
1716
id,
@@ -27,7 +26,6 @@ pub struct Fortunes {
2726
}
2827

2928
impl Fortunes {
30-
#[inline]
3129
pub fn new(id: Queries, message: String) -> Self {
3230
Self { id, message }
3331
}
@@ -37,14 +35,12 @@ impl Fortunes {
3735
pub struct FortunesTemplate(pub Vec<Fortunes>);
3836

3937
impl FortunesTemplate {
40-
#[inline]
4138
pub fn new(list: Vec<Fortunes>) -> Self {
4239
Self(list)
4340
}
4441
}
4542

4643
impl fmt::Display for FortunesTemplate {
47-
#[inline]
4844
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4945
let fortunes: &Vec<Fortunes> = &self.0;
5046
let _ = write!(

frameworks/Rust/hyperlane/src/utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::*;
22

3-
#[inline]
43
pub fn escape_html(input: &str) -> String {
54
let mut result: String = String::new();
65
for ch in input.chars() {
@@ -16,7 +15,6 @@ pub fn escape_html(input: &str) -> String {
1615
result
1716
}
1817

19-
#[inline]
2018
pub fn get_random_id() -> Queries {
2119
let mut rng: SmallRng = SmallRng::from_rng(&mut rng());
2220
let random_id: u32 = rng.random_range(1..RANDOM_MAX_ADD_ONE);

0 commit comments

Comments
 (0)