netty: Simplify and increase compat of UdsNettyChannelProvider

The transport should be usable with non-`unix:` name resolvers. As long
as the name resolver returns the correct socket address type, things
should work fine.
This commit is contained in:
Eric Anderson 2022-05-05 13:14:05 -07:00
parent c20904d681
commit a8bd0b8c92
2 changed files with 8 additions and 63 deletions

View File

@ -16,15 +16,12 @@
package io.grpc.netty;
import io.grpc.CallCredentials;
import io.grpc.ChannelCredentials;
import io.grpc.InsecureChannelCredentials;
import io.grpc.Internal;
import io.grpc.ManagedChannelProvider;
import io.grpc.internal.SharedResourcePool;
import io.netty.channel.unix.DomainSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
@ -44,47 +41,23 @@ public final class UdsNettyChannelProvider extends ManagedChannelProvider {
@Override
public NettyChannelBuilder builderForAddress(String name, int port) {
throw new UnsupportedOperationException("host:port not supported");
throw new AssertionError("NettyChannelProvider shadows this implementation");
}
@Override
public NettyChannelBuilder builderForTarget(String target) {
ChannelCredentials creds = InsecureChannelCredentials.create();
ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(creds);
if (result.error != null) {
throw new RuntimeException(result.error);
}
return getNettyChannelBuilder(target, creds, null, result.negotiator);
throw new AssertionError("NettyChannelProvider shadows this implementation");
}
@Override
public NewChannelBuilderResult newChannelBuilder(String target, ChannelCredentials creds) {
ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(creds);
if (result.error != null) {
return NewChannelBuilderResult.error(result.error);
NewChannelBuilderResult result = new NettyChannelProvider().newChannelBuilder(target, creds);
if (result.getChannelBuilder() != null) {
((NettyChannelBuilder) result.getChannelBuilder())
.eventLoopGroupPool(SharedResourcePool.forResource(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP))
.channelType(Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE);
}
return NewChannelBuilderResult.channelBuilder(
getNettyChannelBuilder(target, creds, result.callCredentials, result.negotiator));
}
private static NettyChannelBuilder getNettyChannelBuilder(
String target,
ChannelCredentials creds,
CallCredentials callCredentials,
ProtocolNegotiator.ClientFactory negotiator) {
if (Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE == null) {
throw new IllegalStateException("Epoll is not available");
}
String targetPath = UdsNameResolverProvider.getTargetPathFromUri(URI.create(target));
NettyChannelBuilder builder =
new NettyChannelBuilder(
new DomainSocketAddress(targetPath), creds, callCredentials, negotiator);
builder =
builder
.eventLoopGroupPool(
SharedResourcePool.forResource(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP))
.channelType(Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE);
return builder;
return result;
}
@Override

View File

@ -100,23 +100,6 @@ public class UdsNettyChannelProviderTest {
assertEquals(3, provider.priority());
}
@Test
public void builderForTarget() {
Assume.assumeTrue(Utils.isEpollAvailable());
assertThat(provider.builderForTarget("unix:sock.sock")).isInstanceOf(NettyChannelBuilder.class);
}
@Test
public void builderForTarget_badScheme() {
Assume.assumeTrue(Utils.isEpollAvailable());
try {
provider.builderForTarget("dns:sock.sock");
fail("exception expected");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().isEqualTo("scheme must be unix");
}
}
@Test
public void newChannelBuilder_success() {
Assume.assumeTrue(Utils.isEpollAvailable());
@ -125,17 +108,6 @@ public class UdsNettyChannelProviderTest {
assertThat(result.getChannelBuilder()).isInstanceOf(NettyChannelBuilder.class);
}
@Test
public void newChannelBuilder_badScheme() {
Assume.assumeTrue(Utils.isEpollAvailable());
try {
provider.newChannelBuilder("dns:sock.sock", InsecureChannelCredentials.create());
fail("exception expected");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().isEqualTo("scheme must be unix");
}
}
@Test
public void managedChannelRegistry_newChannelBuilder() {
Assume.assumeTrue(Utils.isEpollAvailable());