| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- use std::future::Future;
- use std::time::Duration;
- use exchanges::response_base::ResponseData;
- use std::sync::Arc;
- use tokio::sync::Mutex;
- use tracing::info;
- use global::log_utils::init_log_with_info;
- struct Node {
- pub count: u128
- }
- #[tokio::test]
- async fn main() {
- init_log_with_info();
- let node = Arc::new(Mutex::new(Node { count: 0 }));
- generator(handle, node).await;
- }
- // 消息创造者
- async fn generator<F, Fut>(handle_function: F, node: Arc<Mutex<Node>>)
- where
- F: Fn(Arc<Mutex<Node>>, ResponseData) -> Fut,
- Fut: Future<Output = ()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
- {
- let mut data = ResponseData::new("aaa".to_string(),
- "code".to_string(),
- "msg".to_string(),
- "data".to_string());
- loop {
- let mut s_data = data.clone();
- s_data.time = chrono::Utc::now().timestamp_micros();
- handle_function(node.clone(), s_data).await;
- tokio::time::sleep(Duration::from_micros(100)).await;
- }
- }
- // 消息处理者
- async fn handle(node: Arc<Mutex<Node>>, data: ResponseData) {
- let mut node = node.lock().await;
- // 实现您的处理逝剧
- // 例如,增加计数:
- node.count += 1;
- let delay = chrono::Utc::now().timestamp_micros() - data.time;
- // 处理响应数据
- info!("Handled data: {}, count is: {}, delay is: {}.", data.data, node.count, delay);
- }
|