refactor: restructure event handling from trait objects to enum pattern

- Convert UnifiedEvent from trait object (Box<dyn UnifiedEvent>) to concrete enum type
  - Remove match_event! macro in favor of native match expressions
  - Simplify event metadata access: event.event_type() -> event.metadata().event_type
  - Unify event callback signatures: Fn(Box<dyn UnifiedEvent>) -> Fn(UnifiedEvent)
  - Update all example code to use the new enum-based event system
  - Optimize type system to reduce runtime overhead and improve type safety

  Affected scope:
  - Core event parser and processor
  - All protocol event definitions (PumpFun, PumpSwap, Bonk, Raydium series, etc.)
  - gRPC and Shred streaming modules
  - All example code
This commit is contained in:
ysq
2025-10-10 18:19:44 +08:00
parent 77caa39f0c
commit 312a675a40
43 changed files with 1013 additions and 1839 deletions
+4 -31
View File
@@ -1,5 +1,4 @@
use std::sync::Arc;
use std::sync::RwLock;
use tokio::sync::Mutex;
use tonic::transport::Channel;
@@ -14,8 +13,6 @@ use crate::streaming::common::{
pub struct ShredStreamGrpc {
pub shredstream_client: Arc<ShredstreamProxyClient<Channel>>,
pub config: StreamClientConfig,
pub metrics: Arc<RwLock<PerformanceMetrics>>,
pub metrics_manager: MetricsManager,
pub subscription_handle: Arc<Mutex<Option<SubscriptionHandle>>>,
}
@@ -28,38 +25,14 @@ impl ShredStreamGrpc {
/// 创建客户端,使用自定义配置
pub async fn new_with_config(endpoint: String, config: StreamClientConfig) -> AnyResult<Self> {
let shredstream_client = ShredstreamProxyClient::connect(endpoint.clone()).await?;
let metrics = Arc::new(RwLock::new(PerformanceMetrics::new()));
let metrics_manager = MetricsManager::new(config.enable_metrics, "ShredStream".to_string());
MetricsManager::init(config.enable_metrics);
Ok(Self {
shredstream_client: Arc::new(shredstream_client),
config,
metrics: metrics.clone(),
metrics_manager,
subscription_handle: Arc::new(Mutex::new(None)),
})
}
/// Creates a new ShredStreamClient with high-throughput configuration.
///
/// This is a convenience method that creates a client optimized for high-concurrency scenarios
/// where throughput is prioritized over latency. See `StreamClientConfig::high_throughput()`
/// for detailed configuration information.
pub async fn new_high_throughput(endpoint: String) -> AnyResult<Self> {
Self::new_with_config(endpoint, StreamClientConfig::high_throughput()).await
}
/// Creates a new ShredStreamClient with low-latency configuration.
///
/// This is a convenience method that creates a client optimized for real-time scenarios
/// where latency is prioritized over throughput. See `StreamClientConfig::low_latency()`
/// for detailed configuration information.
pub async fn new_low_latency(endpoint: String) -> AnyResult<Self> {
Self::new_with_config(endpoint, StreamClientConfig::low_latency()).await
}
/// 获取当前配置
pub fn get_config(&self) -> &StreamClientConfig {
&self.config
@@ -72,7 +45,7 @@ impl ShredStreamGrpc {
/// 获取性能指标
pub fn get_metrics(&self) -> PerformanceMetrics {
self.metrics_manager.get_metrics()
MetricsManager::global().get_metrics()
}
/// 启用或禁用性能监控
@@ -82,12 +55,12 @@ impl ShredStreamGrpc {
/// 打印性能指标
pub fn print_metrics(&self) {
self.metrics_manager.print_metrics();
MetricsManager::global().print_metrics();
}
/// 启动自动性能监控任务
pub async fn start_auto_metrics_monitoring(&self) {
self.metrics_manager.start_auto_monitoring().await;
MetricsManager::global().start_auto_monitoring().await;
}
/// 停止当前订阅
+1 -2
View File
@@ -10,6 +10,5 @@ pub use types::*;
// 从公用模块重新导出
pub use crate::streaming::common::{
BackpressureConfig, BackpressureStrategy, ConnectionConfig, MetricsEventType, MetricsManager,
PerformanceMetrics, StreamClientConfig,
ConnectionConfig, MetricsEventType, MetricsManager, PerformanceMetrics, StreamClientConfig,
};