Skip to content

Commit

Permalink
feat(s2n-quic): Lazy-init duplicate filter (#2345)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Mark-Simulacrum authored Oct 8, 2024
1 parent 17171ec commit 6b872aa
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions quic/s2n-quic/src/provider/address_token/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ struct BaseKey {
//= https://www.rfc-editor.org/rfc/rfc9000#section-8.1.4
//# To protect against such attacks, servers MUST ensure that
//# replay of tokens is prevented or limited.
duplicate_filter: cuckoofilter::CuckooFilter<HashHasher>,
duplicate_filter: Option<cuckoofilter::CuckooFilter<HashHasher>>,
}

impl BaseKey {
pub fn new(active_duration: Duration) -> Self {
Self {
active_duration,
key: None,
duplicate_filter: cuckoofilter::CuckooFilter::with_capacity(
cuckoofilter::DEFAULT_CAPACITY,
),
duplicate_filter: None,
}
}

Expand Down Expand Up @@ -70,8 +68,7 @@ impl BaseKey {

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

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

Expand Down Expand Up @@ -201,7 +198,8 @@ impl Format {
) -> Option<connection::InitialId> {
if self.keys[token.header.key_id() as usize]
.duplicate_filter
.contains(token)
.as_ref()
.map_or(false, |f| f.contains(token))
{
return None;
}
Expand All @@ -216,6 +214,9 @@ impl Format {
// continue the connection if the filter fails.
let _ = self.keys[token.header.key_id() as usize]
.duplicate_filter
.get_or_insert_with(|| {
cuckoofilter::CuckooFilter::with_capacity(cuckoofilter::DEFAULT_CAPACITY)
})
.add(token);

return token.original_destination_connection_id();
Expand Down

0 comments on commit 6b872aa

Please sign in to comment.