2424import com .google .common .base .Preconditions ;
2525import io .grpc .ChannelLogger ;
2626import io .grpc .ExperimentalApi ;
27+ import io .grpc .ForwardingChannelBuilder ;
2728import io .grpc .Internal ;
28- import io .grpc .internal . AbstractManagedChannelImplBuilder ;
29+ import io .grpc .ManagedChannelBuilder ;
2930import io .grpc .internal .AtomicBackoff ;
3031import io .grpc .internal .ClientTransportFactory ;
3132import io .grpc .internal .ConnectionClientTransport ;
3233import io .grpc .internal .GrpcUtil ;
3334import io .grpc .internal .KeepAliveManager ;
35+ import io .grpc .internal .ManagedChannelImplBuilder ;
36+ import io .grpc .internal .ManagedChannelImplBuilder .ChannelBuilderDefaultPortProvider ;
37+ import io .grpc .internal .ManagedChannelImplBuilder .ClientTransportFactoryBuilder ;
3438import io .grpc .internal .SharedResourceHolder ;
3539import io .grpc .internal .SharedResourceHolder .Resource ;
3640import io .grpc .internal .TransportTracer ;
5458
5559/** Convenience class for building channels with the OkHttp transport. */
5660@ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/1785" )
57- public class OkHttpChannelBuilder extends
58- AbstractManagedChannelImplBuilder <OkHttpChannelBuilder > {
61+ public class OkHttpChannelBuilder extends ForwardingChannelBuilder <OkHttpChannelBuilder > {
5962
6063 public static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535 ;
64+ private final ManagedChannelImplBuilder managedChannelImplBuilder ;
65+ private TransportTracer .Factory transportTracerFactory = TransportTracer .getDefaultFactory ();
66+
6167
6268 /** Identifies the negotiation used for starting up HTTP/2. */
6369 private enum NegotiationType {
@@ -127,6 +133,7 @@ public static OkHttpChannelBuilder forTarget(String target) {
127133 private long keepAliveTimeoutNanos = DEFAULT_KEEPALIVE_TIMEOUT_NANOS ;
128134 private int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW ;
129135 private boolean keepAliveWithoutCalls ;
136+ private int maxInboundMessageSize = GrpcUtil .DEFAULT_MAX_MESSAGE_SIZE ;
130137 private int maxInboundMetadataSize = Integer .MAX_VALUE ;
131138
132139 /**
@@ -140,7 +147,31 @@ protected OkHttpChannelBuilder(String host, int port) {
140147 }
141148
142149 private OkHttpChannelBuilder (String target ) {
143- super (target );
150+ super ();
151+
152+ final class OkHttpChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {
153+ @ Override
154+ public ClientTransportFactory buildClientTransportFactory () {
155+ return buildTransportFactory ();
156+ }
157+ }
158+
159+ final class OkHttpChannelDefaultPortProvider implements ChannelBuilderDefaultPortProvider {
160+ @ Override
161+ public int getDefaultPort () {
162+ return OkHttpChannelBuilder .this .getDefaultPort ();
163+ }
164+ }
165+
166+ managedChannelImplBuilder = new ManagedChannelImplBuilder (target ,
167+ new OkHttpChannelTransportFactoryBuilder (),
168+ new OkHttpChannelDefaultPortProvider ());
169+ }
170+
171+ @ Internal
172+ @ Override
173+ protected final ManagedChannelBuilder <?> delegate () {
174+ return managedChannelImplBuilder ;
144175 }
145176
146177 @ VisibleForTesting
@@ -363,9 +394,19 @@ public OkHttpChannelBuilder maxInboundMetadataSize(int bytes) {
363394 return this ;
364395 }
365396
397+ /**
398+ * Sets the maximum message size allowed for a single gRPC frame. If an inbound messages
399+ * larger than this limit is received it will not be processed and the RPC will fail with
400+ * RESOURCE_EXHAUSTED.
401+ */
366402 @ Override
367- @ Internal
368- protected final ClientTransportFactory buildTransportFactory () {
403+ public OkHttpChannelBuilder maxInboundMessageSize (int max ) {
404+ Preconditions .checkArgument (max >= 0 , "negative max" );
405+ maxInboundMessageSize = max ;
406+ return this ;
407+ }
408+
409+ final ClientTransportFactory buildTransportFactory () {
369410 boolean enableKeepAlive = keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED ;
370411 return new OkHttpTransportFactory (
371412 transportExecutor ,
@@ -374,7 +415,7 @@ protected final ClientTransportFactory buildTransportFactory() {
374415 createSslSocketFactory (),
375416 hostnameVerifier ,
376417 connectionSpec ,
377- maxInboundMessageSize () ,
418+ maxInboundMessageSize ,
378419 enableKeepAlive ,
379420 keepAliveTimeNanos ,
380421 keepAliveTimeoutNanos ,
@@ -385,8 +426,17 @@ protected final ClientTransportFactory buildTransportFactory() {
385426 useGetForSafeMethods );
386427 }
387428
388- @ Override
389- protected int getDefaultPort () {
429+ OkHttpChannelBuilder disableCheckAuthority () {
430+ this .managedChannelImplBuilder .disableCheckAuthority ();
431+ return this ;
432+ }
433+
434+ OkHttpChannelBuilder enableCheckAuthority () {
435+ this .managedChannelImplBuilder .enableCheckAuthority ();
436+ return this ;
437+ }
438+
439+ int getDefaultPort () {
390440 switch (negotiationType ) {
391441 case PLAINTEXT :
392442 return GrpcUtil .DEFAULT_PORT_PLAINTEXT ;
@@ -397,6 +447,10 @@ protected int getDefaultPort() {
397447 }
398448 }
399449
450+ void setStatsEnabled (boolean value ) {
451+ this .managedChannelImplBuilder .setStatsEnabled (value );
452+ }
453+
400454 @ VisibleForTesting
401455 @ Nullable
402456 SSLSocketFactory createSslSocketFactory () {
0 commit comments