GRPC C++  1.64.0
client_interceptor.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPCPP_SUPPORT_CLIENT_INTERCEPTOR_H
20 #define GRPCPP_SUPPORT_CLIENT_INTERCEPTOR_H
21 
22 #include <memory>
23 #include <vector>
24 
25 #include "absl/log/check.h"
26 
27 #include <grpc/support/log.h>
28 #include <grpcpp/impl/rpc_method.h>
31 
32 namespace grpc {
33 
34 class Channel;
35 class ClientContext;
36 
37 namespace internal {
38 class InterceptorBatchMethodsImpl;
39 }
40 
41 namespace experimental {
42 class ClientRpcInfo;
43 
44 // A factory interface for creation of client interceptors. A vector of
45 // factories can be provided at channel creation which will be used to create a
46 // new vector of client interceptors per RPC. Client interceptor authors should
47 // create a subclass of ClientInterceptorFactorInterface which creates objects
48 // of their interceptors.
50  public:
52  // Returns a pointer to an Interceptor object on successful creation, nullptr
53  // otherwise. If nullptr is returned, this server interceptor factory is
54  // ignored for the purposes of that RPC.
56 };
57 } // namespace experimental
58 
59 namespace internal {
60 extern experimental::ClientInterceptorFactoryInterface*
62 
63 extern experimental::ClientInterceptorFactoryInterface*
65 } // namespace internal
66 
71 namespace experimental {
73  public:
74  // TODO(yashykt): Stop default-constructing ClientRpcInfo and remove UNKNOWN
75  // from the list of possible Types.
77  enum class Type {
78  UNARY,
82  UNKNOWN // UNKNOWN is not API and will be removed later
83  };
84 
86 
87  // Delete copy constructor but allow default move constructor
88  ClientRpcInfo(const ClientRpcInfo&) = delete;
89  ClientRpcInfo(ClientRpcInfo&&) = default;
90 
91  // Getter methods
92 
94  const char* method() const { return method_; }
95 
98  const char* suffix_for_stats() const { return suffix_for_stats_; }
99 
101  ChannelInterface* channel() { return channel_; }
102 
106 
108  Type type() const { return type_; }
109 
110  private:
111  static_assert(Type::UNARY ==
113  "violated expectation about Type enum");
114  static_assert(Type::CLIENT_STREAMING ==
116  "violated expectation about Type enum");
117  static_assert(Type::SERVER_STREAMING ==
119  "violated expectation about Type enum");
120  static_assert(Type::BIDI_STREAMING ==
122  "violated expectation about Type enum");
123 
124  // Default constructor should only be used by ClientContext
125  ClientRpcInfo() = default;
126 
127  // Constructor will only be called from ClientContext
129  const char* method, const char* suffix_for_stats,
131  : ctx_(ctx),
132  type_(static_cast<Type>(type)),
133  method_(method),
134  suffix_for_stats_(suffix_for_stats),
135  channel_(channel) {}
136 
137  // Move assignment should only be used by ClientContext
138  // TODO(yashykt): Delete move assignment
139  ClientRpcInfo& operator=(ClientRpcInfo&&) = default;
140 
141  // Runs interceptor at pos \a pos.
142  void RunInterceptor(
143  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
144  CHECK_LT(pos, interceptors_.size());
145  interceptors_[pos]->Intercept(interceptor_methods);
146  }
147 
148  void RegisterInterceptors(
149  const std::vector<std::unique_ptr<
150  experimental::ClientInterceptorFactoryInterface>>& creators,
151  size_t interceptor_pos) {
152  // TODO(yashykt): This calculation seems broken for the case where an
153  // interceptor factor returns nullptr.
154  size_t num_interceptors =
155  creators.size() +
158  if (interceptor_pos > num_interceptors) {
159  // No interceptors to register
160  return;
161  }
163  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
165  ->CreateClientInterceptor(this)));
166  --interceptor_pos;
167  }
168  // NOTE: The following is not a range-based for loop because it will only
169  // iterate over a portion of the creators vector.
170  for (auto it = creators.begin() + interceptor_pos; it != creators.end();
171  ++it) {
172  auto* interceptor = (*it)->CreateClientInterceptor(this);
173  if (interceptor != nullptr) {
174  interceptors_.push_back(
175  std::unique_ptr<experimental::Interceptor>(interceptor));
176  }
177  }
179  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
181  ->CreateClientInterceptor(this)));
182  }
183  }
184 
185  grpc::ClientContext* ctx_ = nullptr;
186  // TODO(yashykt): make type_ const once move-assignment is deleted
187  Type type_{Type::UNKNOWN};
188  const char* method_ = nullptr;
189  const char* suffix_for_stats_ = nullptr;
190  grpc::ChannelInterface* channel_ = nullptr;
191  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
192  bool hijacked_ = false;
193  size_t hijacked_interceptor_ = 0;
194 
196  friend class grpc::ClientContext;
197 };
198 
199 // PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL
200 // INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES.
201 // Registers a global client interceptor factory object, which is used for all
202 // RPCs made in this process. The application is responsible for maintaining the
203 // life of the object while gRPC operations are in progress. The global
204 // interceptor factory should only be registered once at the start of the
205 // process before any gRPC operations have begun.
208 
209 // For testing purposes only
211 
212 } // namespace experimental
213 } // namespace grpc
214 
215 #endif // GRPCPP_SUPPORT_CLIENT_INTERCEPTOR_H
grpc::experimental::ClientRpcInfo::type
Type type() const
Return the type of the RPC (unary or a streaming flavor)
Definition: client_interceptor.h:108
interceptor.h
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::internal::g_global_client_stats_interceptor_factory
experimental::ClientInterceptorFactoryInterface * g_global_client_stats_interceptor_factory
grpc::experimental::ClientRpcInfo::method
const char * method() const
Return the fully-specified method name.
Definition: client_interceptor.h:94
rpc_method.h
grpc::experimental::ClientRpcInfo::Type
Type
Type categorizes RPCs by unary or streaming type.
Definition: client_interceptor.h:77
grpc::internal::RpcMethod::CLIENT_STREAMING
@ CLIENT_STREAMING
Definition: rpc_method.h:33
grpc::experimental::ClientRpcInfo::suffix_for_stats
const char * suffix_for_stats() const
Return an identifying suffix for the client stub, or nullptr if one wasn't specified.
Definition: client_interceptor.h:98
grpc::experimental::ClientRpcInfo
Definition: client_interceptor.h:72
grpc::internal::g_global_client_interceptor_factory
experimental::ClientInterceptorFactoryInterface * g_global_client_interceptor_factory
log.h
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:35
grpc::experimental::ClientInterceptorFactoryInterface::~ClientInterceptorFactoryInterface
virtual ~ClientInterceptorFactoryInterface()
Definition: client_interceptor.h:51
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:195
grpc::experimental::RegisterGlobalClientInterceptorFactory
void RegisterGlobalClientInterceptorFactory(ClientInterceptorFactoryInterface *factory)
grpc::experimental::ClientRpcInfo::~ClientRpcInfo
~ClientRpcInfo()
Definition: client_interceptor.h:85
grpc::experimental::ClientRpcInfo::Type::SERVER_STREAMING
@ SERVER_STREAMING
grpc::experimental::ClientRpcInfo::Type::BIDI_STREAMING
@ BIDI_STREAMING
grpc::experimental::ClientRpcInfo::Type::UNARY
@ UNARY
grpc::experimental::ClientRpcInfo::client_context
grpc::ClientContext * client_context()
Return a pointer to the underlying ClientContext structure associated with the RPC to support feature...
Definition: client_interceptor.h:105
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:71
grpc::experimental::ClientRpcInfo::channel
ChannelInterface * channel()
Return a pointer to the channel on which the RPC is being sent.
Definition: client_interceptor.h:101
grpc::internal::RpcMethod::SERVER_STREAMING
@ SERVER_STREAMING
Definition: rpc_method.h:34
grpc::experimental::ClientInterceptorFactoryInterface::CreateClientInterceptor
virtual Interceptor * CreateClientInterceptor(ClientRpcInfo *info)=0
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::internal::RpcMethod::NORMAL_RPC
@ NORMAL_RPC
Definition: rpc_method.h:32
grpc::experimental::Interceptor
Interface for an interceptor.
Definition: interceptor.h:218
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:49
grpc::experimental::InterceptorBatchMethods
Class that is passed as an argument to the Intercept method of the application's Interceptor interfac...
Definition: interceptor.h:95
grpc::experimental::ClientRpcInfo::Type::CLIENT_STREAMING
@ CLIENT_STREAMING
grpc::experimental::ClientRpcInfo::Type::UNKNOWN
@ UNKNOWN
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:38
string_ref.h
grpc::experimental::TestOnlyResetGlobalClientInterceptorFactory
void TestOnlyResetGlobalClientInterceptorFactory()