java.lang.Object
net.i2p.router.RouterThrottleImpl
All Implemented Interfaces:
RouterThrottle

public class RouterThrottleImplextends Object implements RouterThrottle
Simple throttle that basically stops accepting messages or nontrivial requests if the jobQueue lag is too large.
  • Field Details

    • _context

      protected final RouterContext _context
      Router context, used for the job queue, tunnel counts, bandwidth limits and rates.
    • _log

      private final Log _log
      Logger for the throttle.
    • _tunnelStatus

      private volatile String _tunnelStatus
      Tunnel status text for the reject log.
    • _rejectStartupTime

      private final long _rejectStartupTime
      Startup time before the throttle may reject.
    • JOB_LAG_LIMIT_NETWORK

      private static final long JOB_LAG_LIMIT_NETWORK
      Arbitrary hard limit - if it's taking this long to get to a job, we're congested.
      See Also:
    • PROP_MAX_TUNNELS

      public static final String PROP_MAX_TUNNELS
      Property name for the participating tunnel cap.
      See Also:
    • defaultMaxTunnels

      public static volatile int defaultMaxTunnels
      Default maximum tunnels, adjusted by system speed and memory.
    • PROP_MAX_PROCESSINGTIME

      private static final String PROP_MAX_PROCESSINGTIME
      Config key for the processing time limit.
      See Also:
    • DEFAULT_REJECT_STARTUP_TIME

      private static final long DEFAULT_REJECT_STARTUP_TIME
      Default startup delay before rejecting.
      See Also:
    • MIN_REJECT_STARTUP_TIME

      private static final long MIN_REJECT_STARTUP_TIME
      Shortest reject startup delay allowed.
      See Also:
    • PROP_REJECT_STARTUP_TIME

      private static final String PROP_REJECT_STARTUP_TIME
      Config key for the reject startup delay.
      See Also:
    • PROP_MIN_THROTTLE_TUNNELS

      private static final String PROP_MIN_THROTTLE_TUNNELS
      Config key for the minimum tunnels before throttling.
      See Also:
    • MSG_DELAY_SUSTAIN_MS

      private static final long MSG_DELAY_SUSTAIN_MS
      How long the 1-minute message-processing average must stay above the throttle threshold before we actually reject tunnel requests. Brief lag spikes (GC pauses, netdb churn, rebuild storms) that recover within this window must not trip throttling. 3 minutes > one 1-minute coalesce period, so the condition is confirmed across multiple samples.
      See Also:
    • _msgDelayOverSince

      private volatile long _msgDelayOverSince
      Timestamp (ms) the message-delay average first crossed the threshold, or -1.
    • DEFAULT_MAX_PROCESSINGTIME

      private static final int DEFAULT_MAX_PROCESSINGTIME
      Default processing time limit, in ms.
    • TUNNEL_ACCEPT

      public static final int TUNNEL_ACCEPT
      Tunnel acceptance.
      See Also:
    • PREPROCESSED_SIZE

      private static final int PREPROCESSED_SIZE
      = TrivialPreprocessor.PREPROCESSED_SIZE.
      See Also:
    • DEFAULT_MESSAGES_PER_TUNNEL_ESTIMATE

      public static final int DEFAULT_MESSAGES_PER_TUNNEL_ESTIMATE
      This is the estimated number of 1 KB tunnel messages that we'll see in 10 minute lifetime of an exploratory tunnel. We use it as a baseline minimum for estimating tunnel bandwidth, if accepted.

      600 KB in 10 minutes equals ~4 KBps - increased from 200 for better bandwidth allocation per transit tunnel.

      See Also:
    • MIN_AVAILABLE_BPS

      private static final int MIN_AVAILABLE_BPS
      Also limited to 90% - see below.
      See Also:
    • LIMIT_STR

      private static final String LIMIT_STR
      Log text for a bandwidth-limit rejection.
    • PROP_TUNNEL_GROWTH_FACTOR

      private static final String PROP_TUNNEL_GROWTH_FACTOR
      Config key for the tunnel growth factor.
      See Also:
    • PROP_TUNNEL_TEST_TIME_GROWTH_FACTOR

      private static final String PROP_TUNNEL_TEST_TIME_GROWTH_FACTOR
      Config key for the test time growth factor.
      See Also:
    • DEFAULT_TUNNEL_GROWTH_FACTOR

      private static final double DEFAULT_TUNNEL_GROWTH_FACTOR
      Default tunnel growth factor.
      See Also:
    • DEFAULT_TUNNEL_TEST_TIME_GROWTH_FACTOR

      private static final double DEFAULT_TUNNEL_TEST_TIME_GROWTH_FACTOR
      Default test time growth factor.
      See Also:
    • _tunedGrowthFactor

      private static volatile double _tunedGrowthFactor
      Tuner override for the growth factor, -1 if unset.
    • _cachedGrowthFactorProp

      private String _cachedGrowthFactorProp
      Config value the cached growth factor was read from.
    • _cachedGrowthFactor

      private double _cachedGrowthFactor
      Cached tunnel growth factor.
    • _cachedTestTimeGrowthFactorProp

      private String _cachedTestTimeGrowthFactorProp
      Config value the cached test time factor was read from.
    • _cachedTestTimeGrowthFactor

      private double _cachedTestTimeGrowthFactor
      Cached test time growth factor.
    • _cachedMaxTunnelsProp

      private String _cachedMaxTunnelsProp
      Config value the cached max tunnels was read from.
    • _cachedMaxTunnels

      private int _cachedMaxTunnels
      Cached maximum tunnel count.
    • _cachedMaxProcessingTimeProp

      private String _cachedMaxProcessingTimeProp
      Config value the cached processing time was read from.
    • _cachedMaxProcessingTime

      private int _cachedMaxProcessingTime
      Cached processing time limit, in ms.
    • _cachedMinThrottleTunnelsProp

      private String _cachedMinThrottleTunnelsProp
      Config value the cached minimum throttle tunnels was read from.
    • _cachedMinThrottleTunnels

      private int _cachedMinThrottleTunnels
      Cached minimum tunnels before throttling, -1 if unset.
    • _tunedMaxProcessingTime

      private static volatile int _tunedMaxProcessingTime
      Tuner override for the processing time, -1 if unset.
    • _tunedThrottleRejectExponent

      private static volatile int _tunedThrottleRejectExponent
      Tuner override for the reject exponent, -1 if unset.
  • Constructor Details

    • RouterThrottleImpl

      public RouterThrottleImpl(RouterContext context)
      Create a throttle and schedule the reset of the "starting up" tunnel status.
      Parameters:
      context - the router context
  • Method Details

    • getDefaultMaxTunnels

      public static int getDefaultMaxTunnels()
      Participating tunnel cap in force when the property is unset.
      Returns:
      the default tunnel count
    • setDefaultMaxTunnels

      public static void setDefaultMaxTunnels(int val)
      Set the default tunnel cap, clamped to 500..20000.
      Parameters:
      val - participating tunnels to use when the property is unset
    • acceptNetworkMessage

      public boolean acceptNetworkMessage()
      Should we accept any more data from the network for any sort of message, taking into account our current load, or should we simply slow down?
      Specified by:
      acceptNetworkMessage in interface RouterThrottle
      Returns:
      true to take more network traffic, false to hold off for a while
    • acceptTunnelRequest

      public int acceptTunnelRequest()
      If we should send a reject, return a nonzero reject code. Anything that causes us to drop a request instead of rejecting it must go in BuildHandler.handleInboundRequest(), not here.
      Specified by:
      acceptTunnelRequest in interface RouterThrottle
      Returns:
      0 for accept or nonzero reject code
    • getMinBandwidthFloorPerTunnel

      public static int getMinBandwidthFloorPerTunnel(RouterContext ctx)
      Calculate a floor for per-tunnel bandwidth allocation based on participating tunnel count and total outbound bandwidth share. This ensures a minimum allocation even when many tunnels are hosted.
      Parameters:
      ctx - the router context supplying bandwidth, share percentage and tunnel count
      Returns:
      minimum bytes per second to allocate per tunnel
    • allowTunnel

      private boolean allowTunnel(double bytesAllocated, int numTunnels)
      With bytesAllocated already accounted for across the numTunnels existing tunnels we have agreed to, can we handle another tunnel with our existing bandwidth?
      Parameters:
      bytesAllocated - bytes per second already allocated across the existing tunnels
      numTunnels - the number of participating tunnels we are in
      Returns:
      true if another tunnel request should be allowed
    • getMinThrottleTunnels

      private int getMinThrottleTunnels()
      Don't ever probabilistically throttle tunnels if we have less than this many.
      Returns:
      the configured minimum, or two thirds of the maximum tunnel count when the property is unset or invalid
    • setTunnelGrowthFactor

      public static void setTunnelGrowthFactor(double factor)
      Set the tunnel growth factor (called by Tuner).
      Parameters:
      factor - the tunnel growth factor
    • getTunnelGrowthFactorTuned

      public static double getTunnelGrowthFactorTuned(RouterContext ctx)
      The tunnel growth factor in effect, tuned value if set else config.
      Parameters:
      ctx - the router context
      Returns:
      the tunnel growth factor
    • getMaxTunnels

      private int getMaxTunnels()
      Returns the cached maximum tunnel count.
      Returns:
      the maximum tunnels
    • setMaxProcessingTime

      public static void setMaxProcessingTime(int ms)
      Set the max processing time (called by Tuner).
      Parameters:
      ms - the max message processing time in milliseconds
    • getMaxProcessingTimeTuned

      public static int getMaxProcessingTimeTuned(RouterContext ctx)
      The max processing time in effect, tuned value if set else config.
      Parameters:
      ctx - the router context
      Returns:
      the max message processing time in milliseconds
    • setThrottleRejectExponent

      public static void setThrottleRejectExponent(int exp)
      Set the throttle rejection-curve exponent (called by Tuner).
      Parameters:
      exp - the rejection exponent in effect
    • getThrottleRejectExponent

      public static int getThrottleRejectExponent(RouterContext ctx)
      The throttle rejection-curve exponent, tuned value if set else the router.throttleRejectExponent config, clamped to the caller's [3,20] validity window.
      Parameters:
      ctx - the router context
      Returns:
      the rejection exponent
    • throttleRejectExponentMin

      public static int throttleRejectExponentMin()
      The lowest rejection exponent the throttle applies.

      Public because the auto-tuner declares a range for this exponent and must not propose a value the read path clamps away: the target lands back on the current value and the param reads as one that will not take effect.

      Returns:
      the lower end of the validity window
    • throttleRejectExponentMax

      public static int throttleRejectExponentMax()
      The highest rejection exponent the throttle applies.
      Returns:
      the upper end of the validity window
    • getMaxProcessingTime

      private int getMaxProcessingTime()
      Returns the cached processing time limit.
      Returns:
      the limit, in ms
    • getTunnelGrowthFactor

      private double getTunnelGrowthFactor()
      Returns the tunnel growth factor in effect.
      Returns:
      the growth factor
    • getTunnelTestTimeGrowthFactor

      private double getTunnelTestTimeGrowthFactor()
      Returns the test time growth factor in effect.
      Returns:
      the growth factor
    • getMessageDelay

      public long getMessageDelay()
      The average message processing delay.
      Specified by:
      getMessageDelay in interface RouterThrottle
      Returns:
      the message delay
    • getTunnelLag

      public long getTunnelLag()
      The current tunnel lag.
      Specified by:
      getTunnelLag in interface RouterThrottle
      Returns:
      the tunnel lag
    • getTunnelStatus

      public String getTunnelStatus()
      The current tunnel status message.
      Specified by:
      getTunnelStatus in interface RouterThrottle
      Returns:
      the tunnel status
    • getLocalizedTunnelStatus

      public String getLocalizedTunnelStatus()
      The current tunnel status, translated if available.
      Specified by:
      getLocalizedTunnelStatus in interface RouterThrottle
      Returns:
      the localized tunnel status
    • setTunnelStatus

      private void setTunnelStatus()
      Rebuilds the tunnel status text for the reject log.
    • isShuttingDown

      public static boolean isShuttingDown(RouterContext context)
      Whether the router is shutting down.
      Parameters:
      context - the router context
      Returns:
      whether shutting down
    • setShutdownStatus

      public void setShutdownStatus()
      Throttle hard while the router attempts to shut down.
      Specified by:
      setShutdownStatus in interface RouterThrottle
    • cancelShutdownStatus

      public void cancelShutdownStatus()
      Restore normal throttling after a shutdown attempt.
      Specified by:
      cancelShutdownStatus in interface RouterThrottle
    • setTunnelStatus

      public void setTunnelStatus(String msg)
      The tunnel acceptance status message.
      Specified by:
      setTunnelStatus in interface RouterThrottle
      Parameters:
      msg - the status message
    • _x

      private static final String _x(String s)
      Mark a string for extraction by xgettext and translation. Use this only in static initializers. It does not translate!
      Parameters:
      s - the literal string to mark for extraction
      Returns:
      s unchanged, so xgettext still sees the literal in the source