perf: Major event processing system refactor for improved performance

This commit is contained in:
ysq
2025-08-29 14:59:16 +08:00
parent 218abd3aa4
commit b535bdf052
23 changed files with 1557 additions and 894 deletions
+56 -54
View File
@@ -1,14 +1,14 @@
use super::constants::*;
/// 背压处理策略
/// Backpressure handling strategy
#[derive(Debug, Clone, Copy)]
pub enum BackpressureStrategy {
/// 阻塞等待(默认)
/// Block and wait (default)
Block,
/// 丢弃消息
/// Drop messages
Drop,
/// 重试有限次数后丢弃
Retry { max_attempts: usize, wait_ms: u64 },
/// Execute asynchronously (don't wait for completion)
Async,
}
impl Default for BackpressureStrategy {
@@ -17,50 +17,29 @@ impl Default for BackpressureStrategy {
}
}
/// 批处理配置
#[derive(Debug, Clone)]
pub struct BatchConfig {
/// 批处理大小(默认:100
pub batch_size: usize,
/// 批处理超时时间(毫秒,默认:5ms)
pub batch_timeout_ms: u64,
/// 是否启用批处理(默认:true)
pub enabled: bool,
}
impl Default for BatchConfig {
fn default() -> Self {
Self {
batch_size: DEFAULT_BATCH_SIZE,
batch_timeout_ms: DEFAULT_BATCH_TIMEOUT_MS,
enabled: true,
}
}
}
/// 背压配置
/// Backpressure configuration
#[derive(Debug, Clone)]
pub struct BackpressureConfig {
/// 通道大小(默认:1000
pub channel_size: usize,
/// 背压处理策略(默认:Block
/// Channel size (default: 1000)
pub permits: usize,
/// Backpressure handling strategy (default: Block)
pub strategy: BackpressureStrategy,
}
impl Default for BackpressureConfig {
fn default() -> Self {
Self { channel_size: DEFAULT_CHANNEL_SIZE, strategy: BackpressureStrategy::default() }
Self { permits: 1, strategy: BackpressureStrategy::default() }
}
}
/// 连接配置
/// Connection configuration
#[derive(Debug, Clone)]
pub struct ConnectionConfig {
/// 连接超时时间(秒,默认:10
/// Connection timeout in seconds (default: 10)
pub connect_timeout: u64,
/// 请求超时时间(秒,默认:60
/// Request timeout in seconds (default: 60)
pub request_timeout: u64,
/// 最大解码消息大小(字节,默认:10MB
/// Maximum decoding message size in bytes (default: 10MB)
pub max_decoding_message_size: usize,
}
@@ -74,16 +53,14 @@ impl Default for ConnectionConfig {
}
}
/// 通用客户端配置
/// Common client configuration
#[derive(Debug, Clone)]
pub struct StreamClientConfig {
/// 连接配置
/// Connection configuration
pub connection: ConnectionConfig,
/// 批处理配置
pub batch: BatchConfig,
/// 背压配置
/// Backpressure configuration
pub backpressure: BackpressureConfig,
/// 是否启用性能监控(默认:false
/// Whether performance monitoring is enabled (default: false)
pub enable_metrics: bool,
}
@@ -91,7 +68,6 @@ impl Default for StreamClientConfig {
fn default() -> Self {
Self {
connection: ConnectionConfig::default(),
batch: BatchConfig::default(),
backpressure: BackpressureConfig::default(),
enable_metrics: false,
}
@@ -99,31 +75,57 @@ impl Default for StreamClientConfig {
}
impl StreamClientConfig {
/// 创建高性能配置(适合高并发场景)
pub fn high_performance() -> Self {
/// Creates a high-throughput configuration optimized for high-concurrency scenarios.
///
/// This configuration prioritizes throughput over latency by:
/// - Implementing a drop strategy for backpressure to avoid blocking
/// - Setting a large permit buffer (5,000) to handle burst traffic
///
/// Ideal for scenarios where you need to process large volumes of data
/// and can tolerate occasional message drops during peak loads.
pub fn high_throughput() -> Self {
Self {
connection: ConnectionConfig::default(),
batch: BatchConfig { batch_size: 200, batch_timeout_ms: 5, enabled: true },
backpressure: BackpressureConfig {
channel_size: 20000,
permits: 5000,
strategy: BackpressureStrategy::Drop,
},
enable_metrics: false,
}
}
/// 创建低延迟配置(适合实时场景)
/// Creates a low-latency configuration optimized for real-time scenarios.
///
/// This configuration prioritizes latency over throughput by:
/// - Processing events immediately without buffering
/// - Implementing a blocking backpressure strategy to ensure no data loss
/// - Setting minimal permits (1) to minimize memory usage
///
/// Ideal for scenarios where every millisecond counts and you cannot
/// afford to lose any events, such as trading applications or real-time monitoring.
pub fn low_latency() -> Self {
Self {
connection: ConnectionConfig::default(),
batch: BatchConfig {
batch_size: 10,
batch_timeout_ms: 1,
enabled: false, // 禁用批处理,即时处理
},
backpressure: BackpressureConfig { permits: 1, strategy: BackpressureStrategy::Block },
enable_metrics: false,
}
}
/// Creates an asynchronous processing configuration optimized for high-volume scenarios.
///
/// This configuration balances throughput and reliability by:
/// - Implementing an async backpressure strategy for non-blocking operation
/// - Setting a balanced permit buffer (5,000) for steady flow
///
/// Ideal for scenarios where you need sustained high throughput with
/// fire-and-forget semantics, such as data ingestion pipelines or
/// event streaming applications where some eventual consistency is acceptable.
pub fn async_processing() -> Self {
Self {
connection: ConnectionConfig::default(),
backpressure: BackpressureConfig {
channel_size: 1000,
strategy: BackpressureStrategy::Block,
permits: 5000,
strategy: BackpressureStrategy::Async,
},
enable_metrics: false,
}