2828import io .grpc .ChannelLogger ;
2929import io .grpc .EquivalentAddressGroup ;
3030import io .grpc .ExperimentalApi ;
31+ import io .grpc .ForwardingChannelBuilder ;
3132import io .grpc .HttpConnectProxiedSocketAddress ;
3233import io .grpc .Internal ;
33- import io .grpc .internal . AbstractManagedChannelImplBuilder ;
34+ import io .grpc .ManagedChannelBuilder ;
3435import io .grpc .internal .AtomicBackoff ;
3536import io .grpc .internal .ClientTransportFactory ;
3637import io .grpc .internal .ConnectionClientTransport ;
3738import io .grpc .internal .FixedObjectPool ;
3839import io .grpc .internal .GrpcUtil ;
3940import io .grpc .internal .KeepAliveManager ;
41+ import io .grpc .internal .ManagedChannelImplBuilder ;
42+ import io .grpc .internal .ManagedChannelImplBuilder .ChannelBuilderDefaultPortProvider ;
43+ import io .grpc .internal .ManagedChannelImplBuilder .ClientTransportFactoryBuilder ;
4044import io .grpc .internal .ObjectPool ;
4145import io .grpc .internal .SharedResourcePool ;
4246import io .grpc .internal .TransportTracer ;
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
0 commit comments