java.lang.Object
net.i2p.router.networkdb.kademlia.LookupThrottler

class LookupThrottlerextends Object
Provides denial-of-service protection for network database lookup operations.

Tracks the frequency of recent lookup requests targeting specific reply peer/tunnel pairs to prevent abuse. Implements burst detection over sliding time windows and configurable request limits.

Note this is basic DOS protection and does not prevent spoofed reply identifiers or multiple reply tunnels from malicious requestors.

  • Field Details

    • counter

      Concurrent hash map.
    • burstTimestamps

      private final Map<Hash, ConcurrentHashMap<TunnelId, Deque<Long>>> burstTimestamps
      Map of peer hashes to sets of reply tunnel IDs for throttling.
    • DUMMY_ID

      private static final TunnelId DUMMY_ID
      The id of this is -1.
    • DEFAULT_MAX_LOOKUPS

      private static final int DEFAULT_MAX_LOOKUPS
      This seems like plenty.
      See Also:
    • DEFAULT_MAX_NON_FF_LOOKUPS

      private static final int DEFAULT_MAX_NON_FF_LOOKUPS
      Max lookups per peer when not floodfill.
      See Also:
    • DEFAULT_CLEAN_TIME

      private static final long DEFAULT_CLEAN_TIME
      Clean interval for expired throttle entries.
      See Also:
    • BURST_THRESHOLD

      private static final int BURST_THRESHOLD
      Max requests allowed per peer per burst window.
      See Also:
    • BURST_WINDOW_MS

      private static final long BURST_WINDOW_MS
      Length of the burst detection window in ms.
      See Also:
    • MAX_ENTRIES

      private static final int MAX_ENTRIES
      Hard cap on unique (from, tunnel) pairs tracked to prevent memory leaks. At ~25 peak lookups/sec with a 3-min clean window, ~4,500 keys is sufficient. 10,000 gives ~7 min headroom at peak. The clean runs every 3 min so this cap is only hit during sustained attack.
      See Also:
    • MAX_LOOKUPS

      private final int MAX_LOOKUPS
      Max lookups limit.
    • MAX_NON_FF_LOOKUPS

      private final int MAX_NON_FF_LOOKUPS
      Max non-FF lookups limit.
    • CLEAN_TIME

      private final long CLEAN_TIME
      Clean time.
    • _facade

      private final FloodfillNetworkDatabaseFacade _facade
      Floodfill network database facade for capability checks.
    • _max

      private volatile int _max
      Max value.
    • _cleaner

      private final LookupThrottler.Cleaner _cleaner
      Periodic cleanup task for stale throttle entries.
  • Constructor Details

    • LookupThrottler

      LookupThrottler(FloodfillNetworkDatabaseFacade facade)
      Throttle for the given floodfill facade.
      Parameters:
      facade - floodfill network database facade
    • LookupThrottler

      LookupThrottler(FloodfillNetworkDatabaseFacade facade, int maxlookups, int maxnonfflookups, long cleanTime)
      Throttle with configurable lookup limits.
      Parameters:
      facade - floodfill network database facade
      maxlookups - the concurrent lookup limit while floodfill
      maxnonfflookups - when not floodfill
      cleanTime - milliseconds between throttling table cleanups
  • Method Details

    • cancel

      void cancel()
      Stop the periodic cleaner. Call on facade shutdown.
    • shouldThrottle

      boolean shouldThrottle(Hash key, TunnelId id)
      Increment and check throttling for the given peer and tunnel.
      Parameters:
      key - non-null
      id - null if for direct lookups
      Returns:
      true if throttled