24
24
import com .google .common .base .Preconditions ;
25
25
import io .grpc .ChannelLogger ;
26
26
import io .grpc .ExperimentalApi ;
27
+ import io .grpc .ForwardingChannelBuilder ;
27
28
import io .grpc .Internal ;
28
- import io .grpc .internal . AbstractManagedChannelImplBuilder ;
29
+ import io .grpc .ManagedChannelBuilder ;
29
30
import io .grpc .internal .AtomicBackoff ;
30
31
import io .grpc .internal .ClientTransportFactory ;
31
32
import io .grpc .internal .ConnectionClientTransport ;
32
33
import io .grpc .internal .GrpcUtil ;
33
34
import io .grpc .internal .KeepAliveManager ;
35
+ import io .grpc .internal .ManagedChannelImplBuilder ;
36
+ import io .grpc .internal .ManagedChannelImplBuilder .ChannelBuilderDefaultPortProvider ;
37
+ import io .grpc .internal .ManagedChannelImplBuilder .ClientTransportFactoryBuilder ;
34
38
import io .grpc .internal .SharedResourceHolder ;
35
39
import io .grpc .internal .SharedResourceHolder .Resource ;
36
40
import io .grpc .internal .TransportTracer ;
54
58
55
59
/** Convenience class for building channels with the OkHttp transport. */
56
60
@ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/1785" )
57
- public class OkHttpChannelBuilder extends
58
- AbstractManagedChannelImplBuilder <OkHttpChannelBuilder > {
61
+ public class OkHttpChannelBuilder extends ForwardingChannelBuilder <OkHttpChannelBuilder > {
59
62
60
63
public static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535 ;
64
+ private final ManagedChannelImplBuilder managedChannelImplBuilder ;
65
+ private TransportTracer .Factory transportTracerFactory = TransportTracer .getDefaultFactory ();
66
+
61
67
62
68
/** Identifies the negotiation used for starting up HTTP/2. */
63
69
private enum NegotiationType {
@@ -127,6 +133,7 @@ public static OkHttpChannelBuilder forTarget(String target) {
127
133
private long keepAliveTimeoutNanos = DEFAULT_KEEPALIVE_TIMEOUT_NANOS ;
128
134
private int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW ;
129
135
private boolean keepAliveWithoutCalls ;
136
+ private int maxInboundMessageSize = GrpcUtil .DEFAULT_MAX_MESSAGE_SIZE ;
130
137
private int maxInboundMetadataSize = Integer .MAX_VALUE ;
131
138
132
139
/**
@@ -140,7 +147,31 @@ protected OkHttpChannelBuilder(String host, int port) {
140
147
}
141
148
142
149
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 ;
144
175
}
145
176
146
177
@ VisibleForTesting
@@ -363,9 +394,19 @@ public OkHttpChannelBuilder maxInboundMetadataSize(int bytes) {
363
394
return this ;
364
395
}
365
396
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
+ */
366
402
@ 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 () {
369
410
boolean enableKeepAlive = keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED ;
370
411
return new OkHttpTransportFactory (
371
412
transportExecutor ,
@@ -374,7 +415,7 @@ protected final ClientTransportFactory buildTransportFactory() {
374
415
createSslSocketFactory (),
375
416
hostnameVerifier ,
376
417
connectionSpec ,
377
- maxInboundMessageSize () ,
418
+ maxInboundMessageSize ,
378
419
enableKeepAlive ,
379
420
keepAliveTimeNanos ,
380
421
keepAliveTimeoutNanos ,
@@ -385,8 +426,17 @@ protected final ClientTransportFactory buildTransportFactory() {
385
426
useGetForSafeMethods );
386
427
}
387
428
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 () {
390
440
switch (negotiationType ) {
391
441
case PLAINTEXT :
392
442
return GrpcUtil .DEFAULT_PORT_PLAINTEXT ;
@@ -397,6 +447,10 @@ protected int getDefaultPort() {
397
447
}
398
448
}
399
449
450
+ void setStatsEnabled (boolean value ) {
451
+ this .managedChannelImplBuilder .setStatsEnabled (value );
452
+ }
453
+
400
454
@ VisibleForTesting
401
455
@ Nullable
402
456
SSLSocketFactory createSslSocketFactory () {
0 commit comments