Skip to content

Commit 6b872aa

Browse files
feat(s2n-quic): Lazy-init duplicate filter (#2345)
This avoids a 1MB allocation per endpoint if we never receive a valid token (as is typical, since users need to explicitly opt-in to this with a limiter impl).
1 parent 17171ec commit 6b872aa

File tree

1 file changed

+8
-7
lines changed
  • quic/s2n-quic/src/provider/address_token

1 file changed

+8
-7
lines changed

quic/s2n-quic/src/provider/address_token/default.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ struct BaseKey {
2828
//= https://www.rfc-editor.org/rfc/rfc9000#section-8.1.4
2929
//# To protect against such attacks, servers MUST ensure that
3030
//# replay of tokens is prevented or limited.
31-
duplicate_filter: cuckoofilter::CuckooFilter<HashHasher>,
31+
duplicate_filter: Option<cuckoofilter::CuckooFilter<HashHasher>>,
3232
}
3333

3434
impl BaseKey {
3535
pub fn new(active_duration: Duration) -> Self {
3636
Self {
3737
active_duration,
3838
key: None,
39-
duplicate_filter: cuckoofilter::CuckooFilter::with_capacity(
40-
cuckoofilter::DEFAULT_CAPACITY,
41-
),
39+
duplicate_filter: None,
4240
}
4341
}
4442

@@ -70,8 +68,7 @@ impl BaseKey {
7068

7169
// TODO clear the filter instead of recreating. This is pending a merge to crates.io
7270
// (https://github.com/axiomhq/rust-cuckoofilter/pull/52)
73-
self.duplicate_filter =
74-
cuckoofilter::CuckooFilter::with_capacity(cuckoofilter::DEFAULT_CAPACITY);
71+
self.duplicate_filter = None;
7572

7673
self.key = Some((expires_at, key));
7774

@@ -201,7 +198,8 @@ impl Format {
201198
) -> Option<connection::InitialId> {
202199
if self.keys[token.header.key_id() as usize]
203200
.duplicate_filter
204-
.contains(token)
201+
.as_ref()
202+
.map_or(false, |f| f.contains(token))
205203
{
206204
return None;
207205
}
@@ -216,6 +214,9 @@ impl Format {
216214
// continue the connection if the filter fails.
217215
let _ = self.keys[token.header.key_id() as usize]
218216
.duplicate_filter
217+
.get_or_insert_with(|| {
218+
cuckoofilter::CuckooFilter::with_capacity(cuckoofilter::DEFAULT_CAPACITY)
219+
})
219220
.add(token);
220221

221222
return token.original_destination_connection_id();

0 commit comments

Comments
 (0)