Skip to content

Commit

Permalink
Reintroduce canHandleEmptyAddressListFromNameResolution()
Browse files Browse the repository at this point in the history
  • Loading branch information
temawi committed Jul 22, 2022
1 parent cc7037f commit 7a9061b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
33 changes: 27 additions & 6 deletions api/src/main/java/io/grpc/LoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,20 @@ public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
*/
@SuppressWarnings("deprecation")
public boolean acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
if (recursionCount++ == 0) {
handleResolvedAddresses(resolvedAddresses);
}
recursionCount = 0;
if (resolvedAddresses.getAddresses().isEmpty()
&& !canHandleEmptyAddressListFromNameResolution()) {
handleNameResolutionError(Status.UNAVAILABLE.withDescription(
"NameResolver returned no usable address. addrs=" + resolvedAddresses.getAddresses()
+ ", attrs=" + resolvedAddresses.getAttributes()));
return false;
} else {
if (recursionCount++ == 0) {
handleResolvedAddresses(resolvedAddresses);
}
recursionCount = 0;

// The legacy method does not tell us if the addresses were accepted, so we default to true.
return true;
return true;
}
}

/**
Expand Down Expand Up @@ -366,6 +373,20 @@ public void handleSubchannelState(
*/
public abstract void shutdown();

/**
* Whether this LoadBalancer can handle empty address group list to be passed to {@link
* #handleResolvedAddresses(ResolvedAddresses)}. The default implementation returns
* {@code false}, meaning that if the NameResolver returns an empty list, the Channel will turn
* that into an error and call {@link #handleNameResolutionError}. LoadBalancers that want to
* accept empty lists should override this method and return {@code true}.
*
* <p>This method should always return a constant value. It's not specified when this will be
* called.
*/
public boolean canHandleEmptyAddressListFromNameResolution() {
return false;
}

/**
* The channel asks the LoadBalancer to establish connections now (if applicable) so that the
* upcoming RPC may then just pick a ready connection without waiting for connections. This
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/io/grpc/util/ForwardingLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public void shutdown() {
delegate().shutdown();
}

@Override
public boolean canHandleEmptyAddressListFromNameResolution() {
return delegate().canHandleEmptyAddressListFromNameResolution();
}

@Override
public void requestConnection() {
delegate().requestConnection();
Expand Down

0 comments on commit 7a9061b

Please sign in to comment.