java.lang.Object
net.i2p.router.peermanager.CapacityCalculator

class CapacityCalculatorextends Object
Estimate how many of our tunnels the peer can join per hour.

Combines measured tunnel accept/reject/fail rates across three time windows (10 min, 1 hour, 1 day) with capability-based penalties and bonuses (congestion caps, unreachable, established connection, XOR distance). Peers with no recent activity (tooOld) default to 1 tunnel/hour. New peers receive a time-based penalty that ramps from PENALTY_NEW down to 0 over the first 2 hours of observed lifetime.

  • Field Summary

    Modifier and Type
    Field
    Description
    private static double
    Cached moderate congestion penalty.
    private static String
    Config text the cached D penalty was parsed from.
    private static double
    Cached severe congestion penalty.
    private static String
    Config text the cached E penalty was parsed from.
    private static final double
    Bonus for a peer we are connected to.
    private static final double
    Bonus when the last send to the peer succeeded.
    private static final double
    Bonus for a peer that is not a floodfill.
    private static final double
    Bonus for a recent send success.
    private static final double
    Bonus for a peer close in XOR distance.
    private static long
    The calculator estimates over a 1 hour period.
    (package private) static final long
    Used to adjust each period so that we keep trying to expand the peer's capacity.
    private static final double
    Default penalty for moderate congestion.
    private static final double
    Default penalty for severe congestion.
    private static final double
    Penalty for a peer with the low bandwidth flag.
    private static final double
    Penalty when the last send to the peer failed.
    private static final double
    Penalty for a peer never tried.
    private static final double
    Penalty for a peer without the reachable flag.
    private static final double
    Penalty for a peer with no router info.
    private static final double
    Penalty for a recent send failure.
    private static final double
    Penalty for a peer with the unreachable flag.
    private static final double
    Penalty for a peer marked unreachable.
    static final String
    Property for bonus applied to peers in the same country.
    private static final String
    Config key for the moderate congestion penalty.
    private static final String
    Config key for the severe congestion penalty.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Prevents instantiating the utility class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    calc(PeerProfile profile)
    Calculate the capacity for the given peer profile.
    private static double
    estimateCapacity(RateStat acceptStat, RateStat rejectStat, RateStat failedStat, int period)
    Compute a tunnel accept capacity-per-hour for the given period This is perhaps the most critical part of the peer ranking and selection system, so adjust with great care and testing to ensure good network performance and prevent congestion collapse.
    private static double
    Get the D-cap penalty, cached to avoid repeated Double.parseDouble.
    private static double
    Get the E-cap penalty, cached to avoid repeated Double.parseDouble.
    private static boolean
    tooOld(PeerProfile profile, long now)
    If we haven't heard from them in an hour, they aren't too useful.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PROP_COUNTRY_BONUS

      public static final String PROP_COUNTRY_BONUS
      Property for bonus applied to peers in the same country.
      See Also:
    • GROWTH_FACTOR

      static final long GROWTH_FACTOR
      Used to adjust each period so that we keep trying to expand the peer's capacity.
      See Also:
    • ESTIMATE_PERIOD

      private static long ESTIMATE_PERIOD
      The calculator estimates over a 1 hour period.
    • BONUS_ESTABLISHED

      private static final double BONUS_ESTABLISHED
      Bonus for a peer we are connected to. The total of all the bonuses stays under 4, since bad peers start at 1 and the base is 5.
      See Also:
    • BONUS_XOR

      private static final double BONUS_XOR
      Bonus for a peer close in XOR distance.
      See Also:
    • PENALTY_NEW

      private static final double PENALTY_NEW
      Penalty for a peer never tried.
      See Also:
    • PENALTY_UNREACHABLE

      private static final double PENALTY_UNREACHABLE
      Penalty for a peer marked unreachable.
      See Also:
    • PENALTY_NO_RI

      private static final double PENALTY_NO_RI
      Penalty for a peer with no router info.
      See Also:
    • PENALTY_L_CAP

      private static final double PENALTY_L_CAP
      Penalty for a peer with the low bandwidth flag.
      See Also:
    • PENALTY_NO_R_CAP

      private static final double PENALTY_NO_R_CAP
      Penalty for a peer without the reachable flag.
      See Also:
    • PENALTY_U_CAP

      private static final double PENALTY_U_CAP
      Penalty for a peer with the unreachable flag.
      See Also:
    • PENALTY_LAST_SEND_FAIL

      private static final double PENALTY_LAST_SEND_FAIL
      Penalty when the last send to the peer failed.
      See Also:
    • PROP_D_CAP

      private static final String PROP_D_CAP
      Config key for the moderate congestion penalty.
      See Also:
    • PENALTY_CAP_D

      private static final double PENALTY_CAP_D
      Default penalty for moderate congestion.
      See Also:
    • PROP_E_CAP

      private static final String PROP_E_CAP
      Config key for the severe congestion penalty.
      See Also:
    • PENALTY_CAP_E

      private static final double PENALTY_CAP_E
      Default penalty for severe congestion.
      See Also:
    • PENALTY_RECENT_SEND_FAIL

      private static final double PENALTY_RECENT_SEND_FAIL
      Penalty for a recent send failure.
      See Also:
    • _cachedDcapProp

      private static String _cachedDcapProp
      Config text the cached D penalty was parsed from.
    • _cachedDcap

      private static double _cachedDcap
      Cached moderate congestion penalty.
    • _cachedEcapProp

      private static String _cachedEcapProp
      Config text the cached E penalty was parsed from.
    • _cachedEcap

      private static double _cachedEcap
      Cached severe congestion penalty.
    • BONUS_LAST_SEND_SUCCESS

      private static final double BONUS_LAST_SEND_SUCCESS
      Bonus when the last send to the peer succeeded.
      See Also:
    • BONUS_RECENT_SEND_SUCCESS

      private static final double BONUS_RECENT_SEND_SUCCESS
      Bonus for a recent send success.
      See Also:
    • BONUS_NON_FLOODFILL

      private static final double BONUS_NON_FLOODFILL
      Bonus for a peer that is not a floodfill. A bonus for the others, not a penalty for floodfills, keeps the floodfills at the default.
      See Also:
  • Constructor Details

    • CapacityCalculator

      private CapacityCalculator()
      Prevents instantiating the utility class.
  • Method Details

    • getCachedDcap

      private static double getCachedDcap(RouterContext ctx)
      Get the D-cap penalty, cached to avoid repeated Double.parseDouble.
      Parameters:
      ctx - the router context, source of the d-cap property
      Returns:
      the cached dcap
    • getCachedEcap

      private static double getCachedEcap(RouterContext ctx)
      Get the E-cap penalty, cached to avoid repeated Double.parseDouble.
      Parameters:
      ctx - the router context, source of the e-cap property
      Returns:
      the cached ecap
    • calc

      public static double calc(PeerProfile profile)
      Calculate the capacity for the given peer profile.
      Parameters:
      profile - the peer profile
      Returns:
      the calculated capacity
    • tooOld

      private static boolean tooOld(PeerProfile profile, long now)
      If we haven't heard from them in an hour, they aren't too useful.
      Parameters:
      profile - the peer profile whose activity is checked
      now - the current time in ms since epoch
      Returns:
      true if the peer has not been active in the last hour
    • estimateCapacity

      private static double estimateCapacity(RateStat acceptStat, RateStat rejectStat, RateStat failedStat, int period)
      Compute a tunnel accept capacity-per-hour for the given period This is perhaps the most critical part of the peer ranking and selection system, so adjust with great care and testing to ensure good network performance and prevent congestion collapse.

      The baseline or "growth factor" is 5. Rejects will not reduce the baseline. Failures will. Let A = accepts, R = rejects, F = fails.

      Parameters:
      acceptStat - Accept counter (1 = 1 accept)
      rejectStat - Reject counter (1 = 1 reject)
      failedStat - Failed counter (100 = 1 fail)
      period - the rate period in ms whose rates are evaluated
      Returns:
      estimated and adjusted accepts per hour, for the given period which is, more or less, max(0, 5 + (A * (A / (A + 2R))) - (4 * F))