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 Sep 2, 2020
1 parent b0f0ed0 commit c056979
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.okhttp.OkHttpChannelBuilder;
Expand Down Expand Up @@ -425,7 +426,9 @@ protected ManagedChannelBuilder<?> createChannelBuilder() {
if (fullStreamDecompression) {
nettyBuilder.enableFullStreamDecompression();
}
builder = nettyBuilder;
// Disable the default census stats interceptor, use testing interceptor instead.
InternalNettyChannelBuilder.setStatsEnabled(nettyBuilder, false);
return nettyBuilder.intercept(createCensusStatsClientInterceptor());
} else {
OkHttpChannelBuilder okBuilder = OkHttpChannelBuilder.forAddress(serverHost, serverPort);
if (serverHostOverride != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.testing.integration;

import io.grpc.internal.AbstractServerImplBuilder;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
Expand All @@ -39,7 +40,7 @@ protected NettyChannelBuilder createChannelBuilder() {
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.initialFlowControlWindow(NettyChannelBuilder.DEFAULT_FLOW_CONTROL_WINDOW);
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
InternalNettyChannelBuilder.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.testing.integration;

import io.grpc.internal.AbstractServerImplBuilder;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
Expand Down Expand Up @@ -57,7 +58,7 @@ protected NettyChannelBuilder createChannelBuilder() {
.flowControlWindow(65 * 1024)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
InternalNettyChannelBuilder.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.grpc.internal.AbstractServerImplBuilder;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.netty.handler.ssl.ClientAuth;
Expand Down Expand Up @@ -71,7 +72,7 @@ protected NettyChannelBuilder createChannelBuilder() {
.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE)
.build());
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
InternalNettyChannelBuilder.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor());
} catch (Exception ex) {
throw new RuntimeException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.grpc.ServerInterceptor;
import io.grpc.internal.AbstractServerImplBuilder;
import io.grpc.internal.GrpcUtil;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.testing.integration.Messages.BoolValue;
Expand Down Expand Up @@ -165,7 +166,7 @@ public void onHeaders(Metadata headers) {
})
.usePlaintext();
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
InternalNettyChannelBuilder.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor());
}

Expand Down
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
120 changes: 78 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,39 @@ 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);
managedChannelImplBuilder = new ManagedChannelImplBuilder(target,
new NettyChannelTransportFactoryBuilder(),
new NettyChannelDefaultPortProvider());
}

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

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

@CheckReturnValue
Expand Down Expand Up @@ -408,10 +436,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() {
ClientTransportFactory buildTransportFactory() {
assertEventLoopAndChannelType();

ProtocolNegotiator negotiator;
Expand All @@ -427,12 +465,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 +486,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 +499,6 @@ protected int getDefaultPort() {
}
}

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

@VisibleForTesting
@CheckReturnValue
static ProtocolNegotiator createProtocolNegotiatorByType(
Expand All @@ -484,44 +517,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
Loading

0 comments on commit c056979

Please sign in to comment.