Skip to content

Commit

Permalink
loadbalancer-experimental: Add default policies
Browse files Browse the repository at this point in the history
Motivation:

Users currently need to understand a lot about the different policies
in order to make a good decision.

Modifications:

Add default connection pool and load balancing policies. This makes it
easy for users to pick the recommended default. This also gives us as
maintainers a place to change the recommended default.
  • Loading branch information
bryce-anderson committed Dec 11, 2024
1 parent 869c12a commit 707643c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ private ConnectionPoolPolicies() {
// no instances
}

/**
* Get the recommended default {@link ConnectionPoolPolicy}.
* @param <C> the concrete type of the {@link LoadBalancedConnection}
* @return the recommended default {@link ConnectionPoolPolicy}.
*/
public static <C extends LoadBalancedConnection> ConnectionPoolPolicy<C> defaultPolicy() {
return linearSearch();
}

/**
* A connection selection policy that prioritizes a configurable "core" pool.
* <p>
Expand All @@ -44,8 +53,8 @@ private ConnectionPoolPolicies() {
* @param <C> the concrete type of the {@link LoadBalancedConnection}
* @return the configured {@link ConnectionPoolPolicy}.
*/
public static <C extends LoadBalancedConnection> ConnectionPoolPolicy<C>
corePool(final int corePoolSize, final boolean forceCorePool) {
public static <C extends LoadBalancedConnection>
ConnectionPoolPolicy<C> corePool(final int corePoolSize, final boolean forceCorePool) {
return CorePoolConnectionSelector.factory(corePoolSize, forceCorePool);
}

Expand Down Expand Up @@ -94,8 +103,8 @@ public static <C extends LoadBalancedConnection> ConnectionPoolPolicy<C> linearS
* @param <C> the concrete type of the {@link LoadBalancedConnection}
* @return the configured {@link ConnectionPoolPolicy}.
*/
public static <C extends LoadBalancedConnection> ConnectionPoolPolicy<C>
p2c(int corePoolSize, boolean forceCorePool) {
public static <C extends LoadBalancedConnection>
ConnectionPoolPolicy<C> p2c(int corePoolSize, boolean forceCorePool) {
return p2c(DEFAULT_MAX_EFFORT, corePoolSize, forceCorePool);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ final class DefaultLoadBalancerBuilder<ResolvedAddress, C extends LoadBalancedCo
private Executor backgroundExecutor;
@Nullable
private LoadBalancerObserverFactory loadBalancerObserverFactory;
private LoadBalancingPolicy<ResolvedAddress, C> loadBalancingPolicy = defaultLoadBalancingPolicy();
private ConnectionPoolPolicy<C> connectionPoolPolicy = defaultConnectionSelectorFactory();
private LoadBalancingPolicy<ResolvedAddress, C> loadBalancingPolicy = LoadBalancingPolicies.defaultPolicy();
private ConnectionPoolPolicy<C> connectionPoolPolicy = ConnectionPoolPolicies.defaultPolicy();
private OutlierDetectorConfig outlierDetectorConfig = OutlierDetectorConfig.DEFAULT_CONFIG;

// package private constructor so users must funnel through providers in `LoadBalancers`
Expand Down Expand Up @@ -181,14 +181,4 @@ private Executor getExecutor() {
return backgroundExecutor ==
null ? RoundRobinLoadBalancerFactory.SharedExecutor.getInstance() : backgroundExecutor;
}

private static <ResolvedAddress, C extends LoadBalancedConnection>
LoadBalancingPolicy<ResolvedAddress, C> defaultLoadBalancingPolicy() {
return LoadBalancingPolicies.roundRobin().build();
}

private static <C extends LoadBalancedConnection> ConnectionPoolPolicy<C>
defaultConnectionSelectorFactory() {
return ConnectionPoolPolicies.linearSearch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.servicetalk.loadbalancer;

import io.servicetalk.client.api.LoadBalancedConnection;

/**
* A collections of factories for constructing a {@link LoadBalancingPolicy}.
*/
Expand All @@ -24,6 +26,15 @@ private LoadBalancingPolicies() {
// no instances.
}

/**
* Get the recommended default {@link LoadBalancingPolicy}.
* @return the recommended default {@link LoadBalancingPolicy}.
*/
public static <ResolvedAddress, C extends LoadBalancedConnection>
LoadBalancingPolicy<ResolvedAddress, C> defaultPolicy() {
return LoadBalancingPolicies.roundRobin().build();
}

/**
* Builder for the round-robin {@link LoadBalancingPolicy}.
* Round-robin load balancing is a strategy that maximizes fairness of the request distribution. This comes at the
Expand Down

0 comments on commit 707643c

Please sign in to comment.