|
|
@@ -1,9 +1,24 @@
|
|
|
+use std::collections::HashMap;
|
|
|
+use std::mem::{size_of, size_of_val};
|
|
|
use std::sync::{Arc};
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
+use std::time::Duration;
|
|
|
+use lazy_static::lazy_static;
|
|
|
use tokio::sync::Mutex;
|
|
|
+use tokio::time::Instant;
|
|
|
use tracing::info;
|
|
|
use exchanges::gate_swap_ws::{GateSwapSubscribeType, GateSwapWs, GateSwapWsType};
|
|
|
use exchanges::response_base::ResponseData;
|
|
|
+use standard::{Depth, OrderBook};
|
|
|
+use standard::exchange::ExchangeEnum;
|
|
|
+use standard::exchange_struct_handler::ExchangeStructHandler;
|
|
|
+
|
|
|
+type DepthMap = HashMap<String, Vec<Depth>>;
|
|
|
+
|
|
|
+lazy_static! {
|
|
|
+ static ref DEPTH_MAP: Mutex<DepthMap> = Mutex::new(HashMap::new());
|
|
|
+ static ref LAST_PRINT_TIME: Mutex<Instant> = Mutex::new(Instant::now());
|
|
|
+}
|
|
|
|
|
|
pub async fn run_listener(is_shutdown_arc: Arc<AtomicBool>) {
|
|
|
let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
|
|
|
@@ -15,19 +30,83 @@ pub async fn run_listener(is_shutdown_arc: Arc<AtomicBool>) {
|
|
|
let mut ws = GateSwapWs::new_with_tag(name.to_string(), false, None, GateSwapWsType::PublicAndPrivate("usdt".to_string()));
|
|
|
ws.set_subscribe(vec![
|
|
|
GateSwapSubscribeType::PuFuturesTrades,
|
|
|
- GateSwapSubscribeType::PuFuturesBookTicker,
|
|
|
+ GateSwapSubscribeType::PuFuturesCandlesticks,
|
|
|
GateSwapSubscribeType::PuFuturesOrderBook
|
|
|
]);
|
|
|
|
|
|
- // 读取数据
|
|
|
- let fun = move |data: ResponseData| {
|
|
|
- async move {
|
|
|
- info!(?data);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
// 建立链接
|
|
|
ws.set_symbols(symbols);
|
|
|
- ws.ws_connect_async(is_shutdown_arc, fun, &write_tx_am, write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
+ ws.ws_connect_async(is_shutdown_arc, data_listener, &write_tx_am, write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
});
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+// 读取数据
|
|
|
+pub async fn data_listener(response: ResponseData) {
|
|
|
+ let mut depth_map = DEPTH_MAP.lock().await;
|
|
|
+
|
|
|
+ if response.code != 200 {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ match response.channel.as_str() {
|
|
|
+ // 深度数据
|
|
|
+ "futures.order_book" => {
|
|
|
+ let depth = ExchangeStructHandler::order_book_handle(ExchangeEnum::GateSwap, &response);
|
|
|
+
|
|
|
+ depth_map.entry(depth.symbol.clone())
|
|
|
+ .or_insert_with(Vec::new)
|
|
|
+ .push(depth);
|
|
|
+ },
|
|
|
+ // 订单流数据
|
|
|
+ "futures.trades" => {
|
|
|
+
|
|
|
+ },
|
|
|
+ // k线数据
|
|
|
+ "futures.candlesticks" => {
|
|
|
+
|
|
|
+ },
|
|
|
+ _ => {
|
|
|
+ info!("48 未知的数据类型: {:?}", response)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let mut last_print_time = LAST_PRINT_TIME.lock().await;
|
|
|
+ if last_print_time.elapsed() >= Duration::from_secs(60) {
|
|
|
+ estimate_depth_map_memory_usage(&depth_map);
|
|
|
+ *last_print_time = Instant::now();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub fn estimate_depth_map_memory_usage(depth_map: &DepthMap) {
|
|
|
+ let mut memory_usage_bytes = 0;
|
|
|
+
|
|
|
+ // 估算HashMap固定的开销大小,这个是一个近似值
|
|
|
+ memory_usage_bytes += size_of::<HashMap<String, Vec<Depth>>>();
|
|
|
+
|
|
|
+ for (symbol, depths) in depth_map {
|
|
|
+ // 估算每个symbol字符串的内存使用
|
|
|
+ memory_usage_bytes += size_of_val(symbol.as_str());
|
|
|
+
|
|
|
+ // 估算每个深度向量的内存使用
|
|
|
+ memory_usage_bytes += size_of_val(&depths);
|
|
|
+
|
|
|
+ for depth in depths {
|
|
|
+ // 估算每个Depth结构体的内存使用
|
|
|
+ memory_usage_bytes += size_of::<Depth>();
|
|
|
+
|
|
|
+ // 估算symbol字符串的内存使用
|
|
|
+ memory_usage_bytes += size_of_val(depth.symbol.as_str());
|
|
|
+
|
|
|
+ // 估算asks和bids向量的内存使用
|
|
|
+ memory_usage_bytes += size_of_val(&depth.asks);
|
|
|
+ memory_usage_bytes += size_of_val(&depth.bids);
|
|
|
+
|
|
|
+ // 估算OrderBook结构体在asks和bids向量中的内存使用
|
|
|
+ memory_usage_bytes += depth.asks.len() * size_of::<OrderBook>();
|
|
|
+ memory_usage_bytes += depth.bids.len() * size_of::<OrderBook>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let memory_usage_mb = memory_usage_bytes as f64 / (1024.0 * 1024.0);
|
|
|
+ info!("Estimated memory usage: {:.2} MB", memory_usage_mb);
|
|
|
+}
|