|
|
@@ -1,22 +1,21 @@
|
|
|
use std::future::Future;
|
|
|
-use std::time::Duration;
|
|
|
+use std::time::{Duration, Instant};
|
|
|
use exchanges::response_base::ResponseData;
|
|
|
use std::sync::Arc;
|
|
|
-use rust_decimal::Decimal;
|
|
|
use tokio::sync::Mutex;
|
|
|
use tracing::info;
|
|
|
use global::log_utils::init_log_with_info;
|
|
|
|
|
|
|
|
|
struct Core {
|
|
|
- pub count: u128
|
|
|
+ pub max_delay: u128
|
|
|
}
|
|
|
|
|
|
#[tokio::test]
|
|
|
async fn framework_3_0() {
|
|
|
init_log_with_info();
|
|
|
|
|
|
- let core = Arc::new(Mutex::new(Core { count: 0 }));
|
|
|
+ let core = Arc::new(Mutex::new(Core { max_delay: 0 }));
|
|
|
let handler_am = Arc::new(Mutex::new(TestHandler { core_am: core }));
|
|
|
|
|
|
// 一个闭包解决了引用问题。
|
|
|
@@ -34,27 +33,22 @@ async fn framework_3_0() {
|
|
|
// 消息创造者
|
|
|
async fn generator<F, Fut>(handle_function: F)
|
|
|
where
|
|
|
- F: Fn(ResponseData, Decimal, String) -> Fut,
|
|
|
+ F: Fn(ResponseData) -> Fut,
|
|
|
Fut: Future<Output = ()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
|
|
|
{
|
|
|
let data = ResponseData::new("aaa".to_string(),
|
|
|
"code".to_string(),
|
|
|
"msg".to_string(),
|
|
|
"data".to_string());
|
|
|
- let mut c = 0;
|
|
|
+ // let mut c = 0;
|
|
|
loop {
|
|
|
let mut s_data = data.clone();
|
|
|
s_data.time = chrono::Utc::now().timestamp_micros();
|
|
|
+ s_data.res_time = Instant::now();
|
|
|
|
|
|
handle_function(s_data).await;
|
|
|
|
|
|
- tokio::time::sleep(Duration::from_micros(100)).await;
|
|
|
-
|
|
|
- c = c+1;
|
|
|
-
|
|
|
- if c > 100 {
|
|
|
- break;
|
|
|
- }
|
|
|
+ tokio::time::sleep(Duration::from_micros(10)).await;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -66,11 +60,14 @@ impl TestHandler {
|
|
|
// 消息处理者
|
|
|
pub async fn on_data(&mut self, data: ResponseData) {
|
|
|
let mut core = self.core_am.lock().await;
|
|
|
- // 实现您的处理逝剧
|
|
|
- // 例如,增加计数:
|
|
|
- core.count += 1;
|
|
|
- let delay = chrono::Utc::now().timestamp_micros() - data.time;
|
|
|
+
|
|
|
+ let delay_nanos = data.res_time.elapsed().as_nanos();
|
|
|
+
|
|
|
+ if delay_nanos > core.max_delay {
|
|
|
+ core.max_delay = delay_nanos;
|
|
|
+ }
|
|
|
+
|
|
|
// 处理响应数据
|
|
|
- info!("Handled data: {}, count is: {}, delay is: {}.", data.data, core.count, delay);
|
|
|
+ info!("delay nanos: {}, max delay is: {}", delay_nanos, core.max_delay);
|
|
|
}
|
|
|
}
|