Skip to content

Commit 86d1438

Browse files
committed
okhttp: OkHttpChannelBuilder extends a public API class
1 parent 2f1107e commit 86d1438

File tree

3 files changed

+111
-18
lines changed

3 files changed

+111
-18
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.okhttp;
18+
19+
import io.grpc.Internal;
20+
21+
/**
22+
* Internal {@link OkHttpChannelBuilder} accessor. This is intended for usage internal to the gRPC
23+
* team. If you *really* think you need to use this, contact the gRPC team first.
24+
*/
25+
@Internal
26+
public final class InternalOkHttpChannelBuilder {
27+
28+
public static void setStatsEnabled(OkHttpChannelBuilder builder, boolean value) {
29+
builder.setStatsEnabled(value);
30+
}
31+
32+
private InternalOkHttpChannelBuilder() {}
33+
}

okhttp/src/main/java/io/grpc/okhttp/OkHttpChannelBuilder.java

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
import com.google.common.base.Preconditions;
2525
import io.grpc.ChannelLogger;
2626
import io.grpc.ExperimentalApi;
27+
import io.grpc.ForwardingChannelBuilder;
2728
import io.grpc.Internal;
28-
import io.grpc.internal.AbstractManagedChannelImplBuilder;
29+
import io.grpc.ManagedChannelBuilder;
2930
import io.grpc.internal.AtomicBackoff;
3031
import io.grpc.internal.ClientTransportFactory;
3132
import io.grpc.internal.ConnectionClientTransport;
3233
import io.grpc.internal.GrpcUtil;
3334
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;
3438
import io.grpc.internal.SharedResourceHolder;
3539
import io.grpc.internal.SharedResourceHolder.Resource;
3640
import io.grpc.internal.TransportTracer;
@@ -54,10 +58,12 @@
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() {

okhttp/src/test/java/io/grpc/okhttp/OkHttpChannelBuilderTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,26 @@ private void overrideAuthorityIsReadableHelper(OkHttpChannelBuilder builder,
7272
}
7373

7474
@Test
75-
public void overrideAllowsInvalidAuthority() {
76-
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234) {
77-
@Override
78-
protected String checkAuthority(String authority) {
79-
return authority;
80-
}
81-
};
75+
public void failOverrideInvalidAuthority() {
76+
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234);
77+
78+
thrown.expect(IllegalArgumentException.class);
79+
thrown.expectMessage("Invalid authority:");
80+
builder.overrideAuthority("[invalidauthority");
81+
}
8282

83+
@Test
84+
public void disableCheckAuthorityAllowsInvalidAuthority() {
85+
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234)
86+
.disableCheckAuthority();
8387
builder.overrideAuthority("[invalidauthority").usePlaintext().buildTransportFactory();
8488
}
8589

8690
@Test
87-
public void failOverrideInvalidAuthority() {
88-
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234);
91+
public void enableCheckAuthorityFailOverrideInvalidAuthority() {
92+
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234)
93+
.disableCheckAuthority()
94+
.enableCheckAuthority();
8995

9096
thrown.expect(IllegalArgumentException.class);
9197
thrown.expectMessage("Invalid authority:");

0 commit comments

Comments
 (0)