Skip to content

Commit

Permalink
okhttp: OkHttpChannelBuilder extends a public API class
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiitk committed Aug 25, 2020
1 parent 2f1107e commit 86d1438
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.okhttp;

import io.grpc.Internal;

/**
* Internal {@link OkHttpChannelBuilder} accessor. This is intended for usage internal to the gRPC
* team. If you *really* think you need to use this, contact the gRPC team first.
*/
@Internal
public final class InternalOkHttpChannelBuilder {

public static void setStatsEnabled(OkHttpChannelBuilder builder, boolean value) {
builder.setStatsEnabled(value);
}

private InternalOkHttpChannelBuilder() {}
}
72 changes: 63 additions & 9 deletions okhttp/src/main/java/io/grpc/okhttp/OkHttpChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
import com.google.common.base.Preconditions;
import io.grpc.ChannelLogger;
import io.grpc.ExperimentalApi;
import io.grpc.ForwardingChannelBuilder;
import io.grpc.Internal;
import io.grpc.internal.AbstractManagedChannelImplBuilder;
import io.grpc.ManagedChannelBuilder;
import io.grpc.internal.AtomicBackoff;
import io.grpc.internal.ClientTransportFactory;
import io.grpc.internal.ConnectionClientTransport;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.KeepAliveManager;
import io.grpc.internal.ManagedChannelImplBuilder;
import io.grpc.internal.ManagedChannelImplBuilder.ChannelBuilderDefaultPortProvider;
import io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder;
import io.grpc.internal.SharedResourceHolder;
import io.grpc.internal.SharedResourceHolder.Resource;
import io.grpc.internal.TransportTracer;
Expand All @@ -54,10 +58,12 @@

/** Convenience class for building channels with the OkHttp transport. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785")
public class OkHttpChannelBuilder extends
AbstractManagedChannelImplBuilder<OkHttpChannelBuilder> {
public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannelBuilder> {

public static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535;
private final ManagedChannelImplBuilder managedChannelImplBuilder;
private TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();


/** Identifies the negotiation used for starting up HTTP/2. */
private enum NegotiationType {
Expand Down Expand Up @@ -127,6 +133,7 @@ public static OkHttpChannelBuilder forTarget(String target) {
private long keepAliveTimeoutNanos = DEFAULT_KEEPALIVE_TIMEOUT_NANOS;
private int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW;
private boolean keepAliveWithoutCalls;
private int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
private int maxInboundMetadataSize = Integer.MAX_VALUE;

/**
Expand All @@ -140,7 +147,31 @@ protected OkHttpChannelBuilder(String host, int port) {
}

private OkHttpChannelBuilder(String target) {
super(target);
super();

final class OkHttpChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {
@Override
public ClientTransportFactory buildClientTransportFactory() {
return buildTransportFactory();
}
}

final class OkHttpChannelDefaultPortProvider implements ChannelBuilderDefaultPortProvider {
@Override
public int getDefaultPort() {
return OkHttpChannelBuilder.this.getDefaultPort();
}
}

managedChannelImplBuilder = new ManagedChannelImplBuilder(target,
new OkHttpChannelTransportFactoryBuilder(),
new OkHttpChannelDefaultPortProvider());
}

@Internal
@Override
protected final ManagedChannelBuilder<?> delegate() {
return managedChannelImplBuilder;
}

@VisibleForTesting
Expand Down Expand Up @@ -363,9 +394,19 @@ public OkHttpChannelBuilder maxInboundMetadataSize(int bytes) {
return this;
}

/**
* Sets the maximum message size allowed for a single gRPC frame. If an inbound messages
* larger than this limit is received it will not be processed and the RPC will fail with
* RESOURCE_EXHAUSTED.
*/
@Override
@Internal
protected final ClientTransportFactory buildTransportFactory() {
public OkHttpChannelBuilder maxInboundMessageSize(int max) {
Preconditions.checkArgument(max >= 0, "negative max");
maxInboundMessageSize = max;
return this;
}

final ClientTransportFactory buildTransportFactory() {
boolean enableKeepAlive = keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED;
return new OkHttpTransportFactory(
transportExecutor,
Expand All @@ -374,7 +415,7 @@ protected final ClientTransportFactory buildTransportFactory() {
createSslSocketFactory(),
hostnameVerifier,
connectionSpec,
maxInboundMessageSize(),
maxInboundMessageSize,
enableKeepAlive,
keepAliveTimeNanos,
keepAliveTimeoutNanos,
Expand All @@ -385,8 +426,17 @@ protected final ClientTransportFactory buildTransportFactory() {
useGetForSafeMethods);
}

@Override
protected int getDefaultPort() {
OkHttpChannelBuilder disableCheckAuthority() {
this.managedChannelImplBuilder.disableCheckAuthority();
return this;
}

OkHttpChannelBuilder enableCheckAuthority() {
this.managedChannelImplBuilder.enableCheckAuthority();
return this;
}

int getDefaultPort() {
switch (negotiationType) {
case PLAINTEXT:
return GrpcUtil.DEFAULT_PORT_PLAINTEXT;
Expand All @@ -397,6 +447,10 @@ protected int getDefaultPort() {
}
}

void setStatsEnabled(boolean value) {
this.managedChannelImplBuilder.setStatsEnabled(value);
}

@VisibleForTesting
@Nullable
SSLSocketFactory createSslSocketFactory() {
Expand Down
24 changes: 15 additions & 9 deletions okhttp/src/test/java/io/grpc/okhttp/OkHttpChannelBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,26 @@ private void overrideAuthorityIsReadableHelper(OkHttpChannelBuilder builder,
}

@Test
public void overrideAllowsInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234) {
@Override
protected String checkAuthority(String authority) {
return authority;
}
};
public void failOverrideInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234);

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid authority:");
builder.overrideAuthority("[invalidauthority");
}

@Test
public void disableCheckAuthorityAllowsInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234)
.disableCheckAuthority();
builder.overrideAuthority("[invalidauthority").usePlaintext().buildTransportFactory();
}

@Test
public void failOverrideInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234);
public void enableCheckAuthorityFailOverrideInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234)
.disableCheckAuthority()
.enableCheckAuthority();

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid authority:");
Expand Down

0 comments on commit 86d1438

Please sign in to comment.