cronet: move call options APIs into an internal accessor class (#6073)

* cronet: move call options APIs into an internal accessor class

* Removed deprecated call option key.
This commit is contained in:
Chengyuan Zhang 2019-08-16 01:05:32 -07:00 committed by GitHub
parent 4c1e197fcd
commit e68e4004e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 20 deletions

View File

@ -17,9 +17,6 @@
package io.grpc.cronet;
import io.grpc.CallOptions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/** Call options for use with the Cronet transport. */
public final class CronetCallOptions {
@ -36,7 +33,7 @@ public final class CronetCallOptions {
*/
@Deprecated
public static final CallOptions.Key<Object> CRONET_ANNOTATION_KEY =
CallOptions.Key.create("cronet-annotation");
CronetClientStream.CRONET_ANNOTATION_KEY;
/**
* Returns a copy of {@code callOptions} with {@code annotation} included as one of the Cronet
@ -48,18 +45,6 @@ public final class CronetCallOptions {
* @param annotation the object to attach to the Cronet stream
*/
public static CallOptions withAnnotation(CallOptions callOptions, Object annotation) {
Collection<Object> existingAnnotations = callOptions.getOption(CRONET_ANNOTATIONS_KEY);
ArrayList<Object> newAnnotations;
if (existingAnnotations == null) {
newAnnotations = new ArrayList<>();
} else {
newAnnotations = new ArrayList<>(existingAnnotations);
}
newAnnotations.add(annotation);
return callOptions.withOption(
CronetCallOptions.CRONET_ANNOTATIONS_KEY, Collections.unmodifiableList(newAnnotations));
return CronetClientStream.withAnnotation(callOptions, annotation);
}
static final CallOptions.Key<Collection<Object>> CRONET_ANNOTATIONS_KEY =
CallOptions.Key.create("cronet-annotations");
}

View File

@ -44,6 +44,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -63,6 +64,14 @@ class CronetClientStream extends AbstractClientStream {
private static final int READ_BUFFER_CAPACITY = 4 * 1024;
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocateDirect(0);
private static final String LOG_TAG = "grpc-java-cronet";
@Deprecated
static final CallOptions.Key<Object> CRONET_ANNOTATION_KEY =
CallOptions.Key.create("cronet-annotation");
static final CallOptions.Key<Collection<Object>> CRONET_ANNOTATIONS_KEY =
CallOptions.Key.create("cronet-annotations");
private final String url;
private final String userAgent;
private final StatsTraceContext statsTraceCtx;
@ -107,8 +116,8 @@ class CronetClientStream extends AbstractClientStream {
this.idempotent = method.isIdempotent() || alwaysUsePut;
// Only delay flushing header for unary rpcs.
this.delayRequestHeader = (method.getType() == MethodDescriptor.MethodType.UNARY);
this.annotation = callOptions.getOption(CronetCallOptions.CRONET_ANNOTATION_KEY);
this.annotations = callOptions.getOption(CronetCallOptions.CRONET_ANNOTATIONS_KEY);
this.annotation = callOptions.getOption(CRONET_ANNOTATION_KEY);
this.annotations = callOptions.getOption(CRONET_ANNOTATIONS_KEY);
this.state = new TransportState(maxMessageSize, statsTraceCtx, lock, transportTracer);
}
@ -127,6 +136,28 @@ class CronetClientStream extends AbstractClientStream {
throw new UnsupportedOperationException("Cronet does not support overriding authority");
}
/**
* Returns a copy of {@code callOptions} with {@code annotation} included as one of the Cronet
* annotation objects. When an RPC is made using a {@link CallOptions} instance returned by this
* method, the annotation objects will be attached to the underlying Cronet bidirectional stream.
* When the stream finishes, the user can retrieve the annotation objects via {@link
* org.chromium.net.RequestFinishedInfo.Listener}.
*
* @param annotation the object to attach to the Cronet stream
*/
static CallOptions withAnnotation(CallOptions callOptions, Object annotation) {
Collection<Object> existingAnnotations = callOptions.getOption(CRONET_ANNOTATIONS_KEY);
ArrayList<Object> newAnnotations;
if (existingAnnotations == null) {
newAnnotations = new ArrayList<>();
} else {
newAnnotations = new ArrayList<>(existingAnnotations);
}
newAnnotations.add(annotation);
return callOptions.withOption(
CRONET_ANNOTATIONS_KEY, Collections.unmodifiableList(newAnnotations));
}
class Sink implements AbstractClientStream.Sink {
@Override
public void writeHeaders(Metadata metadata, byte[] payload) {

View File

@ -0,0 +1,36 @@
/*
* Copyright 2019 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.cronet;
import io.grpc.CallOptions;
import io.grpc.Internal;
/**
* Internal accessor class for call options using with the Cronet transport. 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 InternalCronetCallOptions {
// Prevent instantiation
private InternalCronetCallOptions() {}
public static CallOptions withAnnotation(CallOptions callOptions, Object annotation) {
return CronetClientStream.withAnnotation(callOptions, annotation);
}
}

View File

@ -587,7 +587,7 @@ public final class CronetClientStreamTest {
false /* alwaysUsePut */,
method,
StatsTraceContext.NOOP,
CallOptions.DEFAULT.withOption(CronetCallOptions.CRONET_ANNOTATION_KEY, annotation),
CallOptions.DEFAULT.withOption(CronetClientStream.CRONET_ANNOTATION_KEY, annotation),
transportTracer);
callback.setStream(stream);
when(factory.newBidirectionalStreamBuilder(