impl/codegen/client_interceptor.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H
20 #define GRPCPP_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H
21 
22 // IWYU pragma: private, include <grpcpp/support/client_interceptor.h>
23 
24 #include <memory>
25 #include <vector>
26 
30 
31 namespace grpc {
32 
33 class Channel;
34 class ClientContext;
35 
36 namespace internal {
37 class InterceptorBatchMethodsImpl;
38 }
39 
40 namespace experimental {
41 class ClientRpcInfo;
42 
43 // A factory interface for creation of client interceptors. A vector of
44 // factories can be provided at channel creation which will be used to create a
45 // new vector of client interceptors per RPC. Client interceptor authors should
46 // create a subclass of ClientInterceptorFactorInterface which creates objects
47 // of their interceptors.
49  public:
51  // Returns a pointer to an Interceptor object on successful creation, nullptr
52  // otherwise. If nullptr is returned, this server interceptor factory is
53  // ignored for the purposes of that RPC.
55 };
56 } // namespace experimental
57 
58 namespace internal {
59 extern experimental::ClientInterceptorFactoryInterface*
61 }
62 
67 namespace experimental {
69  public:
70  // TODO(yashykt): Stop default-constructing ClientRpcInfo and remove UNKNOWN
71  // from the list of possible Types.
73  enum class Type {
74  UNARY,
78  UNKNOWN // UNKNOWN is not API and will be removed later
79  };
80 
82 
83  // Delete copy constructor but allow default move constructor
84  ClientRpcInfo(const ClientRpcInfo&) = delete;
85  ClientRpcInfo(ClientRpcInfo&&) = default;
86 
87  // Getter methods
88 
90  const char* method() const { return method_; }
91 
94  const char* suffix_for_stats() const { return suffix_for_stats_; }
95 
98 
102 
104  Type type() const { return type_; }
105 
106  private:
107  static_assert(Type::UNARY ==
109  "violated expectation about Type enum");
110  static_assert(Type::CLIENT_STREAMING ==
112  "violated expectation about Type enum");
113  static_assert(Type::SERVER_STREAMING ==
115  "violated expectation about Type enum");
116  static_assert(Type::BIDI_STREAMING ==
118  "violated expectation about Type enum");
119 
120  // Default constructor should only be used by ClientContext
121  ClientRpcInfo() = default;
122 
123  // Constructor will only be called from ClientContext
125  const char* method, const char* suffix_for_stats,
127  : ctx_(ctx),
128  type_(static_cast<Type>(type)),
129  method_(method),
131  channel_(channel) {}
132 
133  // Move assignment should only be used by ClientContext
134  // TODO(yashykt): Delete move assignment
135  ClientRpcInfo& operator=(ClientRpcInfo&&) = default;
136 
137  // Runs interceptor at pos \a pos.
139  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
141  interceptors_[pos]->Intercept(interceptor_methods);
142  }
143 
145  const std::vector<std::unique_ptr<
147  size_t interceptor_pos) {
148  if (interceptor_pos > creators.size()) {
149  // No interceptors to register
150  return;
151  }
152  // NOTE: The following is not a range-based for loop because it will only
153  // iterate over a portion of the creators vector.
154  for (auto it = creators.begin() + interceptor_pos; it != creators.end();
155  ++it) {
156  auto* interceptor = (*it)->CreateClientInterceptor(this);
157  if (interceptor != nullptr) {
158  interceptors_.push_back(
159  std::unique_ptr<experimental::Interceptor>(interceptor));
160  }
161  }
163  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
165  ->CreateClientInterceptor(this)));
166  }
167  }
168 
170  // TODO(yashykt): make type_ const once move-assignment is deleted
172  const char* method_ = nullptr;
173  const char* suffix_for_stats_ = nullptr;
175  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
176  bool hijacked_ = false;
178 
180  friend class grpc::ClientContext;
181 };
182 
183 // PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL
184 // INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES.
185 // Registers a global client interceptor factory object, which is used for all
186 // RPCs made in this process. The application is responsible for maintaining the
187 // life of the object while gRPC operations are in progress. The global
188 // interceptor factory should only be registered once at the start of the
189 // process before any gRPC operations have begun.
192 
193 // For testing purposes only
195 
196 } // namespace experimental
197 } // namespace grpc
198 
199 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H
regen-readme.it
it
Definition: regen-readme.py:15
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
ctx
Definition: benchmark-async.c:30
grpc::experimental::ClientRpcInfo::~ClientRpcInfo
~ClientRpcInfo()
Definition: impl/codegen/client_interceptor.h:81
grpc
Definition: grpcpp/alarm.h:33
grpc::experimental::ClientRpcInfo::hijacked_interceptor_
size_t hijacked_interceptor_
Definition: impl/codegen/client_interceptor.h:177
grpc::experimental::ClientInterceptorFactoryInterface
Definition: impl/codegen/client_interceptor.h:48
grpc::experimental::ClientRpcInfo::type_
Type type_
Definition: impl/codegen/client_interceptor.h:171
grpc::experimental::ClientRpcInfo::interceptors_
std::vector< std::unique_ptr< experimental::Interceptor > > interceptors_
Definition: impl/codegen/client_interceptor.h:175
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:37
grpc::experimental::ClientRpcInfo::method
const char * method() const
Return the fully-specified method name.
Definition: impl/codegen/client_interceptor.h:90
grpc::experimental::ClientRpcInfo::Type::SERVER_STREAMING
@ SERVER_STREAMING
grpc::experimental::ClientRpcInfo::operator=
ClientRpcInfo & operator=(ClientRpcInfo &&)=default
grpc::experimental::ClientRpcInfo
Definition: impl/codegen/client_interceptor.h:68
grpc::internal::g_global_client_interceptor_factory
experimental::ClientInterceptorFactoryInterface * g_global_client_interceptor_factory
Definition: client_interceptor.cc:26
framework.rpc.grpc_channelz.Channel
Channel
Definition: grpc_channelz.py:32
grpc::internal::RpcMethod::NORMAL_RPC
@ NORMAL_RPC
Definition: grpcpp/impl/codegen/rpc_method.h:34
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: grpcpp/impl/codegen/channel_interface.h:73
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
grpc::experimental::ClientRpcInfo::type
Type type() const
Return the type of the RPC (unary or a streaming flavor)
Definition: impl/codegen/client_interceptor.h:104
grpc::experimental::RegisterGlobalClientInterceptorFactory
void RegisterGlobalClientInterceptorFactory(ClientInterceptorFactoryInterface *factory)
Definition: client_interceptor.cc:30
grpc::experimental::ClientRpcInfo::suffix_for_stats_
const char * suffix_for_stats_
Definition: impl/codegen/client_interceptor.h:173
rpc_method.h
grpc::experimental::ClientRpcInfo::RunInterceptor
void RunInterceptor(experimental::InterceptorBatchMethods *interceptor_methods, size_t pos)
Definition: impl/codegen/client_interceptor.h:138
grpc::experimental::ClientRpcInfo::suffix_for_stats
const char * suffix_for_stats() const
Definition: impl/codegen/client_interceptor.h:94
grpc::internal::RpcMethod::RpcType
RpcType
Definition: grpcpp/impl/codegen/rpc_method.h:33
grpc::experimental::ClientRpcInfo::ClientRpcInfo
ClientRpcInfo()=default
grpc::experimental::InterceptorBatchMethods
Definition: impl/codegen/interceptor.h:98
grpc::experimental::ClientRpcInfo::channel
ChannelInterface * channel()
Return a pointer to the channel on which the RPC is being sent.
Definition: impl/codegen/client_interceptor.h:97
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
string_ref.h
grpc::experimental::ClientRpcInfo::method_
const char * method_
Definition: impl/codegen/client_interceptor.h:172
grpc::experimental::ClientRpcInfo::Type::UNKNOWN
@ UNKNOWN
grpc::experimental::ClientInterceptorFactoryInterface::~ClientInterceptorFactoryInterface
virtual ~ClientInterceptorFactoryInterface()
Definition: impl/codegen/client_interceptor.h:50
grpc::experimental::ClientRpcInfo::RegisterInterceptors
void RegisterInterceptors(const std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> &creators, size_t interceptor_pos)
Definition: impl/codegen/client_interceptor.h:144
grpc::experimental::ClientRpcInfo::channel_
grpc::ChannelInterface * channel_
Definition: impl/codegen/client_interceptor.h:174
grpc::experimental::Interceptor
Definition: impl/codegen/interceptor.h:221
grpc::experimental::ClientRpcInfo::client_context
grpc::ClientContext * client_context()
Definition: impl/codegen/client_interceptor.h:101
grpc::experimental::ClientRpcInfo::hijacked_
bool hijacked_
Definition: impl/codegen/client_interceptor.h:176
grpc::experimental::ClientRpcInfo::ClientRpcInfo
ClientRpcInfo(grpc::ClientContext *ctx, internal::RpcMethod::RpcType type, const char *method, const char *suffix_for_stats, grpc::ChannelInterface *channel)
Definition: impl/codegen/client_interceptor.h:124
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:151
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc::experimental::ClientRpcInfo::ctx_
grpc::ClientContext * ctx_
Definition: impl/codegen/client_interceptor.h:169
grpc::experimental::ClientInterceptorFactoryInterface::CreateClientInterceptor
virtual Interceptor * CreateClientInterceptor(ClientRpcInfo *info)=0
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: grpcpp/impl/codegen/rpc_method.h:37
grpc::internal::RpcMethod::SERVER_STREAMING
@ SERVER_STREAMING
Definition: grpcpp/impl/codegen/rpc_method.h:36
grpc::internal::RpcMethod::CLIENT_STREAMING
@ CLIENT_STREAMING
Definition: grpcpp/impl/codegen/rpc_method.h:35
grpc::experimental::ClientRpcInfo::Type::UNARY
@ UNARY
grpc::experimental::ClientRpcInfo::Type::CLIENT_STREAMING
@ CLIENT_STREAMING
grpc::experimental::TestOnlyResetGlobalClientInterceptorFactory
void TestOnlyResetGlobalClientInterceptorFactory()
Definition: client_interceptor.cc:41
grpc::experimental::ClientRpcInfo::Type::BIDI_STREAMING
@ BIDI_STREAMING
interceptor.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:47