java.lang.Object
net.i2p.router.tunnel.pool.RequestThrottler

public class RequestThrottlerextends Object
Throttles incoming tunnel requests earlier than ParticipatingThrottler, with higher limits and more frequent cleanup.

Checks various router characteristics and decides whether to throttle, ban, or disconnect routers based on request count, version, bandwidth, and system load.

  • Field Details

    • context

      private final RouterContext context
      The router context.
    • counter

      private final ObjectCounter<Hash> counter
      Build requests counted per peer.
    • _log

      private final Log _log
      Logger for throttling decisions.
    • _banLogger

      private BanLogger _banLogger
      Records why routers were banned.
    • lastFirewallCheckTime

      private volatile long lastFirewallCheckTime
      When our firewall status was last checked.
    • cachedFirewalledStatus

      private volatile boolean cachedFirewalledStatus
      Cached result of the firewall status check.
    • FIREWALL_CHECK_INTERVAL

      private static final long FIREWALL_CHECK_INTERVAL
      Interval between firewall status checks.
      See Also:
    • lastPropertyCheckTime

      private volatile long lastPropertyCheckTime
      When the config values were last re-read.
    • cachedShouldThrottle

      private volatile Boolean cachedShouldThrottle
      Cached flag for whether to throttle at all.
    • cachedShouldDisconnect

      private volatile Boolean cachedShouldDisconnect
      Cached flag for disconnecting excessive requesters.
    • cachedShouldBlockOldRouters

      private volatile Boolean cachedShouldBlockOldRouters
      Cached flag for rejecting old routers.
    • cachedShouldBanExcessive

      private volatile boolean cachedShouldBanExcessive
      Cached flag for banning excessive requesters.
    • PROPERTY_CHECK_INTERVAL

      private static final long PROPERTY_CHECK_INTERVAL
      Interval between config re-reads.
      See Also:
    • _reqMinLimit

      public static volatile int _reqMinLimit
      Minimum request limit per peer.
    • _reqMaxLimit

      public static volatile int _reqMaxLimit
      Maximum request limit per peer.
    • _reqPercentLimit

      public static volatile int _reqPercentLimit
      Request limit as percentage of participating tunnel count.
    • _reqBurst1sThreshold

      public static volatile int _reqBurst1sThreshold
      Burst threshold for 1-second sliding window (requests per second).
    • _reqRejectThreshold

      public static volatile int _reqRejectThreshold
      Rejection threshold: start rejecting at count/limit ratio this high (30-100, default 70%).
    • _reqRejectSteepness

      public static volatile int _reqRejectSteepness
      Rejection steepness: 100=linear ramp, 200=quadratic.
    • _reqLoadWeight

      public static volatile int _reqLoadWeight
      Load weight: how much load inflates effective ratio (0-300%, default 100%).
    • _highLoadLagMs

      public static volatile int _highLoadLagMs
      High-load job lag threshold (ms)
    • _highLoadCpuPct

      public static volatile int _highLoadCpuPct
      High-load CPU threshold (percent)
    • _highLoadSysLoadPct

      public static volatile int _highLoadSysLoadPct
      High-load system load threshold (percent, normalized by cores)
    • _moderateLoadLagMs

      public static volatile int _moderateLoadLagMs
      Moderate-load job lag threshold (ms)
    • _moderateLoadCpuPct

      public static volatile int _moderateLoadCpuPct
      Moderate-load CPU threshold (percent)
    • _moderateLoadSysLoadPct

      public static volatile int _moderateLoadSysLoadPct
      Moderate-load system load threshold (percent, normalized by cores)
    • _sustainedModerateLoadMs

      public static volatile long _sustainedModerateLoadMs
      Duration (ms) moderate load must persist before disconnecting low-share peers
    • CLEAN_TIME

      private static final long CLEAN_TIME
      Interval between request count resets.
      See Also:
    • LU_BAN_MS

      private static final long LU_BAN_MS
      Ban length for LU prev-hops under load.
      See Also:
    • BURST_BAN_OFFENSES

      private static final int BURST_BAN_OFFENSES
      Burst offenses before a ban issues; earlier offenses only throttle the request.
      See Also:
    • BURST_BAN_BASE_MS

      private static final long BURST_BAN_BASE_MS
      Ban length for the first banning burst offense, scaling with further offenses.
      See Also:
    • BURST_BAN_MAX_MS

      private static final long BURST_BAN_MAX_MS
      Ceiling for burst bans.
      See Also:
    • RATES

      private static final long[] RATES
      Rate stat periods for the throttle statistics.
    • BURST_WINDOW_MS

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

      private static final int BURST_BUCKET_COUNT
      Buckets in the burst window, one second each.
      See Also:
    • BURST_THRESHOLD_MIN

      private static final int BURST_THRESHOLD_MIN
      Lowest burst threshold allowed.
      See Also:
    • BURST_THRESHOLD_MAX

      private static final int BURST_THRESHOLD_MAX
      Highest burst threshold allowed.
      See Also:
    • BURST_THRESHOLD_PERCENT

      private static final double BURST_THRESHOLD_PERCENT
      Share of the 90 second limit used as the burst threshold.
      See Also:
    • BURST_OFFENSE_RESET

      private static final long BURST_OFFENSE_RESET
      Idle time before burst offenses are forgotten.
      See Also:
    • _burstCounter

      private final RequestThrottler.BurstWindowCounter _burstCounter
      Sliding window counter for burst detection.
    • _burstOffenses

      private final Map<Hash, RequestThrottler.BurstOffenseRecord> _burstOffenses
      Burst offenses recorded per peer.
    • _moderateLoadStart

      private volatile long _moderateLoadStart
      When the request load first reached the moderate level.
    • DEFAULT_SHOULD_THROTTLE

      static final boolean DEFAULT_SHOULD_THROTTLE
      Whether count-based transit throttling applies when the property is unset.
      See Also:
    • PROP_SHOULD_THROTTLE

      static final String PROP_SHOULD_THROTTLE
      Enables counting transit requests and rejecting or dropping over-limit peers.
      See Also:
    • DEFAULT_SHOULD_DISCONNECT

      static final boolean DEFAULT_SHOULD_DISCONNECT
      Whether a versionless peer is disconnected when the property is unset.
      See Also:
    • PROP_SHOULD_DISCONNECT

      static final String PROP_SHOULD_DISCONNECT
      Enables scheduling a disconnect for a peer that joins with no version.
      See Also:
    • DEFAULT_BLOCK_OLD_ROUTERS

      private static final boolean DEFAULT_BLOCK_OLD_ROUTERS
      Rejecting old routers is on by default.
      See Also:
    • PROP_BLOCK_OLD_ROUTERS

      private static final String PROP_BLOCK_OLD_ROUTERS
      Config key for rejecting routers with old profiles.
      See Also:
    • DEFAULT_BAN_EXCESSIVE_REQUESTS

      private static final boolean DEFAULT_BAN_EXCESSIVE_REQUESTS
      Banning excessive requesters is off by default.
      See Also:
    • PROP_BAN_EXCESSIVE_REQUESTS

      private static final String PROP_BAN_EXCESSIVE_REQUESTS
      Config key turning the excessive request ban on or off.
      See Also:
  • Constructor Details

    • RequestThrottler

      RequestThrottler(RouterContext ctx)
      Creates a new RequestThrottler bound to the given router context. Initializes the request counter, burst detector, ban logger, and configures tunable parameters from router properties.
      Parameters:
      ctx - the router context
  • Method Details

    • getRequestMinLimit

      public static int getRequestMinLimit()
      The minimum request limit per peer.
      Returns:
      the minimum limit
    • setRequestMinLimit

      public static void setRequestMinLimit(int val)
      The minimum request limit per peer.
      Parameters:
      val - the minimum limit (clamped to 1-100)
    • getRequestMaxLimit

      public static int getRequestMaxLimit()
      The maximum request limit per peer.
      Returns:
      the maximum limit
    • setRequestMaxLimit

      public static void setRequestMaxLimit(int val)
      The maximum request limit per peer.
      Parameters:
      val - the maximum limit (clamped to 10-1000)
    • getRequestPctLimit

      public static int getRequestPctLimit()
      The request limit as percentage of participating tunnel count.
      Returns:
      the percentage limit
    • setRequestPctLimit

      public static void setRequestPctLimit(int val)
      The request limit as percentage of participating tunnel count.
      Parameters:
      val - the percentage limit (clamped to 1-100)
    • getRequestBurst1sThreshold

      public static int getRequestBurst1sThreshold()
      The burst threshold for 1-second sliding window.
      Returns:
      the burst threshold (requests per second)
    • setRequestBurst1sThreshold

      public static void setRequestBurst1sThreshold(int val)
      The burst threshold for 1-second sliding window.
      Parameters:
      val - the burst threshold (clamped to 1-100)
    • getRequestRejectThreshold

      public static int getRequestRejectThreshold()
      The rejection threshold (30-100, default 70%).
      Returns:
      the rejection threshold percentage
    • setRequestRejectThreshold

      public static void setRequestRejectThreshold(int val)
      The rejection threshold percentage.
      Parameters:
      val - the rejection threshold (clamped to 30-100)
    • getRequestRejectSteepness

      public static int getRequestRejectSteepness()
      The rejection steepness (100=linear ramp, 200=quadratic).
      Returns:
      the rejection steepness value
    • setRequestRejectSteepness

      public static void setRequestRejectSteepness(int val)
      The rejection steepness (100=linear, 500=max curve).
      Parameters:
      val - the steepness value (clamped to 100-500)
    • getRequestLoadWeight

      public static int getRequestLoadWeight()
      The load weight (how much load inflates effective ratio, 0-300%).
      Returns:
      the load weight percentage
    • setRequestLoadWeight

      public static void setRequestLoadWeight(int val)
      The load weight percentage.
      Parameters:
      val - the load weight (clamped to 0-300)
    • getHighLoadLagMs

      public static int getHighLoadLagMs()
      The high-load job lag threshold in milliseconds.
      Returns:
      the high-load lag threshold (ms)
    • setHighLoadLagMs

      public static void setHighLoadLagMs(int val)
      The high-load job lag threshold in milliseconds.
      Parameters:
      val - the high-load lag threshold (clamped to 200-5000)
    • getHighLoadCpuPct

      public static int getHighLoadCpuPct()
      The high-load CPU threshold percentage.
      Returns:
      the high-load CPU percentage
    • setHighLoadCpuPct

      public static void setHighLoadCpuPct(int val)
      The high-load CPU threshold percentage.
      Parameters:
      val - the CPU percentage (clamped to 50-100)
    • getHighLoadSysLoadPct

      public static int getHighLoadSysLoadPct()
      The high-load system load threshold percentage (normalized by cores).
      Returns:
      the high-load system load percentage
    • setHighLoadSysLoadPct

      public static void setHighLoadSysLoadPct(int val)
      The high-load system load threshold percentage.
      Parameters:
      val - the system load percentage (clamped to 50-100)
    • getModerateLoadLagMs

      public static int getModerateLoadLagMs()
      The moderate-load job lag threshold in milliseconds.
      Returns:
      the moderate-load lag threshold (ms)
    • setModerateLoadLagMs

      public static void setModerateLoadLagMs(int val)
      The moderate-load job lag threshold in milliseconds.
      Parameters:
      val - the moderate-load lag threshold (clamped to 100-3000)
    • getModerateLoadCpuPct

      public static int getModerateLoadCpuPct()
      The moderate-load CPU threshold percentage.
      Returns:
      the moderate-load CPU percentage
    • setModerateLoadCpuPct

      public static void setModerateLoadCpuPct(int val)
      The moderate-load CPU threshold percentage.
      Parameters:
      val - the CPU percentage (clamped to 40-100)
    • getModerateLoadSysLoadPct

      public static int getModerateLoadSysLoadPct()
      The moderate-load system load threshold percentage (normalized by cores).
      Returns:
      the moderate-load system load percentage
    • setModerateLoadSysLoadPct

      public static void setModerateLoadSysLoadPct(int val)
      The moderate-load system load threshold percentage.
      Parameters:
      val - the system load percentage (clamped to 30-100)
    • getSustainedModerateLoadMs

      public static long getSustainedModerateLoadMs()
      The duration (ms) moderate load must persist before disconnecting low-share peers.
      Returns:
      the sustained moderate load duration (ms)
    • setSustainedModerateLoadMs

      public static void setSustainedModerateLoadMs(long val)
      The duration (ms) moderate load must persist before disconnecting low-share peers.
      Parameters:
      val - the sustained moderate load duration (clamped to 10-300s)
    • luEnforcementProbability

      static float luEnforcementProbability(float loadScore)
      Probability of enforcing the LU prev-hop ban at a given load score: zero when idle, linear up to full enforcement at 50% load and beyond, so unloaded routers carry LU traffic while loaded ones shed it.
      Parameters:
      loadScore - the 0.0-1.0 load score from ParticipatingThrottler.calculateLoadScore(RouterContext)
      Returns:
      the enforcement probability, clamped to 0.0-1.0
    • burstThreshold

      static int burstThreshold(int configuredThreshold, int limit)
      Effective single-second burst threshold for a peer: the configured floor, raised so peers whose limit permits heavy traffic are not banned for bursts that limit allows (one tenth of the 90-second request limit as a per-second ceiling).
      Parameters:
      configuredThreshold - the i2p.tunnel.requestThrottle.burst1sThreshold value
      limit - the peer's request limit for the current window
      Returns:
      the burst threshold in requests per second
    • burstBanDurationMs

      static long burstBanDurationMs(int offenses)
      Ban length for a repeated burst offense: zero for the first BURST_BAN_OFFENSES - 1 offenses (throttle-only), then five minutes growing by five minutes per further offense, capped at thirty minutes.
      Parameters:
      offenses - the consecutive offense count, 1-based
      Returns:
      the ban duration in ms, 0 when only throttling applies
    • clamp

      private static int clamp(int val, int lo, int hi)
      Constrain a configured property to an allowed range. Used so a single expression both reads the property and clamps it, rather than storing the raw value and overwriting it on the next line.
      Parameters:
      val - the value read from the properties
      lo - the lowest permitted value
      hi - the highest permitted value
      Returns:
      val limited to [lo, hi]
    • shouldThrottle

      boolean shouldThrottle(Hash h)
      Checks if the given router's tunnel requests should be throttled. Increments the request count and evaluates limits, router capabilities, blocking policies, version, and system load to decide throttling.
      Parameters:
      h - the router's hash
      Returns:
      true if the request should be throttled (denied), false otherwise
    • isUnreachable

      private boolean isUnreachable(RouterInfo ri)
      Checks whether a router is known to be unreachable.
      Parameters:
      ri - the router info
      Returns:
      true if the router is unreachable
    • isLowShare

      private boolean isLowShare(RouterInfo ri)
      Checks if router has low bandwidth sharing capabilities.
      Parameters:
      ri - the router to check, null returns false
      Returns:
      whether low share
    • isFast

      private boolean isFast(RouterInfo ri)
      Checks if router has high bandwidth capabilities.
      Parameters:
      ri - the router to check, null returns false
      Returns:
      true if the router advertises BW256, BW512 or unbounded bandwidth
    • updateCachedProperties

      private void updateCachedProperties()
      Updates cached property values to avoid repeated property lookups.
    • isFirewalled

      private boolean isFirewalled()
      Checks if we are firewalled, with caching to avoid repeated commSystem status checks.
      Returns:
      true if firewalled, false otherwise