Skip to content

Commit

Permalink
netty: NettyChannelBuilder extends a public API class
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiitk committed Aug 25, 2020
1 parent e7afdb3 commit 2f1107e
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 50 deletions.
21 changes: 21 additions & 0 deletions netty/src/main/java/io/grpc/netty/InternalNettyChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,31 @@ public final class InternalNettyChannelBuilder {
/**
* Checks authority upon channel construction. The purpose of this interface is to raise the
* visibility of {@link NettyChannelBuilder.OverrideAuthorityChecker}.
* @deprecated To be removed, use {@link #disableCheckAuthority(NettyChannelBuilder builder)} to
* disable authority check.
*/
@Deprecated
public interface OverrideAuthorityChecker extends NettyChannelBuilder.OverrideAuthorityChecker {}

/**
* Overrides authority checker.
* @deprecated To be removed, use {@link #disableCheckAuthority(NettyChannelBuilder builder)} to
* disable authority check.
*/
@Deprecated
public static void overrideAuthorityChecker(
NettyChannelBuilder channelBuilder, OverrideAuthorityChecker authorityChecker) {
channelBuilder.overrideAuthorityChecker(authorityChecker);
}

public static void disableCheckAuthority(NettyChannelBuilder builder) {
builder.disableCheckAuthority();
}

public static void enableCheckAuthority(NettyChannelBuilder builder) {
builder.enableCheckAuthority();
}

/** A class that provides a Netty handler to control protocol negotiation. */
public interface ProtocolNegotiatorFactory
extends NettyChannelBuilder.ProtocolNegotiatorFactory {
Expand Down Expand Up @@ -68,6 +85,10 @@ public static void setStatsRecordStartedRpcs(NettyChannelBuilder builder, boolea
builder.setStatsRecordStartedRpcs(value);
}

public static void setStatsRecordFinishedRpcs(NettyChannelBuilder builder, boolean value) {
builder.setStatsRecordFinishedRpcs(value);
}

public static void setStatsRecordRealTimeMetrics(NettyChannelBuilder builder, boolean value) {
builder.setStatsRecordRealTimeMetrics(value);
}
Expand Down
122 changes: 80 additions & 42 deletions netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@
import io.grpc.ChannelLogger;
import io.grpc.EquivalentAddressGroup;
import io.grpc.ExperimentalApi;
import io.grpc.ForwardingChannelBuilder;
import io.grpc.HttpConnectProxiedSocketAddress;
import io.grpc.Internal;
import io.grpc.internal.AbstractManagedChannelImplBuilder;
import io.grpc.ManagedChannelBuilder;
import io.grpc.internal.AtomicBackoff;
import io.grpc.internal.ClientTransportFactory;
import io.grpc.internal.ConnectionClientTransport;
import io.grpc.internal.FixedObjectPool;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.KeepAliveManager;
import io.grpc.internal.ManagedChannelImplBuilder;
import io.grpc.internal.ManagedChannelImplBuilder.ChannelBuilderDefaultPortProvider;
import io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder;
import io.grpc.internal.ObjectPool;
import io.grpc.internal.SharedResourcePool;
import io.grpc.internal.TransportTracer;
Expand All @@ -63,8 +67,7 @@
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1784")
@CanIgnoreReturnValue
public final class NettyChannelBuilder
extends AbstractManagedChannelImplBuilder<NettyChannelBuilder> {
public final class NettyChannelBuilder extends ForwardingChannelBuilder<NettyChannelBuilder> {

// 1MiB.
public static final int DEFAULT_FLOW_CONTROL_WINDOW = 1024 * 1024;
Expand All @@ -85,16 +88,16 @@ public final class NettyChannelBuilder
DEFAULT_AUTO_FLOW_CONTROL = Boolean.parseBoolean(autoFlowControl);
}

private final Map<ChannelOption<?>, Object> channelOptions =
new HashMap<>();

private final ManagedChannelImplBuilder managedChannelImplBuilder;
private TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
private final Map<ChannelOption<?>, Object> channelOptions = new HashMap<>();
private NegotiationType negotiationType = NegotiationType.TLS;
private OverrideAuthorityChecker authorityChecker;
private ChannelFactory<? extends Channel> channelFactory = DEFAULT_CHANNEL_FACTORY;
private ObjectPool<? extends EventLoopGroup> eventLoopGroupPool = DEFAULT_EVENT_LOOP_GROUP_POOL;
private SslContext sslContext;
private boolean autoFlowControl = DEFAULT_AUTO_FLOW_CONTROL;
private int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW;
private int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
private int maxHeaderListSize = GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE;
private long keepAliveTimeNanos = KEEPALIVE_TIME_NANOS_DISABLED;
private long keepAliveTimeoutNanos = DEFAULT_KEEPALIVE_TIMEOUT_NANOS;
Expand Down Expand Up @@ -142,14 +145,41 @@ public static NettyChannelBuilder forTarget(String target) {
this(GrpcUtil.authorityFromHostAndPort(host, port));
}

private final class NettyChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {
@Override
public ClientTransportFactory buildClientTransportFactory() {
return buildTransportFactory();
}
}

private final class NettyChannelDefaultPortProvider implements ChannelBuilderDefaultPortProvider {
@Override
public int getDefaultPort() {
return NettyChannelBuilder.this.getDefaultPort();
}
}

@CheckReturnValue
NettyChannelBuilder(String target) {
super(target);
super();
managedChannelImplBuilder = new ManagedChannelImplBuilder(target,
new NettyChannelTransportFactoryBuilder(),
new NettyChannelDefaultPortProvider());
}

@CheckReturnValue
NettyChannelBuilder(SocketAddress address) {
super(address, getAuthorityFromAddress(address));
super();
managedChannelImplBuilder = new ManagedChannelImplBuilder(address,
getAuthorityFromAddress(address),
new NettyChannelTransportFactoryBuilder(),
new NettyChannelDefaultPortProvider());
}

@Internal
@Override
protected ManagedChannelBuilder<?> delegate() {
return managedChannelImplBuilder;
}

@CheckReturnValue
Expand Down Expand Up @@ -408,10 +438,20 @@ public SocketAddress createSocketAddress(
}
}

/**
* Sets the maximum message size allowed for a single gRPC frame. If an inbound messages larger
* than this limit is received it will not be processed and the RPC will fail with
* RESOURCE_EXHAUSTED.
*/
@Override
public NettyChannelBuilder maxInboundMessageSize(int max) {
checkArgument(max >= 0, "negative max");
maxInboundMessageSize = max;
return this;
}

@CheckReturnValue
@Internal
protected ClientTransportFactory buildTransportFactory() {
final ClientTransportFactory buildTransportFactory() {
assertEventLoopAndChannelType();

ProtocolNegotiator negotiator;
Expand All @@ -427,12 +467,12 @@ protected ClientTransportFactory buildTransportFactory() {
}
}
negotiator = createProtocolNegotiatorByType(negotiationType, localSslContext,
this.getOffloadExecutorPool());
this.managedChannelImplBuilder.getOffloadExecutorPool());
}

return new NettyTransportFactory(
negotiator, channelFactory, channelOptions,
eventLoopGroupPool, autoFlowControl, flowControlWindow, maxInboundMessageSize(),
eventLoopGroupPool, autoFlowControl, flowControlWindow, maxInboundMessageSize,
maxHeaderListSize, keepAliveTimeNanos, keepAliveTimeoutNanos, keepAliveWithoutCalls,
transportTracerFactory, localSocketPicker, useGetForSafeMethods);
}
Expand All @@ -448,9 +488,8 @@ void assertEventLoopAndChannelType() {
"Both EventLoopGroup and ChannelType should be provided or neither should be");
}

@Override
@CheckReturnValue
protected int getDefaultPort() {
int getDefaultPort() {
switch (negotiationType) {
case PLAINTEXT:
case PLAINTEXT_UPGRADE:
Expand All @@ -462,10 +501,6 @@ protected int getDefaultPort() {
}
}

void overrideAuthorityChecker(@Nullable OverrideAuthorityChecker authorityChecker) {
this.authorityChecker = authorityChecker;
}

@VisibleForTesting
@CheckReturnValue
static ProtocolNegotiator createProtocolNegotiatorByType(
Expand All @@ -484,44 +519,47 @@ static ProtocolNegotiator createProtocolNegotiatorByType(
}
}

@CheckReturnValue
interface OverrideAuthorityChecker {
String checkAuthority(String authority);
@Deprecated
interface OverrideAuthorityChecker extends ManagedChannelImplBuilder.OverrideAuthorityChecker {}

@Deprecated
void overrideAuthorityChecker(@Nullable OverrideAuthorityChecker authorityChecker) {
this.managedChannelImplBuilder.overrideAuthorityChecker(authorityChecker);
}

@Override
@CheckReturnValue
@Internal
protected String checkAuthority(String authority) {
if (authorityChecker != null) {
return authorityChecker.checkAuthority(authority);
}
return super.checkAuthority(authority);
NettyChannelBuilder disableCheckAuthority() {
this.managedChannelImplBuilder.disableCheckAuthority();
return this;
}

NettyChannelBuilder enableCheckAuthority() {
this.managedChannelImplBuilder.enableCheckAuthority();
return this;
}

void protocolNegotiatorFactory(ProtocolNegotiatorFactory protocolNegotiatorFactory) {
this.protocolNegotiatorFactory
= checkNotNull(protocolNegotiatorFactory, "protocolNegotiatorFactory");
}

@Override
protected void setTracingEnabled(boolean value) {
super.setTracingEnabled(value);
void setTracingEnabled(boolean value) {
this.managedChannelImplBuilder.setTracingEnabled(value);
}

@Override
protected void setStatsEnabled(boolean value) {
super.setStatsEnabled(value);
void setStatsEnabled(boolean value) {
this.managedChannelImplBuilder.setStatsEnabled(value);
}

@Override
protected void setStatsRecordStartedRpcs(boolean value) {
super.setStatsRecordStartedRpcs(value);
void setStatsRecordStartedRpcs(boolean value) {
this.managedChannelImplBuilder.setStatsRecordStartedRpcs(value);
}

@Override
protected void setStatsRecordRealTimeMetrics(boolean value) {
super.setStatsRecordRealTimeMetrics(value);
void setStatsRecordFinishedRpcs(boolean value) {
this.managedChannelImplBuilder.setStatsRecordFinishedRpcs(value);
}

void setStatsRecordRealTimeMetrics(boolean value) {
this.managedChannelImplBuilder.setStatsRecordRealTimeMetrics(value);
}

@VisibleForTesting
Expand Down
58 changes: 50 additions & 8 deletions netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static org.mockito.Mockito.mock;

import io.grpc.ManagedChannel;
import io.grpc.netty.InternalNettyChannelBuilder.OverrideAuthorityChecker;
import io.grpc.internal.GrpcUtil;
import io.grpc.netty.NettyTestUtil.TrackingObjectPoolForTest;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFactory;
Expand All @@ -44,7 +44,7 @@ public class NettyChannelBuilderTest {

@Rule public final ExpectedException thrown = ExpectedException.none();
private final SslContext noSslContext = null;

private void shutdown(ManagedChannel mc) throws Exception {
mc.shutdownNow();
assertTrue(mc.awaitTermination(1, TimeUnit.SECONDS));
Expand Down Expand Up @@ -92,14 +92,35 @@ private void overrideAuthorityIsReadableHelper(NettyChannelBuilder builder,
}

@Test
@SuppressWarnings("deprecation")
public void overrideAllowsInvalidAuthority() {
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){});
InternalNettyChannelBuilder.overrideAuthorityChecker(builder, new OverrideAuthorityChecker() {
@Override
public String checkAuthority(String authority) {
return authority;
}
});
InternalNettyChannelBuilder.overrideAuthorityChecker(builder,
new io.grpc.netty.InternalNettyChannelBuilder.OverrideAuthorityChecker() {
@Override
public String checkAuthority(String authority) {
return authority;
}
});
Object unused = builder.overrideAuthority("[invalidauthority")
.negotiationType(NegotiationType.PLAINTEXT)
.buildTransportFactory();
}

@Test
@SuppressWarnings("deprecation")
public void overrideFailsInvalidAuthority() {
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){});
InternalNettyChannelBuilder.overrideAuthorityChecker(builder,
new io.grpc.netty.InternalNettyChannelBuilder.OverrideAuthorityChecker() {
@Override
public String checkAuthority(String authority) {
return GrpcUtil.checkAuthority(authority);
}
});

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid authority:");
Object unused = builder.overrideAuthority("[invalidauthority")
.negotiationType(NegotiationType.PLAINTEXT)
.buildTransportFactory();
Expand All @@ -115,6 +136,27 @@ public void failOverrideInvalidAuthority() {
builder.overrideAuthority("[invalidauthority");
}

@Test
public void disableCheckAuthorityAllowsInvalidAuthority() {
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){})
.disableCheckAuthority();

Object unused = builder.overrideAuthority("[invalidauthority")
.negotiationType(NegotiationType.PLAINTEXT)
.buildTransportFactory();
}

@Test
public void enableCheckAuthorityFailOverrideInvalidAuthority() {
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){})
.disableCheckAuthority()
.enableCheckAuthority();

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid authority:");
builder.overrideAuthority("[invalidauthority");
}

@Test
public void failInvalidAuthority() {
thrown.expect(IllegalArgumentException.class);
Expand Down

0 comments on commit 2f1107e

Please sign in to comment.