@@ -28,17 +28,15 @@ struct BaseKey {
28
28
//= https://www.rfc-editor.org/rfc/rfc9000#section-8.1.4
29
29
//# To protect against such attacks, servers MUST ensure that
30
30
//# replay of tokens is prevented or limited.
31
- duplicate_filter : cuckoofilter:: CuckooFilter < HashHasher > ,
31
+ duplicate_filter : Option < cuckoofilter:: CuckooFilter < HashHasher > > ,
32
32
}
33
33
34
34
impl BaseKey {
35
35
pub fn new ( active_duration : Duration ) -> Self {
36
36
Self {
37
37
active_duration,
38
38
key : None ,
39
- duplicate_filter : cuckoofilter:: CuckooFilter :: with_capacity (
40
- cuckoofilter:: DEFAULT_CAPACITY ,
41
- ) ,
39
+ duplicate_filter : None ,
42
40
}
43
41
}
44
42
@@ -70,8 +68,7 @@ impl BaseKey {
70
68
71
69
// TODO clear the filter instead of recreating. This is pending a merge to crates.io
72
70
// (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 ;
75
72
76
73
self . key = Some ( ( expires_at, key) ) ;
77
74
@@ -201,7 +198,8 @@ impl Format {
201
198
) -> Option < connection:: InitialId > {
202
199
if self . keys [ token. header . key_id ( ) as usize ]
203
200
. duplicate_filter
204
- . contains ( token)
201
+ . as_ref ( )
202
+ . map_or ( false , |f| f. contains ( token) )
205
203
{
206
204
return None ;
207
205
}
@@ -216,6 +214,9 @@ impl Format {
216
214
// continue the connection if the filter fails.
217
215
let _ = self . keys [ token. header . key_id ( ) as usize ]
218
216
. duplicate_filter
217
+ . get_or_insert_with ( || {
218
+ cuckoofilter:: CuckooFilter :: with_capacity ( cuckoofilter:: DEFAULT_CAPACITY )
219
+ } )
219
220
. add ( token) ;
220
221
221
222
return token. original_destination_connection_id ( ) ;
0 commit comments