• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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;
18 
19 import javax.annotation.concurrent.ThreadSafe;
20 
21 /**
22  * Interface for intercepting outgoing calls before they are dispatched by a {@link Channel}.
23  *
24  * <p>Implementers use this mechanism to add cross-cutting behavior to {@link Channel} and
25  * stub implementations. Common examples of such behavior include:
26  * <ul>
27  * <li>Logging and monitoring call behavior</li>
28  * <li>Adding metadata for proxies to observe</li>
29  * <li>Request and response rewriting</li>
30  * </ul>
31  *
32  * <p>Providing authentication credentials is better served by {@link
33  * CallCredentials}. But a {@code ClientInterceptor} could set the {@code
34  * CallCredentials} within the {@link CallOptions}.
35  *
36  * <p>The interceptor may be called for multiple {@link ClientCall calls} by one or more threads
37  * without completing the previous ones first. Refer to the
38  * {@link io.grpc.ClientCall.Listener ClientCall.Listener} docs for more details regarding thread
39  * safety of the returned listener.
40  */
41 @ThreadSafe
42 public interface ClientInterceptor {
43   /**
44    * Intercept {@link ClientCall} creation by the {@code next} {@link Channel}.
45    *
46    * <p>Many variations of interception are possible. Complex implementations may return a wrapper
47    * around the result of {@code next.newCall()}, whereas a simpler implementation may just modify
48    * the header metadata prior to returning the result of {@code next.newCall()}.
49    *
50    * <p>{@code next.newCall()} <strong>must not</strong> be called under a different {@link Context}
51    * other than the current {@code Context}. The outcome of such usage is undefined and may cause
52    * memory leak due to unbounded chain of {@code Context}s.
53    *
54    * @param method the remote method to be called.
55    * @param callOptions the runtime options to be applied to this call.
56    * @param next the channel which is being intercepted.
57    * @return the call object for the remote operation, never {@code null}.
58    */
interceptCall( MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next)59   <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
60       MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next);
61 }
62