From 0390b29e15e79acd1e84c511d5c20d8e20ee2eb6 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Tue, 30 Oct 2018 02:09:11 -0700 Subject: Register global interceptors functionality --- include/grpcpp/channel.h | 2 +- include/grpcpp/impl/codegen/channel_interface.h | 2 +- include/grpcpp/impl/codegen/client_interceptor.h | 27 ++++++++++++++++++++++- include/grpcpp/impl/codegen/intercepted_channel.h | 4 ++-- 4 files changed, 30 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/grpcpp/channel.h b/include/grpcpp/channel.h index 14209b85ee..4502b94b17 100644 --- a/include/grpcpp/channel.h +++ b/include/grpcpp/channel.h @@ -91,7 +91,7 @@ class Channel final : public ChannelInterface, internal::Call CreateCallInternal(const internal::RpcMethod& method, ClientContext* context, CompletionQueue* cq, - int interceptor_pos) override; + size_t interceptor_pos) override; const grpc::string host_; grpc_channel* const c_channel_; // owned diff --git a/include/grpcpp/impl/codegen/channel_interface.h b/include/grpcpp/impl/codegen/channel_interface.h index 6fd1dd1d9b..e8ed6a9805 100644 --- a/include/grpcpp/impl/codegen/channel_interface.h +++ b/include/grpcpp/impl/codegen/channel_interface.h @@ -133,7 +133,7 @@ class ChannelInterface { virtual internal::Call CreateCallInternal(const internal::RpcMethod& method, ClientContext* context, CompletionQueue* cq, - int interceptor_pos) { + size_t interceptor_pos) { return internal::Call(); } diff --git a/include/grpcpp/impl/codegen/client_interceptor.h b/include/grpcpp/impl/codegen/client_interceptor.h index 00113f04aa..f44a7c469c 100644 --- a/include/grpcpp/impl/codegen/client_interceptor.h +++ b/include/grpcpp/impl/codegen/client_interceptor.h @@ -19,6 +19,7 @@ #ifndef GRPCPP_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H #define GRPCPP_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H +#include #include #include @@ -42,7 +43,14 @@ class ClientInterceptorFactoryInterface { virtual ~ClientInterceptorFactoryInterface() {} virtual Interceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0; }; +} // namespace experimental +namespace internal { +extern experimental::ClientInterceptorFactoryInterface* + g_global_client_interceptor_factory; +} + +namespace experimental { class ClientRpcInfo { public: ClientRpcInfo() {} @@ -72,12 +80,21 @@ class ClientRpcInfo { void RegisterInterceptors( const std::vector>& creators, - int interceptor_pos) { + size_t interceptor_pos) { + if (interceptor_pos > creators.size()) { + // No interceptors to register + return; + } for (auto it = creators.begin() + interceptor_pos; it != creators.end(); ++it) { interceptors_.push_back(std::unique_ptr( (*it)->CreateClientInterceptor(this))); } + if (internal::g_global_client_interceptor_factory != nullptr) { + interceptors_.push_back(std::unique_ptr( + internal::g_global_client_interceptor_factory + ->CreateClientInterceptor(this))); + } } grpc::ClientContext* ctx_ = nullptr; @@ -91,6 +108,14 @@ class ClientRpcInfo { friend class grpc::ClientContext; }; +// DO NOT USE THIS +// Registers a global client interceptor factory object. This should be +// registered before any channels are created. The application is responsible +// for maintaining the life of the object while gRPC is used. It is unsafe to +// try to register if gRPC operations have already started. +void RegisterGlobalClientInterceptorFactory( + ClientInterceptorFactoryInterface* factory); + } // namespace experimental } // namespace grpc diff --git a/include/grpcpp/impl/codegen/intercepted_channel.h b/include/grpcpp/impl/codegen/intercepted_channel.h index 612e56d862..5255a6d147 100644 --- a/include/grpcpp/impl/codegen/intercepted_channel.h +++ b/include/grpcpp/impl/codegen/intercepted_channel.h @@ -42,7 +42,7 @@ class InterceptedChannel : public ChannelInterface { } private: - InterceptedChannel(ChannelInterface* channel, int pos) + InterceptedChannel(ChannelInterface* channel, size_t pos) : channel_(channel), interceptor_pos_(pos) {} Call CreateCall(const RpcMethod& method, ClientContext* context, @@ -70,7 +70,7 @@ class InterceptedChannel : public ChannelInterface { CompletionQueue* CallbackCQ() override { return channel_->CallbackCQ(); } ChannelInterface* channel_; - int interceptor_pos_; + size_t interceptor_pos_; friend class InterceptorBatchMethodsImpl; }; -- cgit v1.2.3 From 857e79ce005870332f0e44347e7c933747bd5d72 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Wed, 31 Oct 2018 14:35:54 -0700 Subject: Improve documentation --- include/grpcpp/impl/codegen/client_interceptor.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/grpcpp/impl/codegen/client_interceptor.h b/include/grpcpp/impl/codegen/client_interceptor.h index f44a7c469c..bc62bb4a7b 100644 --- a/include/grpcpp/impl/codegen/client_interceptor.h +++ b/include/grpcpp/impl/codegen/client_interceptor.h @@ -108,11 +108,17 @@ class ClientRpcInfo { friend class grpc::ClientContext; }; -// DO NOT USE THIS -// Registers a global client interceptor factory object. This should be -// registered before any channels are created. The application is responsible -// for maintaining the life of the object while gRPC is used. It is unsafe to -// try to register if gRPC operations have already started. +// PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL +// INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES. +// Registers a global client interceptor factory object, which is used for all +// RPCs made in this process. If the argument is nullptr, the global +// interceptor factory is deregistered. The application is responsible for +// maintaining the life of the object while gRPC operations are in progress. It +// is unsafe to try to register/deregister if any gRPC operation is in progress. +// For safety, it is in the best interests of the developer to register the +// global interceptor factory once at the start of the process before any gRPC +// operations have begun. Deregistration is optional since gRPC does not +// maintain any references to the object. void RegisterGlobalClientInterceptorFactory( ClientInterceptorFactoryInterface* factory); -- cgit v1.2.3