Class BloomTokenLog

java.lang.Object
io.github.mahmoudimus.quic.token.BloomTokenLog
All Implemented Interfaces:
TokenLog

public final class BloomTokenLog extends Object implements 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 Details

    • DEFAULT_MAX_BYTES

      public static final int DEFAULT_MAX_BYTES
      The default memory budget, 10 MiB.
      See Also:
    • DEFAULT_EXPECTED_HITS

      public static final long DEFAULT_EXPECTED_HITS
      The 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 filters
      kNum - the number of bloom filter hash functions
  • Method Details

    • withExpectedItems

      public static BloomTokenLog withExpectedItems(int maxBytes, long expectedHits)
      A log with a memory budget, tuned for an expected number of uses per lifetime.
      Parameters:
      maxBytes - the approximate memory budget
      expectedHits - the expected token uses per lifetime
      Returns:
      the log
    • checkAndInsert

      public boolean checkAndInsert(byte[] nonce, Instant issued, Duration lifetime)
      Description copied from interface: TokenLog
      Records that a token was used.
      Specified by:
      checkAndInsert in interface TokenLog
      Parameters:
      nonce - the token's unique nonce
      issued - when the token was issued
      lifetime - how long such tokens are valid
      Returns:
      true if the token is accepted, false if it may have been used before