Skip to content

Commit

Permalink
Extract inner types from RoundRobinLoadBalancer (#2724)
Browse files Browse the repository at this point in the history
Motivation:

The RoundRobinLoadBalancer contains a bunch of code that is doing a
good deal more than load balancing, namely it's doing pooling, circuit
breaking, and other things. This both makes the implementation hard to
read and reason about and makes the inner pieces difficult to reuse for
future load balancer implementation.

Modifications:

- Extract the types into their own files.
- Hide a bit of the internal state to start to define abstraction boundaries.
  Note that this is pretty minimal so far, just accessors etc, so to keep
  this PR mechanical in nature.
  • Loading branch information
bryce-anderson authored Oct 17, 2023
1 parent 53ae438 commit f00a486
Show file tree
Hide file tree
Showing 5 changed files with 618 additions and 530 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright © 2021-2023 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicetalk.loadbalancer;

import io.servicetalk.client.api.ConnectionRejectedException;
import io.servicetalk.client.api.NoActiveHostException;
import io.servicetalk.client.api.NoAvailableHostException;
import io.servicetalk.concurrent.internal.ThrowableUtils;

final class Exceptions {

static final class StacklessNoAvailableHostException extends NoAvailableHostException {
private static final long serialVersionUID = 5942960040738091793L;

private StacklessNoAvailableHostException(final String message) {
super(message);
}

@Override
public Throwable fillInStackTrace() {
return this;
}

static StacklessNoAvailableHostException newInstance(String message, Class<?> clazz, String method) {
return ThrowableUtils.unknownStackTrace(new StacklessNoAvailableHostException(message), clazz, method);
}
}

static final class StacklessNoActiveHostException extends NoActiveHostException {

private static final long serialVersionUID = 7500474499335155869L;

private StacklessNoActiveHostException(final String message) {
super(message);
}

@Override
public Throwable fillInStackTrace() {
return this;
}

static StacklessNoActiveHostException newInstance(String message, Class<?> clazz, String method) {
return ThrowableUtils.unknownStackTrace(new StacklessNoActiveHostException(message), clazz, method);
}
}

static final class StacklessConnectionRejectedException extends ConnectionRejectedException {
private static final long serialVersionUID = -4940708893680455819L;

private StacklessConnectionRejectedException(final String message) {
super(message);
}

@Override
public Throwable fillInStackTrace() {
return this;
}

static StacklessConnectionRejectedException newInstance(String message, Class<?> clazz, String method) {
return ThrowableUtils.unknownStackTrace(new StacklessConnectionRejectedException(message), clazz, method);
}
}

private Exceptions() {
// no instances
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright © 2021-2023 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicetalk.loadbalancer;

import io.servicetalk.concurrent.api.Executor;

import java.time.Duration;

final class HealthCheckConfig {
final Executor executor;
final Duration healthCheckInterval;
final Duration jitter;
final int failedThreshold;
final long healthCheckResubscribeLowerBound;
final long healthCheckResubscribeUpperBound;

HealthCheckConfig(final Executor executor, final Duration healthCheckInterval, final Duration healthCheckJitter,
final int failedThreshold, final long healthCheckResubscribeLowerBound,
final long healthCheckResubscribeUpperBound) {
this.executor = executor;
this.healthCheckInterval = healthCheckInterval;
this.failedThreshold = failedThreshold;
this.jitter = healthCheckJitter;
this.healthCheckResubscribeLowerBound = healthCheckResubscribeLowerBound;
this.healthCheckResubscribeUpperBound = healthCheckResubscribeUpperBound;
}
}
Loading

0 comments on commit f00a486

Please sign in to comment.