2025-08-11 01:04:16 +08:00
|
|
|
use super::constants::*;
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Backpressure handling strategy
|
2025-08-11 01:04:16 +08:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub enum BackpressureStrategy {
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Block and wait (default)
|
2025-08-11 01:04:16 +08:00
|
|
|
Block,
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Drop messages
|
2025-08-11 01:04:16 +08:00
|
|
|
Drop,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for BackpressureStrategy {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::Block
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Backpressure configuration
|
2025-08-11 01:04:16 +08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct BackpressureConfig {
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Channel size (default: 1000)
|
|
|
|
|
pub permits: usize,
|
|
|
|
|
/// Backpressure handling strategy (default: Block)
|
2025-08-11 01:04:16 +08:00
|
|
|
pub strategy: BackpressureStrategy,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for BackpressureConfig {
|
|
|
|
|
fn default() -> Self {
|
2025-09-02 17:27:27 +08:00
|
|
|
Self { permits: 3000, strategy: BackpressureStrategy::default() }
|
2025-08-11 01:04:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Connection configuration
|
2025-08-11 01:04:16 +08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct ConnectionConfig {
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Connection timeout in seconds (default: 10)
|
2025-08-11 01:04:16 +08:00
|
|
|
pub connect_timeout: u64,
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Request timeout in seconds (default: 60)
|
2025-08-11 01:04:16 +08:00
|
|
|
pub request_timeout: u64,
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Maximum decoding message size in bytes (default: 10MB)
|
2025-08-11 01:04:16 +08:00
|
|
|
pub max_decoding_message_size: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for ConnectionConfig {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
connect_timeout: DEFAULT_CONNECT_TIMEOUT,
|
|
|
|
|
request_timeout: DEFAULT_REQUEST_TIMEOUT,
|
|
|
|
|
max_decoding_message_size: DEFAULT_MAX_DECODING_MESSAGE_SIZE,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Common client configuration
|
2025-08-11 01:04:16 +08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct StreamClientConfig {
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Connection configuration
|
2025-08-11 01:04:16 +08:00
|
|
|
pub connection: ConnectionConfig,
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Backpressure configuration
|
2025-08-11 01:04:16 +08:00
|
|
|
pub backpressure: BackpressureConfig,
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Whether performance monitoring is enabled (default: false)
|
2025-08-11 01:04:16 +08:00
|
|
|
pub enable_metrics: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for StreamClientConfig {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
connection: ConnectionConfig::default(),
|
|
|
|
|
backpressure: BackpressureConfig::default(),
|
|
|
|
|
enable_metrics: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StreamClientConfig {
|
2025-08-29 14:59:16 +08:00
|
|
|
/// 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 {
|
2025-08-11 01:04:16 +08:00
|
|
|
Self {
|
|
|
|
|
connection: ConnectionConfig::default(),
|
|
|
|
|
backpressure: BackpressureConfig {
|
2025-08-31 22:18:18 +08:00
|
|
|
permits: 20000,
|
2025-08-11 01:04:16 +08:00
|
|
|
strategy: BackpressureStrategy::Drop,
|
|
|
|
|
},
|
2025-08-13 23:30:59 +08:00
|
|
|
enable_metrics: false,
|
2025-08-11 01:04:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
/// 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
|
2025-08-31 22:18:18 +08:00
|
|
|
/// - Setting optimal permits (4000) for balanced throughput and latency
|
2025-08-29 14:59:16 +08:00
|
|
|
///
|
|
|
|
|
/// Ideal for scenarios where every millisecond counts and you cannot
|
|
|
|
|
/// afford to lose any events, such as trading applications or real-time monitoring.
|
2025-08-11 01:04:16 +08:00
|
|
|
pub fn low_latency() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
connection: ConnectionConfig::default(),
|
2025-08-31 22:18:18 +08:00
|
|
|
backpressure: BackpressureConfig { permits: 4000, strategy: BackpressureStrategy::Block },
|
2025-08-29 14:59:16 +08:00
|
|
|
enable_metrics: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 01:04:16 +08:00
|
|
|
}
|