1 /* 2 * Copyright 2018, gRPC Authors All rights reserved. 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.benchmarks; 18 19 import io.grpc.CallOptions; 20 import io.grpc.Channel; 21 import io.grpc.ClientCall; 22 import io.grpc.ClientInterceptor; 23 import io.grpc.MethodDescriptor; 24 25 /** Interceptor that lets you cancel the most recent call made. This class is not thread-safe. */ 26 class CancellableInterceptor implements ClientInterceptor { 27 private ClientCall<?, ?> call; 28 29 @Override interceptCall( MethodDescriptor<ReqT,RespT> method, CallOptions callOptions, Channel next)30 public <ReqT,RespT> ClientCall<ReqT,RespT> interceptCall( 31 MethodDescriptor<ReqT,RespT> method, CallOptions callOptions, Channel next) { 32 ClientCall<ReqT, RespT> call = next.newCall(method, callOptions); 33 this.call = call; 34 return call; 35 } 36 cancel(String message, Throwable cause)37 public void cancel(String message, Throwable cause) { 38 if (call == null) { 39 throw new NullPointerException("No previous call"); 40 } 41 call.cancel(message, cause); 42 } 43 } 44