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 <grpcpp/impl/rpc_method.h> 23 #include <grpcpp/support/interceptor.h> 24 #include <grpcpp/support/string_ref.h> 25 26 #include <memory> 27 #include <vector> 28 29 #include "absl/log/absl_check.h" 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. 48 class ClientInterceptorFactoryInterface { 49 public: ~ClientInterceptorFactoryInterface()50 virtual ~ClientInterceptorFactoryInterface() {} 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. 54 virtual Interceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0; 55 }; 56 } // namespace experimental 57 58 namespace internal { 59 extern experimental::ClientInterceptorFactoryInterface* 60 g_global_client_interceptor_factory; 61 62 extern experimental::ClientInterceptorFactoryInterface* 63 g_global_client_stats_interceptor_factory; 64 } // namespace internal 65 66 /// ClientRpcInfo represents the state of a particular RPC as it 67 /// appears to an interceptor. It is created and owned by the library and 68 /// passed to the CreateClientInterceptor method of the application's 69 /// ClientInterceptorFactoryInterface implementation 70 namespace experimental { 71 class ClientRpcInfo { 72 public: 73 // TODO(yashykt): Stop default-constructing ClientRpcInfo and remove UNKNOWN 74 // from the list of possible Types. 75 /// Type categorizes RPCs by unary or streaming type 76 enum class Type { 77 UNARY, 78 CLIENT_STREAMING, 79 SERVER_STREAMING, 80 BIDI_STREAMING, 81 UNKNOWN // UNKNOWN is not API and will be removed later 82 }; 83 ~ClientRpcInfo()84 ~ClientRpcInfo() {} 85 86 // Delete copy constructor but allow default move constructor 87 ClientRpcInfo(const ClientRpcInfo&) = delete; 88 ClientRpcInfo(ClientRpcInfo&&) = default; 89 90 // Getter methods 91 92 /// Return the fully-specified method name method()93 const char* method() const { return method_; } 94 95 /// Return an identifying suffix for the client stub, or nullptr if one wasn't 96 /// specified. suffix_for_stats()97 const char* suffix_for_stats() const { return suffix_for_stats_; } 98 99 /// Return a pointer to the channel on which the RPC is being sent channel()100 ChannelInterface* channel() { return channel_; } 101 102 /// Return a pointer to the underlying ClientContext structure associated 103 /// with the RPC to support features that apply to it client_context()104 grpc::ClientContext* client_context() { return ctx_; } 105 106 /// Return the type of the RPC (unary or a streaming flavor) type()107 Type type() const { return type_; } 108 109 private: 110 static_assert(Type::UNARY == 111 static_cast<Type>(internal::RpcMethod::NORMAL_RPC), 112 "violated expectation about Type enum"); 113 static_assert(Type::CLIENT_STREAMING == 114 static_cast<Type>(internal::RpcMethod::CLIENT_STREAMING), 115 "violated expectation about Type enum"); 116 static_assert(Type::SERVER_STREAMING == 117 static_cast<Type>(internal::RpcMethod::SERVER_STREAMING), 118 "violated expectation about Type enum"); 119 static_assert(Type::BIDI_STREAMING == 120 static_cast<Type>(internal::RpcMethod::BIDI_STREAMING), 121 "violated expectation about Type enum"); 122 123 // Default constructor should only be used by ClientContext 124 ClientRpcInfo() = default; 125 126 // Constructor will only be called from ClientContext ClientRpcInfo(grpc::ClientContext * ctx,internal::RpcMethod::RpcType type,const char * method,const char * suffix_for_stats,grpc::ChannelInterface * channel)127 ClientRpcInfo(grpc::ClientContext* ctx, internal::RpcMethod::RpcType type, 128 const char* method, const char* suffix_for_stats, 129 grpc::ChannelInterface* channel) 130 : ctx_(ctx), 131 type_(static_cast<Type>(type)), 132 method_(method), 133 suffix_for_stats_(suffix_for_stats), 134 channel_(channel) {} 135 136 // Move assignment should only be used by ClientContext 137 // TODO(yashykt): Delete move assignment 138 ClientRpcInfo& operator=(ClientRpcInfo&&) = default; 139 140 // Runs interceptor at pos \a pos. RunInterceptor(experimental::InterceptorBatchMethods * interceptor_methods,size_t pos)141 void RunInterceptor( 142 experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) { 143 ABSL_CHECK_LT(pos, interceptors_.size()); 144 interceptors_[pos]->Intercept(interceptor_methods); 145 } 146 RegisterInterceptors(const std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> & creators,size_t interceptor_pos)147 void RegisterInterceptors( 148 const std::vector<std::unique_ptr< 149 experimental::ClientInterceptorFactoryInterface>>& creators, 150 size_t interceptor_pos) { 151 // TODO(yashykt): This calculation seems broken for the case where an 152 // interceptor factor returns nullptr. 153 size_t num_interceptors = 154 creators.size() + 155 (internal::g_global_client_stats_interceptor_factory != nullptr) + 156 (internal::g_global_client_interceptor_factory != nullptr); 157 if (interceptor_pos > num_interceptors) { 158 // No interceptors to register 159 return; 160 } 161 if (internal::g_global_client_stats_interceptor_factory != nullptr) { 162 interceptors_.push_back(std::unique_ptr<experimental::Interceptor>( 163 internal::g_global_client_stats_interceptor_factory 164 ->CreateClientInterceptor(this))); 165 --interceptor_pos; 166 } 167 // NOTE: The following is not a range-based for loop because it will only 168 // iterate over a portion of the creators vector. 169 for (auto it = creators.begin() + interceptor_pos; it != creators.end(); 170 ++it) { 171 auto* interceptor = (*it)->CreateClientInterceptor(this); 172 if (interceptor != nullptr) { 173 interceptors_.push_back( 174 std::unique_ptr<experimental::Interceptor>(interceptor)); 175 } 176 } 177 if (internal::g_global_client_interceptor_factory != nullptr) { 178 interceptors_.push_back(std::unique_ptr<experimental::Interceptor>( 179 internal::g_global_client_interceptor_factory 180 ->CreateClientInterceptor(this))); 181 } 182 } 183 184 grpc::ClientContext* ctx_ = nullptr; 185 // TODO(yashykt): make type_ const once move-assignment is deleted 186 Type type_{Type::UNKNOWN}; 187 const char* method_ = nullptr; 188 const char* suffix_for_stats_ = nullptr; 189 grpc::ChannelInterface* channel_ = nullptr; 190 std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_; 191 bool hijacked_ = false; 192 size_t hijacked_interceptor_ = 0; 193 194 friend class internal::InterceptorBatchMethodsImpl; 195 friend class grpc::ClientContext; 196 }; 197 198 // PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL 199 // INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES. 200 // Registers a global client interceptor factory object, which is used for all 201 // RPCs made in this process. The application is responsible for maintaining the 202 // life of the object while gRPC operations are in progress. The global 203 // interceptor factory should only be registered once at the start of the 204 // process before any gRPC operations have begun. 205 void RegisterGlobalClientInterceptorFactory( 206 ClientInterceptorFactoryInterface* factory); 207 208 // For testing purposes only 209 void TestOnlyResetGlobalClientInterceptorFactory(); 210 211 } // namespace experimental 212 } // namespace grpc 213 214 #endif // GRPCPP_SUPPORT_CLIENT_INTERCEPTOR_H 215