Package io.github.mahmoudimus.quic.token
Class BloomTokenLog
java.lang.Object
io.github.mahmoudimus.quic.token.BloomTokenLog
- All Implemented Interfaces:
TokenLog
A
TokenLog of bounded memory. Each period keeps a hash set of the tokens used, which
turns into a bloom filter once it outgrows its share of the memory budget, so memory grows
linearly and then stops. Time is divided into periods of one token lifetime, and two filters
cover the two periods in which unexpired tokens can expire; older filters are dropped, so the
false positive rate does not climb forever.
Tokens are identified by the first 8 bytes of their nonce, which the server chose at random and authenticated, so no secure hash is needed.
Threads. Not thread-safe and takes no locks. An endpoint shares one log among its connections, so the endpoint layer must serialize calls to it.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longThe default expected token uses per lifetime.static final intThe default memory budget, 10 MiB. -
Constructor Summary
ConstructorsConstructorDescriptionA log with the default budget and expected use.BloomTokenLog(int maxBytes, int kNum) A log with a memory budget and a number of hash functions. -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckAndInsert(byte[] nonce, Instant issued, Duration lifetime) Records that a token was used.static BloomTokenLogwithExpectedItems(int maxBytes, long expectedHits) A log with a memory budget, tuned for an expected number of uses per lifetime.
-
Field Details
-
DEFAULT_MAX_BYTES
public static final int DEFAULT_MAX_BYTESThe default memory budget, 10 MiB.- See Also:
-
DEFAULT_EXPECTED_HITS
public static final long DEFAULT_EXPECTED_HITSThe default expected token uses per lifetime.- See Also:
-
-
Constructor Details
-
BloomTokenLog
public BloomTokenLog()A log with the default budget and expected use. -
BloomTokenLog
public BloomTokenLog(int maxBytes, int kNum) A log with a memory budget and a number of hash functions.- Parameters:
maxBytes- the approximate memory budget, split between the two filterskNum- the number of bloom filter hash functions
-
-
Method Details
-
withExpectedItems
A log with a memory budget, tuned for an expected number of uses per lifetime.- Parameters:
maxBytes- the approximate memory budgetexpectedHits- the expected token uses per lifetime- Returns:
- the log
-
checkAndInsert
Description copied from interface:TokenLogRecords that a token was used.- Specified by:
checkAndInsertin interfaceTokenLog- Parameters:
nonce- the token's unique nonceissued- when the token was issuedlifetime- how long such tokens are valid- Returns:
- true if the token is accepted, false if it may have been used before
-