Class ConnectionSelectorPolicies

java.lang.Object
io.servicetalk.loadbalancer.ConnectionSelectorPolicies

public final class ConnectionSelectorPolicies extends Object
A factory to create different ConnectionSelectorPolicy variants.
  • Method Details

    • corePool

      public static <C extends LoadBalancedConnection> ConnectionSelectorPolicy<C> corePool(int corePoolSize, boolean forceCorePool)
      A connection selection policy that prioritizes a configurable "core" pool.

      This ConnectionSelectorPolicy attempts to emulate the pooling behavior often seen in thread pools. Specifically it allows for the configuration of a "core pool" size which are intended to be long-lived. Iteration starts in the core pool at a random position and then iterates through the entire core pool before moving to an overflow pool. Because iteration of this core pool starts at a random position the core connections will get an even traffic load and, because they are equally selectable, will tend not to be removed due to idleness.

      If the core pool cannot satisfy the load traffic can spill over to extra connections which are selected in-order. This has the property of minimizing traffic to the latest elements added outside the core pool size, thus let them idle out of the pool once they're no longer necessary.

      Type Parameters:
      C - the concrete type of the LoadBalancedConnection
      Parameters:
      corePoolSize - the size of the core pool.
      forceCorePool - whether to avoid selecting connections from the core pool until it has reached the configured core pool size.
      Returns:
      the configured ConnectionSelectorPolicy.
    • linearSearch

      public static <C extends LoadBalancedConnection> ConnectionSelectorPolicy<C> linearSearch()
      A connection selection policy that prioritizes connection reuse.

      This ConnectionSelectorPolicy attempts to minimize the number of connections by attempting to direct traffic to connections in the order they were created in linear order up until a configured quantity. After this linear pool is exhausted the remaining connections will be selected from at random. Prioritizing traffic to the existing connections will let tailing connections be removed due to idleness.

      Type Parameters:
      C - the concrete type of the LoadBalancedConnection
      Returns:
      the configured ConnectionSelectorPolicy.
    • linearSearch

      public static <C extends LoadBalancedConnection> ConnectionSelectorPolicy<C> linearSearch(int linearSearchSpace)
      A connection selection policy that prioritizes connection reuse.

      This ConnectionSelectorPolicy attempts to minimize the number of connections by attempting to direct traffic to connections in the order they were created in linear order up until a configured quantity. After this linear pool is exhausted the remaining connections will be selected from at random. Prioritizing traffic to the existing connections will let tailing connections be removed due to idleness.

      Type Parameters:
      C - the concrete type of the LoadBalancedConnection
      Parameters:
      linearSearchSpace - the space to search linearly before resorting to random selection for remaining connections.
      Returns:
      the configured ConnectionSelectorPolicy.
    • p2c

      public static <C extends LoadBalancedConnection> ConnectionSelectorPolicy<C> p2c(int corePoolSize, boolean forceCorePool)
      A ConnectionSelectorPolicy that attempts to discern between the health of individual connections. If individual connections have health data the P2C policy can be used to bias traffic toward the best connections. This has the following algorithm:
      1. Randomly select two connections from the 'core pool' (pick-two).
        1. Try to select the 'best' of the two connections.
        2. If we fail to select the best connection, try the other connection.
      2. If both connections fail, repeat the pick-two operation for up to maxEffort attempts, begin linear iteration through the remaining connections searching for an acceptable connection.
      Type Parameters:
      C - the concrete type of the LoadBalancedConnection
      Parameters:
      corePoolSize - the size of the core pool.
      forceCorePool - whether to avoid selecting connections from the core pool until it has reached the configured core pool size.
      Returns:
      the configured ConnectionSelectorPolicy.
    • p2c

      public static <C extends LoadBalancedConnection> ConnectionSelectorPolicy<C> p2c(int maxEffort, int corePoolSize, boolean forceCorePool)
      A ConnectionSelectorPolicy that attempts to discern between the health of individual connections. If individual connections have health data the P2C policy can be used to bias traffic toward the best connections. This has the following algorithm:
      1. Randomly select two connections from the 'core pool' (pick-two).
        1. Try to select the 'best' of the two connections.
        2. If we fail to select the best connection, try the other connection.
      2. If both connections fail, repeat the pick-two operation for up to maxEffort attempts, begin linear iteration through the remaining connections searching for an acceptable connection.
      Type Parameters:
      C - the concrete type of the LoadBalancedConnection
      Parameters:
      maxEffort - the maximum number of attempts to pick a healthy connection from the core pool.
      corePoolSize - the size of the core pool.
      forceCorePool - whether to avoid selecting connections from the core pool until it has reached the configured core pool size.
      Returns:
      the configured ConnectionSelectorPolicy.