Skip to content

Commit

Permalink
Remove the provider and directly use the property
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-anderson committed Dec 12, 2024
1 parent 5ae052a commit 40c6c62
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
public final class RoundRobinLoadBalancerFactory<ResolvedAddress, C extends LoadBalancedConnection>
implements LoadBalancerFactory<ResolvedAddress, C> {

static final String ROUND_ROBIN_USER_DEFAULT_LOAD_BALANCER =
"io.servicetalk.loadbalancer.roundRobinUsesDefaultLoadBalancer";

private final String id;
private final int linearSearchSpace;
@Nullable
Expand All @@ -74,17 +77,19 @@ public <T extends C> LoadBalancer<T> newLoadBalancer(
final String targetResource,
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
return new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
linearSearchSpace, healthCheckConfig);
return useDefaultLoadBalancer() ?
buildDefaultLoadBalancerFactory(targetResource, eventPublisher, connectionFactory) :
new RoundRobinLoadBalancer<>(
id, targetResource, eventPublisher, connectionFactory, linearSearchSpace, healthCheckConfig);
}

@Override
public LoadBalancer<C> newLoadBalancer(
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, C> connectionFactory,
final String targetResource) {
return new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
linearSearchSpace, healthCheckConfig);
// for now, we forward to the deprecated method since it can support both
return newLoadBalancer(targetResource, eventPublisher, connectionFactory);
}

@Override
Expand All @@ -102,6 +107,59 @@ public String toString() {
'}';
}

private <T extends C> LoadBalancer<T> buildDefaultLoadBalancerFactory(
final String targetResource,
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
int healthCheckFailedConnectionsThreshold;
Duration healthCheckInterval;
Duration healthCheckJitter;
Duration healthCheckResubscribeInterval;
Duration healthCheckResubscribeJitter;
Executor backgroundExecutor;
if (healthCheckConfig == null) {
healthCheckFailedConnectionsThreshold = -1;
healthCheckInterval = DEFAULT_HEALTH_CHECK_INTERVAL;
healthCheckJitter = DEFAULT_HEALTH_CHECK_JITTER;
healthCheckResubscribeInterval = DEFAULT_HEALTH_CHECK_RESUBSCRIBE_INTERVAL;
healthCheckResubscribeJitter = DEFAULT_HEALTH_CHECK_JITTER;
backgroundExecutor = null;
} else {
healthCheckFailedConnectionsThreshold = healthCheckConfig.failedThreshold;
healthCheckInterval = healthCheckConfig.healthCheckInterval;
healthCheckJitter = healthCheckConfig.jitter;
healthCheckResubscribeInterval = healthCheckConfig.resubscribeInterval;
healthCheckResubscribeJitter = healthCheckConfig.healthCheckResubscribeJitter;
backgroundExecutor = healthCheckConfig.executor;
}

OutlierDetectorConfig outlierDetectorConfig = new OutlierDetectorConfig.Builder()
.ewmaHalfLife(Duration.ZERO)
.enforcingFailurePercentage(0)
.enforcingSuccessRate(0)
.enforcingConsecutive5xx(0)
.failedConnectionsThreshold(healthCheckFailedConnectionsThreshold)
.failureDetectorInterval(healthCheckInterval, healthCheckJitter)
.serviceDiscoveryResubscribeInterval(healthCheckResubscribeInterval, healthCheckResubscribeJitter)
.build();

LoadBalancingPolicy<ResolvedAddress, T> loadBalancingPolicy =
LoadBalancingPolicies.roundRobin()
.failOpen(false)
.ignoreWeights(true)
.build();

LoadBalancerBuilder<ResolvedAddress, T> builder = LoadBalancers.builder(id);
if (backgroundExecutor != null) {
builder = builder.backgroundExecutor(backgroundExecutor);
}
return builder.outlierDetectorConfig(outlierDetectorConfig)
.loadBalancingPolicy(loadBalancingPolicy)
.connectionPoolPolicy(ConnectionPoolPolicies.linearSearch(linearSearchSpace))
.build()
.newLoadBalancer(eventPublisher, connectionFactory, targetResource);
}

/**
* Builder for {@link RoundRobinLoadBalancerFactory}.
*
Expand Down Expand Up @@ -227,4 +285,10 @@ static Executor getInstance() {
return INSTANCE;
}
}

private static boolean useDefaultLoadBalancer() {
// Enabled by default.
String propValue = System.getProperty(ROUND_ROBIN_USER_DEFAULT_LOAD_BALANCER);
return propValue == null || Boolean.parseBoolean(propValue);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 40c6c62

Please sign in to comment.