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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
pub use s2n_quic_core::endpoint::{
limits::{ConnectionAttempt, Outcome},
Limiter,
};
use s2n_quic_core::{event::Timestamp, path::THROTTLED_PORTS_LEN};
pub trait Provider: 'static {
type Limits: 'static + Limiter;
type Error: core::fmt::Display;
fn start(self) -> Result<Self::Limits, Self::Error>;
}
use core::time::Duration;
pub use default::Limits as Default;
impl_provider_utils!();
impl<T: 'static + Limiter> Provider for T {
type Limits = T;
type Error = core::convert::Infallible;
fn start(self) -> Result<Self::Limits, Self::Error> {
Ok(self)
}
}
const THROTTLED_PORT_LIMIT: usize = 10;
const THROTTLE_FREQUENCY: Duration = Duration::from_secs(1);
#[derive(Default, Debug, Clone, Copy)]
struct BasicRateLimiter {
last_throttle_reset: Option<Timestamp>,
count: usize,
}
impl BasicRateLimiter {
fn should_throttle(
&mut self,
limit: usize,
throttle_frequency: Duration,
connection_attempt: &ConnectionAttempt,
) -> bool {
self.count += 1;
let timestamp = connection_attempt.timestamp;
if self.count > limit {
match self.last_throttle_reset {
Some(last_throttle_reset)
if timestamp.saturating_duration_since(last_throttle_reset)
< throttle_frequency =>
{
return true;
}
_ => {
self.count = 0;
self.last_throttle_reset = Some(timestamp);
return false;
}
};
}
if self.last_throttle_reset.is_none() {
self.last_throttle_reset = Some(timestamp);
}
false
}
}
#[cfg(test)]
mod tests {
use super::{BasicRateLimiter, THROTTLED_PORT_LIMIT, THROTTLE_FREQUENCY};
use core::time::Duration;
use s2n_quic_core::{
endpoint::limits::ConnectionAttempt,
event::IntoEvent,
inet::SocketAddress,
time::{testing::Clock as MockClock, Clock},
};
#[test]
fn first_throttle_reset() {
let remote_address = SocketAddress::default();
let mock_clock = MockClock::default();
let info =
ConnectionAttempt::new(0, 0, &remote_address, mock_clock.get_time().into_event());
let mut rate_limiter = BasicRateLimiter::default();
let very_long_freq = Duration::MAX;
for request in 0..(THROTTLED_PORT_LIMIT * 3) {
if request >= THROTTLED_PORT_LIMIT {
assert!(rate_limiter.should_throttle(THROTTLED_PORT_LIMIT, very_long_freq, &info));
} else {
assert!(!rate_limiter.should_throttle(THROTTLED_PORT_LIMIT, very_long_freq, &info));
}
}
}
#[test]
fn throttle_timer_reset() {
let remote_address = SocketAddress::default();
let mut mock_clock = MockClock::default();
let mut rate_limiter = BasicRateLimiter::default();
let short_freq = Duration::from_millis(10);
let sleep_longer_than_short_freq = Duration::from_millis(500);
for request in 0..(THROTTLED_PORT_LIMIT * 3) {
let info =
ConnectionAttempt::new(0, 0, &remote_address, mock_clock.get_time().into_event());
if request % THROTTLED_PORT_LIMIT == 0 {
mock_clock.inc_by(sleep_longer_than_short_freq)
}
assert!(!rate_limiter.should_throttle(THROTTLED_PORT_LIMIT, short_freq, &info));
}
}
#[test]
fn throttle_constants_changed() {
assert_eq!(THROTTLED_PORT_LIMIT, 10);
assert_eq!(THROTTLE_FREQUENCY, Duration::from_secs(1));
}
}
pub mod default {
use super::*;
use core::convert::Infallible;
#[derive(Default)]
pub struct Builder {
max_inflight_handshake_limit: Option<usize>,
}
impl Builder {
pub fn with_inflight_handshake_limit(mut self, limit: usize) -> Result<Self, Infallible> {
self.max_inflight_handshake_limit = Some(limit);
Ok(self)
}
pub fn build(self) -> Result<Limits, Infallible> {
Ok(Limits {
max_inflight_handshake_limit: self.max_inflight_handshake_limit,
rate_limiter: [BasicRateLimiter::default(); THROTTLED_PORTS_LEN],
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct Limits {
max_inflight_handshake_limit: Option<usize>,
rate_limiter: [BasicRateLimiter; THROTTLED_PORTS_LEN],
}
impl Limits {
pub fn builder() -> Builder {
Builder::default()
}
}
impl super::Limiter for Limits {
fn on_connection_attempt(&mut self, info: &ConnectionAttempt) -> Outcome {
let remote_port = info.remote_address.port();
if s2n_quic_core::path::remote_port_blocked(remote_port) {
return Outcome::drop();
}
if let Some(port_index) = s2n_quic_core::path::remote_port_throttled_index(remote_port)
{
let rate_limiter = &mut self.rate_limiter[port_index];
if rate_limiter.should_throttle(THROTTLED_PORT_LIMIT, THROTTLE_FREQUENCY, info) {
return Outcome::drop();
}
}
if let Some(limit) = self.max_inflight_handshake_limit {
if info.inflight_handshakes >= limit {
return Outcome::retry();
}
}
Outcome::allow()
}
}
impl std::default::Default for Limits {
fn default() -> Self {
Self {
max_inflight_handshake_limit: None,
rate_limiter: [BasicRateLimiter::default(); THROTTLED_PORTS_LEN],
}
}
}
#[test]
fn builder_test() {
let elp = Limits::builder()
.with_inflight_handshake_limit(100)
.unwrap()
.build()
.unwrap();
assert_eq!(elp.max_inflight_handshake_limit, Some(100));
}
#[test]
fn blocked_port_connection_attempt() {
use s2n_quic_core::{
event::IntoEvent,
inet::SocketAddress,
time::{testing::Clock as MockClock, Clock},
};
let mut remote_address = SocketAddress::default();
let mut limits = Limits::builder().build().unwrap();
let mock_clock = MockClock::default();
for port in 0..u16::MAX {
let blocked_expected = s2n_quic_core::path::remote_port_blocked(port);
remote_address.set_port(port);
let info =
ConnectionAttempt::new(0, 0, &remote_address, mock_clock.get_time().into_event());
let outcome = limits.on_connection_attempt(&info);
if blocked_expected {
assert_eq!(Outcome::drop(), outcome);
} else {
assert_eq!(Outcome::allow(), outcome);
}
}
}
}