Skip to content

Commit 2f1107e

Browse files
committed
netty: NettyChannelBuilder extends a public API class
1 parent e7afdb3 commit 2f1107e

3 files changed

Lines changed: 151 additions & 50 deletions

File tree

netty/src/main/java/io/grpc/netty/InternalNettyChannelBuilder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,31 @@ public final class InternalNettyChannelBuilder {
3131
/**
3232
* Checks authority upon channel construction. The purpose of this interface is to raise the
3333
* visibility of {@link NettyChannelBuilder.OverrideAuthorityChecker}.
34+
* @deprecated To be removed, use {@link #disableCheckAuthority(NettyChannelBuilder builder)} to
35+
* disable authority check.
3436
*/
37+
@Deprecated
3538
public interface OverrideAuthorityChecker extends NettyChannelBuilder.OverrideAuthorityChecker {}
3639

40+
/**
41+
* Overrides authority checker.
42+
* @deprecated To be removed, use {@link #disableCheckAuthority(NettyChannelBuilder builder)} to
43+
* disable authority check.
44+
*/
45+
@Deprecated
3746
public static void overrideAuthorityChecker(
3847
NettyChannelBuilder channelBuilder, OverrideAuthorityChecker authorityChecker) {
3948
channelBuilder.overrideAuthorityChecker(authorityChecker);
4049
}
4150

51+
public static void disableCheckAuthority(NettyChannelBuilder builder) {
52+
builder.disableCheckAuthority();
53+
}
54+
55+
public static void enableCheckAuthority(NettyChannelBuilder builder) {
56+
builder.enableCheckAuthority();
57+
}
58+
4259
/** A class that provides a Netty handler to control protocol negotiation. */
4360
public interface ProtocolNegotiatorFactory
4461
extends NettyChannelBuilder.ProtocolNegotiatorFactory {
@@ -68,6 +85,10 @@ public static void setStatsRecordStartedRpcs(NettyChannelBuilder builder, boolea
6885
builder.setStatsRecordStartedRpcs(value);
6986
}
7087

88+
public static void setStatsRecordFinishedRpcs(NettyChannelBuilder builder, boolean value) {
89+
builder.setStatsRecordFinishedRpcs(value);
90+
}
91+
7192
public static void setStatsRecordRealTimeMetrics(NettyChannelBuilder builder, boolean value) {
7293
builder.setStatsRecordRealTimeMetrics(value);
7394
}

netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@
2828
import io.grpc.ChannelLogger;
2929
import io.grpc.EquivalentAddressGroup;
3030
import io.grpc.ExperimentalApi;
31+
import io.grpc.ForwardingChannelBuilder;
3132
import io.grpc.HttpConnectProxiedSocketAddress;
3233
import io.grpc.Internal;
33-
import io.grpc.internal.AbstractManagedChannelImplBuilder;
34+
import io.grpc.ManagedChannelBuilder;
3435
import io.grpc.internal.AtomicBackoff;
3536
import io.grpc.internal.ClientTransportFactory;
3637
import io.grpc.internal.ConnectionClientTransport;
3738
import io.grpc.internal.FixedObjectPool;
3839
import io.grpc.internal.GrpcUtil;
3940
import io.grpc.internal.KeepAliveManager;
41+
import io.grpc.internal.ManagedChannelImplBuilder;
42+
import io.grpc.internal.ManagedChannelImplBuilder.ChannelBuilderDefaultPortProvider;
43+
import io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder;
4044
import io.grpc.internal.ObjectPool;
4145
import io.grpc.internal.SharedResourcePool;
4246
import io.grpc.internal.TransportTracer;
@@ -63,8 +67,7 @@
6367
*/
6468
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1784")
6569
@CanIgnoreReturnValue
66-
public final class NettyChannelBuilder
67-
extends AbstractManagedChannelImplBuilder<NettyChannelBuilder> {
70+
public final class NettyChannelBuilder extends ForwardingChannelBuilder<NettyChannelBuilder> {
6871

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

88-
private final Map<ChannelOption<?>, Object> channelOptions =
89-
new HashMap<>();
90-
91+
private final ManagedChannelImplBuilder managedChannelImplBuilder;
92+
private TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
93+
private final Map<ChannelOption<?>, Object> channelOptions = new HashMap<>();
9194
private NegotiationType negotiationType = NegotiationType.TLS;
92-
private OverrideAuthorityChecker authorityChecker;
9395
private ChannelFactory<? extends Channel> channelFactory = DEFAULT_CHANNEL_FACTORY;
9496
private ObjectPool<? extends EventLoopGroup> eventLoopGroupPool = DEFAULT_EVENT_LOOP_GROUP_POOL;
9597
private SslContext sslContext;
9698
private boolean autoFlowControl = DEFAULT_AUTO_FLOW_CONTROL;
9799
private int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW;
100+
private int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
98101
private int maxHeaderListSize = GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE;
99102
private long keepAliveTimeNanos = KEEPALIVE_TIME_NANOS_DISABLED;
100103
private long keepAliveTimeoutNanos = DEFAULT_KEEPALIVE_TIMEOUT_NANOS;
@@ -142,14 +145,41 @@ public static NettyChannelBuilder forTarget(String target) {
142145
this(GrpcUtil.authorityFromHostAndPort(host, port));
143146
}
144147

148+
private final class NettyChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {
149+
@Override
150+
public ClientTransportFactory buildClientTransportFactory() {
151+
return buildTransportFactory();
152+
}
153+
}
154+
155+
private final class NettyChannelDefaultPortProvider implements ChannelBuilderDefaultPortProvider {
156+
@Override
157+
public int getDefaultPort() {
158+
return NettyChannelBuilder.this.getDefaultPort();
159+
}
160+
}
161+
145162
@CheckReturnValue
146163
NettyChannelBuilder(String target) {
147-
super(target);
164+
super();
165+
managedChannelImplBuilder = new ManagedChannelImplBuilder(target,
166+
new NettyChannelTransportFactoryBuilder(),
167+
new NettyChannelDefaultPortProvider());
148168
}
149169

150170
@CheckReturnValue
151171
NettyChannelBuilder(SocketAddress address) {
152-
super(address, getAuthorityFromAddress(address));
172+
super();
173+
managedChannelImplBuilder = new ManagedChannelImplBuilder(address,
174+
getAuthorityFromAddress(address),
175+
new NettyChannelTransportFactoryBuilder(),
176+
new NettyChannelDefaultPortProvider());
177+
}
178+
179+
@Internal
180+
@Override
181+
protected ManagedChannelBuilder<?> delegate() {
182+
return managedChannelImplBuilder;
153183
}
154184

155185
@CheckReturnValue
@@ -408,10 +438,20 @@ public SocketAddress createSocketAddress(
408438
}
409439
}
410440

441+
/**
442+
* Sets the maximum message size allowed for a single gRPC frame. If an inbound messages larger
443+
* than this limit is received it will not be processed and the RPC will fail with
444+
* RESOURCE_EXHAUSTED.
445+
*/
411446
@Override
447+
public NettyChannelBuilder maxInboundMessageSize(int max) {
448+
checkArgument(max >= 0, "negative max");
449+
maxInboundMessageSize = max;
450+
return this;
451+
}
452+
412453
@CheckReturnValue
413-
@Internal
414-
protected ClientTransportFactory buildTransportFactory() {
454+
final ClientTransportFactory buildTransportFactory() {
415455
assertEventLoopAndChannelType();
416456

417457
ProtocolNegotiator negotiator;
@@ -427,12 +467,12 @@ protected ClientTransportFactory buildTransportFactory() {
427467
}
428468
}
429469
negotiator = createProtocolNegotiatorByType(negotiationType, localSslContext,
430-
this.getOffloadExecutorPool());
470+
this.managedChannelImplBuilder.getOffloadExecutorPool());
431471
}
432472

433473
return new NettyTransportFactory(
434474
negotiator, channelFactory, channelOptions,
435-
eventLoopGroupPool, autoFlowControl, flowControlWindow, maxInboundMessageSize(),
475+
eventLoopGroupPool, autoFlowControl, flowControlWindow, maxInboundMessageSize,
436476
maxHeaderListSize, keepAliveTimeNanos, keepAliveTimeoutNanos, keepAliveWithoutCalls,
437477
transportTracerFactory, localSocketPicker, useGetForSafeMethods);
438478
}
@@ -448,9 +488,8 @@ void assertEventLoopAndChannelType() {
448488
"Both EventLoopGroup and ChannelType should be provided or neither should be");
449489
}
450490

451-
@Override
452491
@CheckReturnValue
453-
protected int getDefaultPort() {
492+
int getDefaultPort() {
454493
switch (negotiationType) {
455494
case PLAINTEXT:
456495
case PLAINTEXT_UPGRADE:
@@ -462,10 +501,6 @@ protected int getDefaultPort() {
462501
}
463502
}
464503

465-
void overrideAuthorityChecker(@Nullable OverrideAuthorityChecker authorityChecker) {
466-
this.authorityChecker = authorityChecker;
467-
}
468-
469504
@VisibleForTesting
470505
@CheckReturnValue
471506
static ProtocolNegotiator createProtocolNegotiatorByType(
@@ -484,44 +519,47 @@ static ProtocolNegotiator createProtocolNegotiatorByType(
484519
}
485520
}
486521

487-
@CheckReturnValue
488-
interface OverrideAuthorityChecker {
489-
String checkAuthority(String authority);
522+
@Deprecated
523+
interface OverrideAuthorityChecker extends ManagedChannelImplBuilder.OverrideAuthorityChecker {}
524+
525+
@Deprecated
526+
void overrideAuthorityChecker(@Nullable OverrideAuthorityChecker authorityChecker) {
527+
this.managedChannelImplBuilder.overrideAuthorityChecker(authorityChecker);
490528
}
491529

492-
@Override
493-
@CheckReturnValue
494-
@Internal
495-
protected String checkAuthority(String authority) {
496-
if (authorityChecker != null) {
497-
return authorityChecker.checkAuthority(authority);
498-
}
499-
return super.checkAuthority(authority);
530+
NettyChannelBuilder disableCheckAuthority() {
531+
this.managedChannelImplBuilder.disableCheckAuthority();
532+
return this;
533+
}
534+
535+
NettyChannelBuilder enableCheckAuthority() {
536+
this.managedChannelImplBuilder.enableCheckAuthority();
537+
return this;
500538
}
501539

502540
void protocolNegotiatorFactory(ProtocolNegotiatorFactory protocolNegotiatorFactory) {
503541
this.protocolNegotiatorFactory
504542
= checkNotNull(protocolNegotiatorFactory, "protocolNegotiatorFactory");
505543
}
506544

507-
@Override
508-
protected void setTracingEnabled(boolean value) {
509-
super.setTracingEnabled(value);
545+
void setTracingEnabled(boolean value) {
546+
this.managedChannelImplBuilder.setTracingEnabled(value);
510547
}
511548

512-
@Override
513-
protected void setStatsEnabled(boolean value) {
514-
super.setStatsEnabled(value);
549+
void setStatsEnabled(boolean value) {
550+
this.managedChannelImplBuilder.setStatsEnabled(value);
515551
}
516552

517-
@Override
518-
protected void setStatsRecordStartedRpcs(boolean value) {
519-
super.setStatsRecordStartedRpcs(value);
553+
void setStatsRecordStartedRpcs(boolean value) {
554+
this.managedChannelImplBuilder.setStatsRecordStartedRpcs(value);
520555
}
521556

522-
@Override
523-
protected void setStatsRecordRealTimeMetrics(boolean value) {
524-
super.setStatsRecordRealTimeMetrics(value);
557+
void setStatsRecordFinishedRpcs(boolean value) {
558+
this.managedChannelImplBuilder.setStatsRecordFinishedRpcs(value);
559+
}
560+
561+
void setStatsRecordRealTimeMetrics(boolean value) {
562+
this.managedChannelImplBuilder.setStatsRecordRealTimeMetrics(value);
525563
}
526564

527565
@VisibleForTesting

netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.mockito.Mockito.mock;
2323

2424
import io.grpc.ManagedChannel;
25-
import io.grpc.netty.InternalNettyChannelBuilder.OverrideAuthorityChecker;
25+
import io.grpc.internal.GrpcUtil;
2626
import io.grpc.netty.NettyTestUtil.TrackingObjectPoolForTest;
2727
import io.netty.channel.Channel;
2828
import io.netty.channel.ChannelFactory;
@@ -44,7 +44,7 @@ public class NettyChannelBuilderTest {
4444

4545
@Rule public final ExpectedException thrown = ExpectedException.none();
4646
private final SslContext noSslContext = null;
47-
47+
4848
private void shutdown(ManagedChannel mc) throws Exception {
4949
mc.shutdownNow();
5050
assertTrue(mc.awaitTermination(1, TimeUnit.SECONDS));
@@ -92,14 +92,35 @@ private void overrideAuthorityIsReadableHelper(NettyChannelBuilder builder,
9292
}
9393

9494
@Test
95+
@SuppressWarnings("deprecation")
9596
public void overrideAllowsInvalidAuthority() {
9697
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){});
97-
InternalNettyChannelBuilder.overrideAuthorityChecker(builder, new OverrideAuthorityChecker() {
98-
@Override
99-
public String checkAuthority(String authority) {
100-
return authority;
101-
}
102-
});
98+
InternalNettyChannelBuilder.overrideAuthorityChecker(builder,
99+
new io.grpc.netty.InternalNettyChannelBuilder.OverrideAuthorityChecker() {
100+
@Override
101+
public String checkAuthority(String authority) {
102+
return authority;
103+
}
104+
});
105+
Object unused = builder.overrideAuthority("[invalidauthority")
106+
.negotiationType(NegotiationType.PLAINTEXT)
107+
.buildTransportFactory();
108+
}
109+
110+
@Test
111+
@SuppressWarnings("deprecation")
112+
public void overrideFailsInvalidAuthority() {
113+
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){});
114+
InternalNettyChannelBuilder.overrideAuthorityChecker(builder,
115+
new io.grpc.netty.InternalNettyChannelBuilder.OverrideAuthorityChecker() {
116+
@Override
117+
public String checkAuthority(String authority) {
118+
return GrpcUtil.checkAuthority(authority);
119+
}
120+
});
121+
122+
thrown.expect(IllegalArgumentException.class);
123+
thrown.expectMessage("Invalid authority:");
103124
Object unused = builder.overrideAuthority("[invalidauthority")
104125
.negotiationType(NegotiationType.PLAINTEXT)
105126
.buildTransportFactory();
@@ -115,6 +136,27 @@ public void failOverrideInvalidAuthority() {
115136
builder.overrideAuthority("[invalidauthority");
116137
}
117138

139+
@Test
140+
public void disableCheckAuthorityAllowsInvalidAuthority() {
141+
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){})
142+
.disableCheckAuthority();
143+
144+
Object unused = builder.overrideAuthority("[invalidauthority")
145+
.negotiationType(NegotiationType.PLAINTEXT)
146+
.buildTransportFactory();
147+
}
148+
149+
@Test
150+
public void enableCheckAuthorityFailOverrideInvalidAuthority() {
151+
NettyChannelBuilder builder = new NettyChannelBuilder(new SocketAddress(){})
152+
.disableCheckAuthority()
153+
.enableCheckAuthority();
154+
155+
thrown.expect(IllegalArgumentException.class);
156+
thrown.expectMessage("Invalid authority:");
157+
builder.overrideAuthority("[invalidauthority");
158+
}
159+
118160
@Test
119161
public void failInvalidAuthority() {
120162
thrown.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)