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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Limit the max number of requests being concurrently processed.
// <-- Add this line
use fmt;
pub use AdaptiveConcurrencyLimitLayer;
pub use AdaptiveConcurrencyLimit;
use Builder;
/// Configuration of adaptive concurrency parameters.
///
/// These parameters typically do not require changes from the default, and incorrect values can lead to meta-stable or
/// unstable performance and sink behavior. Proceed with caution.
// The defaults for these values were chosen after running several simulations on a test service that had
// various responses to load. The values are the best balances found between competing outcomes.
// #[serde(deny_unknown_fields)]
/// Configuration settings for the AIMD (Additive Increase/Multiplicative Decrease)
/// adaptive concurrency control algorithm.
///
/// This struct provides various configuration options to tune the behavior of the
/// adaptive concurrency limiter. The algorithm adjusts the number of concurrent requests
/// based on response latencies and errors, using an AIMD approach similar to TCP congestion control.
///
/// # Configuration Parameters
///
/// Since all fields are private, configuration must be done through the builder pattern.
/// The following table summarizes available parameters:
///
/// | Parameter | Default | Description |
/// |-----------|---------|-------------|
/// | `initial_concurrency` | 1 | Starting number of concurrent requests<br>**Recommendations**: Set to service's average concurrency limit<br>Higher = faster ramp-up but riskier<br>Lower = safer cold-start but underutilized |
/// | `decrease_ratio` | 0.9 | Multiplicative decrease factor on congestion<br>**Range**: 0-1<br>**Trade-offs**:<br>- Higher (0.95) = gentler backoffs<br>- Lower (0.7) = faster congestion recovery |
/// | `ewma_alpha` | 0.4 | Smoothing factor for latency measurements<br>**Formula**: `new_avg = alpha*current + (1-alpha)*prev`<br>**Range**: 0-1<br>**Recommendations**:<br>- Increase for bursty workloads<br>- Decrease for stable services |
/// | `rtt_deviation_scale` | 2.5 | Multiplier for abnormal latency threshold<br>**Formula**: `threshold = avg + scale*deviation`<br>**Range**: ≥0 (1.0-3.0 typical)<br>**Tuning**:<br>- Higher = fewer false positives<br>- Lower = more sensitive to fluctuations |
/// | `max_concurrency_limit` | 200 | Upper bound for concurrency<br>**Considerations**:<br>- Set to service's max safe capacity<br>- Too low = caps performance<br>- Too high = risk of cascading failures |
///
/// # Example
///
/// ```rust
/// use rate_limiter_aimd::adaptive_concurrency::AdaptiveConcurrencySettings;
///
/// // Create settings with custom values
/// let settings = AdaptiveConcurrencySettings::builder()
/// .initial_concurrency(10)
/// .decrease_ratio(0.8)
/// .max_concurrency_limit(500)
/// .build();
/// ```
/// Returns the default initial concurrency value (1).
///
/// This is used when no custom value is specified in the settings builder.
const
/// Returns the default decrease ratio (0.9).
///
/// This controls how aggressively the concurrency limit is reduced when congestion is detected.
/// A value of 0.9 means the limit will be reduced to 90% of its current value (a 10% decrease).
const
/// Returns the default EWMA (Exponentially Weighted Moving Average) alpha value (0.4).
///
/// This controls the smoothing factor for latency measurements. A higher value gives more weight
/// to recent measurements, while a lower value gives more weight to historical data.
///
/// The value should be between 0 and 1. The default of 0.4 provides a balance between
/// responsiveness to recent changes and stability from historical data.
const
/// Returns the default RTT (Round Trip Time) deviation scale factor (2.5).
///
/// This is used to determine when latency variance is high enough to indicate congestion.
/// A higher value makes the algorithm less sensitive to latency variations, while a lower
/// value makes it more sensitive.
const
/// Returns the default maximum concurrency limit (200).
///
/// This sets an upper bound on the concurrency level that the algorithm can reach.
/// It prevents unbounded growth in cases where the system appears to handle higher loads.
const
;