kiteticker_async_manager/manager/
config.rs

1use crate::models::Mode;
2use std::time::Duration;
3
4/// Configuration for the KiteTicker multi-connection manager
5#[derive(Debug, Clone)]
6pub struct KiteManagerConfig {
7  /// Maximum symbols per WebSocket connection (Kite limit: 3000)
8  pub max_symbols_per_connection: usize,
9
10  /// Number of WebSocket connections to maintain (Kite limit: 3)
11  pub max_connections: usize,
12
13  /// Buffer size for each connection's message channel
14  pub connection_buffer_size: usize,
15
16  /// Buffer size for each parser's output channel
17  pub parser_buffer_size: usize,
18
19  /// Connection timeout for establishing WebSocket connections
20  pub connection_timeout: Duration,
21
22  /// Health check interval for monitoring connections
23  pub health_check_interval: Duration,
24
25  /// Maximum reconnection attempts per connection
26  pub max_reconnect_attempts: usize,
27
28  /// Delay between reconnection attempts
29  pub reconnect_delay: Duration,
30
31  /// Enable dedicated parser tasks for each connection
32  pub enable_dedicated_parsers: bool,
33
34  /// Default subscription mode for new symbols
35  pub default_mode: Mode,
36
37  /// Consider the websocket alive if a frame (including heartbeat) arrived within this duration
38  pub heartbeat_liveness_threshold: Duration,
39}
40
41impl Default for KiteManagerConfig {
42  fn default() -> Self {
43    Self {
44      max_symbols_per_connection: 3000,
45      max_connections: 3,
46      connection_buffer_size: 5000, // High buffer for performance
47      parser_buffer_size: 10000,    // Even higher for parsed messages
48      connection_timeout: Duration::from_secs(30),
49      health_check_interval: Duration::from_secs(10),
50      max_reconnect_attempts: 5,
51      reconnect_delay: Duration::from_secs(2),
52      enable_dedicated_parsers: true,
53      default_mode: Mode::Quote,
54      heartbeat_liveness_threshold: Duration::from_secs(10),
55    }
56  }
57}
58
59/// Connection statistics for monitoring
60#[derive(Debug, Clone, Default)]
61pub struct ConnectionStats {
62  pub connection_id: usize,
63  pub is_connected: bool,
64  pub symbol_count: usize,
65  pub messages_received: u64,
66  pub messages_parsed: u64,
67  pub errors_count: u64,
68  pub last_message_time: Option<std::time::Instant>,
69  pub average_latency: Duration,
70  pub connection_uptime: Duration,
71}
72
73/// Manager-wide statistics
74#[derive(Debug, Clone, Default)]
75pub struct ManagerStats {
76  pub total_symbols: usize,
77  pub active_connections: usize,
78  pub total_messages_received: u64,
79  pub total_messages_parsed: u64,
80  pub total_errors: u64,
81  pub uptime: Duration,
82  pub connection_stats: Vec<ConnectionStats>,
83}
84
85/// Channel identifier for output channels
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87pub enum ChannelId {
88  Connection1 = 0,
89  Connection2 = 1,
90  Connection3 = 2,
91}
92
93impl ChannelId {
94  pub fn from_index(index: usize) -> Option<Self> {
95    match index {
96      0 => Some(Self::Connection1),
97      1 => Some(Self::Connection2),
98      2 => Some(Self::Connection3),
99      _ => None,
100    }
101  }
102
103  pub fn to_index(self) -> usize {
104    self as usize
105  }
106
107  pub fn all() -> Vec<Self> {
108    vec![Self::Connection1, Self::Connection2, Self::Connection3]
109  }
110}
111
112// ============================================================================
113// Multi-API Configuration Types
114// ============================================================================
115
116/// Unique identifier for an API key
117#[derive(Debug, Clone, PartialEq, Eq, Hash)]
118pub struct ApiKeyId(pub String);
119
120impl ApiKeyId {
121  pub fn new(id: impl Into<String>) -> Self {
122    Self(id.into())
123  }
124}
125
126impl From<&str> for ApiKeyId {
127  fn from(s: &str) -> Self {
128    Self(s.to_string())
129  }
130}
131
132impl From<String> for ApiKeyId {
133  fn from(s: String) -> Self {
134    Self(s)
135  }
136}
137
138/// API credentials for a single Kite Connect account
139#[derive(Debug, Clone)]
140pub struct ApiCredentials {
141  pub api_key: String,
142  pub access_token: String,
143}
144
145impl ApiCredentials {
146  pub fn new(api_key: impl Into<String>, access_token: impl Into<String>) -> Self {
147    Self {
148      api_key: api_key.into(),
149      access_token: access_token.into(),
150    }
151  }
152}
153
154/// Strategy for distributing symbols across multiple API keys
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum DistributionStrategy {
157  /// Automatically distribute symbols across all API keys using round-robin
158  RoundRobin,
159  /// Manually assign symbols to specific API keys
160  Manual,
161}
162
163impl Default for DistributionStrategy {
164  fn default() -> Self {
165    Self::RoundRobin
166  }
167}
168
169/// Configuration for multi-API manager
170#[derive(Debug, Clone)]
171pub struct MultiApiConfig {
172  /// Base configuration for each API key's connections
173  pub base_config: KiteManagerConfig,
174  
175  /// Maximum connections per API key (Kite limit: 3)
176  pub max_connections_per_api: usize,
177  
178  /// Symbol distribution strategy
179  pub distribution_strategy: DistributionStrategy,
180  
181  /// Enable health monitoring across all API keys
182  pub enable_health_monitoring: bool,
183}
184
185impl Default for MultiApiConfig {
186  fn default() -> Self {
187    Self {
188      base_config: KiteManagerConfig::default(),
189      max_connections_per_api: 3,
190      distribution_strategy: DistributionStrategy::RoundRobin,
191      enable_health_monitoring: true,
192    }
193  }
194}
195
196/// Statistics for a single API key
197#[derive(Debug, Clone, Default)]
198pub struct ApiKeyStats {
199  pub api_key_id: String,
200  pub active_connections: usize,
201  pub total_symbols: usize,
202  pub total_messages_received: u64,
203  pub total_messages_parsed: u64,
204  pub total_errors: u64,
205  pub connection_stats: Vec<ConnectionStats>,
206}
207
208/// Aggregate statistics across all API keys
209#[derive(Debug, Clone, Default)]
210pub struct MultiApiStats {
211  pub total_api_keys: usize,
212  pub total_connections: usize,
213  pub total_symbols: usize,
214  pub total_messages_received: u64,
215  pub total_messages_parsed: u64,
216  pub total_errors: u64,
217  pub uptime: Duration,
218  pub per_api_stats: Vec<ApiKeyStats>,
219}