From 116edcbe07998dd54da7a89c57d87957418c37ee Mon Sep 17 00:00:00 2001 From: Sik Yoon Date: Sat, 2 Dec 2023 20:33:35 +0900 Subject: [PATCH] Move client variable into loop --- src/main.rs | 75 ++++++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/src/main.rs b/src/main.rs index 605f274..28f2de1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -396,12 +396,6 @@ async fn main() -> Result<(), Box> { // Task#1: check server time and health tokio::task::spawn(async move { - let client = ClientBuilder::new() - .timeout(Duration::from_millis(1000)) - .build() - .unwrap(); - let mut usertime = UserTime::new(); - let mut serverhealth = ServerHealth::new(); let mut prev_server_epoch: u128 = 0; let mut epoch_difference: f64 = 0.0; let mut epoch_mean: f64 = 0.0; @@ -409,6 +403,12 @@ async fn main() -> Result<(), Box> { loop { let instant = Instant::now(); + let client = ClientBuilder::new() + .timeout(Duration::from_millis(1000)) + .build() + .unwrap(); + let mut usertime = UserTime::new(); + let mut serverhealth = ServerHealth::new(); let mut epoch_difference_vec: Vec = Vec::new(); // let mut usertime_mutex = usertime_arc.lock().await; // server_health_check_team::execute_server_health_check(&mut usertime_mutex).await; @@ -493,13 +493,13 @@ async fn main() -> Result<(), Box> { // Task#2: request trade fee tokio::task::spawn(async move { - let client = ClientBuilder::new() - .timeout(tokio::time::Duration::from_millis(3000)) - .build() - .unwrap(); let mut elapsed_time = 0; loop { let instant = Instant::now(); + let client = ClientBuilder::new() + .timeout(tokio::time::Duration::from_millis(3000)) + .build() + .unwrap(); let tx1_changed = rx1_1.changed().await; let tx2_changed = rx2.changed().await; let local_epoch = *rx1_1.borrow(); @@ -554,11 +554,11 @@ async fn main() -> Result<(), Box> { // Task#3: request lot stepsize and ticksize tokio::task::spawn(async move { - let client = ClientBuilder::new() + loop { + let client = ClientBuilder::new() .timeout(tokio::time::Duration::from_millis(3000)) .build() .unwrap(); - loop { let mut exchange_info_data_temp: Vec = Vec::new(); let mut result; loop { @@ -595,13 +595,13 @@ async fn main() -> Result<(), Box> { // total_price_down_dist_index tokio::task::spawn(async move { sleep(Duration::from_secs(10)).await; - let client = ClientBuilder::new() - .timeout(tokio::time::Duration::from_millis(3000)) - .build() - .unwrap(); let mut elapsed_time = 0; loop { let instant = Instant::now(); + let client = ClientBuilder::new() + .timeout(tokio::time::Duration::from_millis(3000)) + .build() + .unwrap(); let result = request_others::request_24hr_ticker_price_change_statistics(&client).await; match result { Ok(T) => { @@ -668,15 +668,14 @@ async fn main() -> Result<(), Box> { // Task#5: price per second tokio::task::spawn(async move { sleep(Duration::from_secs(20)).await; - let client = ClientBuilder::new() - .timeout(tokio::time::Duration::from_millis(1000)) - .build() - .unwrap(); let mut server_epoch = 0; let mut elapsed_time = 0; loop { let instant = Instant::now(); - + let client = ClientBuilder::new() + .timeout(tokio::time::Duration::from_millis(1000)) + .build() + .unwrap(); let mut price_vec_temp: Vec = Vec::new(); let result = request_others::request_all_coin_price(&client, &mut price_vec_temp).await; let mut price_vec_temp_c: Vec = Vec::new(); @@ -1241,13 +1240,13 @@ async fn main() -> Result<(), Box> { } else if RUNNING_MODE == TEST { sleep(Duration::from_secs(10)).await; } - let client = ClientBuilder::new() - .timeout(tokio::time::Duration::from_millis(3000)) - .build() - .unwrap(); let mut elapsed_time = 0; loop { let instant = Instant::now(); + let client = ClientBuilder::new() + .timeout(tokio::time::Duration::from_millis(3000)) + .build() + .unwrap(); let exchange_info_vec = rx3_exchange_info_data.borrow().clone(); let trade_fee_vec = rx2_tradefee_vec.borrow().clone(); let result = coex::order_team::monitoring_open_buy_order( @@ -1324,13 +1323,13 @@ async fn main() -> Result<(), Box> { } else if RUNNING_MODE == TEST { sleep(Duration::from_secs(10)).await; } - let client = ClientBuilder::new() - .timeout(tokio::time::Duration::from_millis(3000)) - .build() - .unwrap(); let mut elapsed_time = 0; loop { let instant = Instant::now(); + let client = ClientBuilder::new() + .timeout(tokio::time::Duration::from_millis(3000)) + .build() + .unwrap(); let exchange_info_vec = rx4_exchange_info_data.borrow().clone(); let trade_fee_vec = rx4_tradefee_vec.borrow().clone(); let result = coex::order_team::monitoring_open_sell_order( @@ -1361,13 +1360,13 @@ async fn main() -> Result<(), Box> { // Task#23: monitoring_filled_sell_order tokio::task::spawn(async move { - let client = ClientBuilder::new() - .timeout(tokio::time::Duration::from_millis(3000)) - .build() - .unwrap(); let mut elapsed_time = 0; loop { let instant = Instant::now(); + let client = ClientBuilder::new() + .timeout(tokio::time::Duration::from_millis(3000)) + .build() + .unwrap(); let result = coex::order_team::monitoring_filled_sell_order(&client).await; // send Task#0 a message to notify running on @@ -1394,7 +1393,7 @@ async fn main() -> Result<(), Box> { loop { let instant = Instant::now(); let mut all_data = AllData::new(); - + // realtime price data all_data.rt_price_1m_vec = rx4_rt_price_1m_vec.borrow().clone(); @@ -1420,14 +1419,14 @@ async fn main() -> Result<(), Box> { // Task#25: update current_total_usdt and available_usdt tokio::task::spawn(async move { - let client = ClientBuilder::new() - .timeout(tokio::time::Duration::from_millis(3000)) - .build() - .unwrap(); let mut previous_result = false; let mut elapsed_time = 0; loop { let instant = Instant::now(); + let client = ClientBuilder::new() + .timeout(tokio::time::Duration::from_millis(3000)) + .build() + .unwrap(); let result = coex::assets_managing_team::monitoring_asset_usdt(&mut previous_result, &client) .await;