1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! Handshake Connection Management System
//!
//! Provides controlled execution of MQTT handshake operations with:
//! - Port-specific worker pools
//! - Dynamic busy state detection
//! - Connection rate monitoring
//!
//! ## Core Functionality
//! 1. **Executor Management**:
//! - Creates dedicated TaskExecQueue per listener port
//! - Auto-scales worker pools based on config limits
//! - Maintains execution statistics
//!
//! 2. **Busy State Detection**:
//! - Dynamic threshold calculation (35% of max capacity)
//! - Cross-port busy state aggregation
//! - Integration with shared server state
//!
//! 3. **Performance Monitoring**:
//! - Per-port execution rate tracking
//! - Active task count summation
//! - Async-compatible metrics collection
//!
//! ## Implementation Details
//! - Uses DashMap for concurrent port mapping
//! - Lazy initialization of executor pools
//! - Tokio-based async task spawning
//! - Zero-cost deref to TaskExecQueue
//!
//! Operational Flow:
//! 1. Get executor for specific port (auto-creates if needed)
//! 2. Execute handshake tasks in isolated pool
//! 3. Monitor aggregate system state
//! 4. Trigger busy state when thresholds exceeded
use ;
use Deref;
use crateServerContext;
use crate;
type BusyLimit = isize;
// #[inline]
// pub(crate) fn unavailable_stats() -> &'static Counter {
// static UNAVAILABLE_STATS: OnceCell<Counter> = OnceCell::new();
// UNAVAILABLE_STATS.get_or_init(|| {
// let period = Duration::from_secs(5);
// let c = Counter::new(period);
// c.close_auto_update();
// let c1 = c.clone();
// tokio::spawn(async move {
// let mut delay = None;
// loop {
// tokio::time::sleep(period).await;
// c1.rate_update();
// let fail_rate = c1.rate();
// let is_too_many_unavailable = is_too_many_unavailable().await;
// if fail_rate > 0.0 && !is_too_many_unavailable {
// log::warn!(
// "Connection handshake with too many unavailable, switching to TooManyUnavailable::Yes, fail rate: {}",
// fail_rate
// );
// too_many_unavailable_set(TooManyUnavailable::Yes).await;
// delay = Some(Instant::now());
// } else if (delay.map(|d| d.elapsed().as_secs() > 120).unwrap_or(false))
// || (is_too_many_unavailable && !is_busy().await && handshakings() < 10)
// {
// log::info!(
// "Connection handshake restored to TooManyUnavailable::No, delay: {:?}",
// delay.map(|d| d.elapsed())
// );
// too_many_unavailable_set(TooManyUnavailable::No).await;
// delay = None;
// }
// }
// });
// c
// })
// }
//
// enum TooManyUnavailable {
// Yes,
// No,
// }
//
// #[inline]
// fn too_many_unavailable() -> &'static RwLock<TooManyUnavailable> {
// static TOO_MANY_UNAVAILABLE: OnceCell<RwLock<TooManyUnavailable>> = OnceCell::new();
// TOO_MANY_UNAVAILABLE.get_or_init(|| RwLock::new(TooManyUnavailable::No))
// }
//
// #[inline]
// pub(crate) async fn is_too_many_unavailable() -> bool {
// matches!(*too_many_unavailable().read().await, TooManyUnavailable::Yes)
// }
//
// #[inline]
// async fn too_many_unavailable_set(v: TooManyUnavailable) {
// *too_many_unavailable().write().await = v;
// }