Starting on server interceptors

pull/16842/head
Yash Tibrewal 7 years ago
parent f9fe345553
commit b232bacf27
  1. 1
      BUILD
  2. 5
      CMakeLists.txt
  3. 5
      Makefile
  4. 1
      build.yaml
  5. 1
      gRPC-C++.podspec
  6. 26
      include/grpcpp/impl/codegen/call.h
  7. 47
      include/grpcpp/impl/codegen/client_interceptor.h
  8. 8
      include/grpcpp/impl/codegen/interceptor.h
  9. 89
      include/grpcpp/impl/codegen/server_interceptor.h
  10. 8
      src/cpp/server/server_context.cc
  11. 12
      test/cpp/end2end/client_interceptors_end2end_test.cc
  12. 1
      tools/doxygen/Doxyfile.c++
  13. 1
      tools/doxygen/Doxyfile.c++.internal
  14. 2
      tools/run_tests/generated/sources_and_headers.json

@ -2065,6 +2065,7 @@ grpc_cc_library(
"include/grpcpp/impl/codegen/security/auth_context.h",
"include/grpcpp/impl/codegen/serialization_traits.h",
"include/grpcpp/impl/codegen/server_context.h",
"include/grpcpp/impl/codegen/server_interceptor.h",
"include/grpcpp/impl/codegen/server_interface.h",
"include/grpcpp/impl/codegen/service_type.h",
"include/grpcpp/impl/codegen/slice.h",

@ -3100,6 +3100,7 @@ foreach(_hdr
include/grpcpp/impl/codegen/security/auth_context.h
include/grpcpp/impl/codegen/serialization_traits.h
include/grpcpp/impl/codegen/server_context.h
include/grpcpp/impl/codegen/server_interceptor.h
include/grpcpp/impl/codegen/server_interface.h
include/grpcpp/impl/codegen/service_type.h
include/grpcpp/impl/codegen/slice.h
@ -3671,6 +3672,7 @@ foreach(_hdr
include/grpcpp/impl/codegen/security/auth_context.h
include/grpcpp/impl/codegen/serialization_traits.h
include/grpcpp/impl/codegen/server_context.h
include/grpcpp/impl/codegen/server_interceptor.h
include/grpcpp/impl/codegen/server_interface.h
include/grpcpp/impl/codegen/service_type.h
include/grpcpp/impl/codegen/slice.h
@ -4080,6 +4082,7 @@ foreach(_hdr
include/grpcpp/impl/codegen/security/auth_context.h
include/grpcpp/impl/codegen/serialization_traits.h
include/grpcpp/impl/codegen/server_context.h
include/grpcpp/impl/codegen/server_interceptor.h
include/grpcpp/impl/codegen/server_interface.h
include/grpcpp/impl/codegen/service_type.h
include/grpcpp/impl/codegen/slice.h
@ -4261,6 +4264,7 @@ foreach(_hdr
include/grpcpp/impl/codegen/security/auth_context.h
include/grpcpp/impl/codegen/serialization_traits.h
include/grpcpp/impl/codegen/server_context.h
include/grpcpp/impl/codegen/server_interceptor.h
include/grpcpp/impl/codegen/server_interface.h
include/grpcpp/impl/codegen/service_type.h
include/grpcpp/impl/codegen/slice.h
@ -4583,6 +4587,7 @@ foreach(_hdr
include/grpcpp/impl/codegen/security/auth_context.h
include/grpcpp/impl/codegen/serialization_traits.h
include/grpcpp/impl/codegen/server_context.h
include/grpcpp/impl/codegen/server_interceptor.h
include/grpcpp/impl/codegen/server_interface.h
include/grpcpp/impl/codegen/service_type.h
include/grpcpp/impl/codegen/slice.h

@ -5444,6 +5444,7 @@ PUBLIC_HEADERS_CXX += \
include/grpcpp/impl/codegen/security/auth_context.h \
include/grpcpp/impl/codegen/serialization_traits.h \
include/grpcpp/impl/codegen/server_context.h \
include/grpcpp/impl/codegen/server_interceptor.h \
include/grpcpp/impl/codegen/server_interface.h \
include/grpcpp/impl/codegen/service_type.h \
include/grpcpp/impl/codegen/slice.h \
@ -6024,6 +6025,7 @@ PUBLIC_HEADERS_CXX += \
include/grpcpp/impl/codegen/security/auth_context.h \
include/grpcpp/impl/codegen/serialization_traits.h \
include/grpcpp/impl/codegen/server_context.h \
include/grpcpp/impl/codegen/server_interceptor.h \
include/grpcpp/impl/codegen/server_interface.h \
include/grpcpp/impl/codegen/service_type.h \
include/grpcpp/impl/codegen/slice.h \
@ -6418,6 +6420,7 @@ PUBLIC_HEADERS_CXX += \
include/grpcpp/impl/codegen/security/auth_context.h \
include/grpcpp/impl/codegen/serialization_traits.h \
include/grpcpp/impl/codegen/server_context.h \
include/grpcpp/impl/codegen/server_interceptor.h \
include/grpcpp/impl/codegen/server_interface.h \
include/grpcpp/impl/codegen/service_type.h \
include/grpcpp/impl/codegen/slice.h \
@ -6576,6 +6579,7 @@ PUBLIC_HEADERS_CXX += \
include/grpcpp/impl/codegen/security/auth_context.h \
include/grpcpp/impl/codegen/serialization_traits.h \
include/grpcpp/impl/codegen/server_context.h \
include/grpcpp/impl/codegen/server_interceptor.h \
include/grpcpp/impl/codegen/server_interface.h \
include/grpcpp/impl/codegen/service_type.h \
include/grpcpp/impl/codegen/slice.h \
@ -6903,6 +6907,7 @@ PUBLIC_HEADERS_CXX += \
include/grpcpp/impl/codegen/security/auth_context.h \
include/grpcpp/impl/codegen/serialization_traits.h \
include/grpcpp/impl/codegen/server_context.h \
include/grpcpp/impl/codegen/server_interceptor.h \
include/grpcpp/impl/codegen/server_interface.h \
include/grpcpp/impl/codegen/service_type.h \
include/grpcpp/impl/codegen/slice.h \

@ -1238,6 +1238,7 @@ filegroups:
- include/grpcpp/impl/codegen/security/auth_context.h
- include/grpcpp/impl/codegen/serialization_traits.h
- include/grpcpp/impl/codegen/server_context.h
- include/grpcpp/impl/codegen/server_interceptor.h
- include/grpcpp/impl/codegen/server_interface.h
- include/grpcpp/impl/codegen/service_type.h
- include/grpcpp/impl/codegen/slice.h

@ -148,6 +148,7 @@ Pod::Spec.new do |s|
'include/grpcpp/impl/codegen/security/auth_context.h',
'include/grpcpp/impl/codegen/serialization_traits.h',
'include/grpcpp/impl/codegen/server_context.h',
'include/grpcpp/impl/codegen/server_interceptor.h',
'include/grpcpp/impl/codegen/server_interface.h',
'include/grpcpp/impl/codegen/service_type.h',
'include/grpcpp/impl/codegen/slice.h',

@ -35,6 +35,7 @@
#include <grpcpp/impl/codegen/config.h>
#include <grpcpp/impl/codegen/core_codegen_interface.h>
#include <grpcpp/impl/codegen/serialization_traits.h>
#include <grpcpp/impl/codegen/server_interceptor.h>
#include <grpcpp/impl/codegen/slice.h>
#include <grpcpp/impl/codegen/status.h>
#include <grpcpp/impl/codegen/string_ref.h>
@ -764,14 +765,14 @@ class Call final {
cq_(nullptr),
call_(nullptr),
max_receive_message_size_(-1),
rpc_info_(nullptr) {}
client_rpc_info_(nullptr) {}
/** call is owned by the caller */
Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq)
: call_hook_(call_hook),
cq_(cq),
call_(call),
max_receive_message_size_(-1),
rpc_info_(nullptr) {}
client_rpc_info_(nullptr) {}
Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq,
experimental::ClientRpcInfo* rpc_info)
@ -779,7 +780,7 @@ class Call final {
cq_(cq),
call_(call),
max_receive_message_size_(-1),
rpc_info_(rpc_info) {}
client_rpc_info_(rpc_info) {}
Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq,
int max_receive_message_size)
@ -787,7 +788,7 @@ class Call final {
cq_(cq),
call_(call),
max_receive_message_size_(max_receive_message_size),
rpc_info_(nullptr) {}
client_rpc_info_(nullptr) {}
void PerformOps(CallOpSetInterface* ops) {
call_hook_->PerformOpsOnCall(ops, this);
@ -797,14 +798,17 @@ class Call final {
CompletionQueue* cq() const { return cq_; }
int max_receive_message_size() const { return max_receive_message_size_; }
experimental::ClientRpcInfo* rpc_info() const { return rpc_info_; }
experimental::ClientRpcInfo* client_rpc_info() const {
return client_rpc_info_;
}
private:
CallHook* call_hook_;
CompletionQueue* cq_;
grpc_call* call_;
int max_receive_message_size_;
experimental::ClientRpcInfo* rpc_info_;
experimental::ClientRpcInfo* client_rpc_info_;
experimental::ServerRpcInfo* server_rpc_info_;
};
/// An abstract collection of call ops, used to generate the
@ -861,7 +865,7 @@ class InterceptorBatchMethodsImpl
virtual void Proceed() override { /* fill this */
curr_iteration_ = reverse_ ? curr_iteration_ - 1 : curr_iteration_ + 1;
auto* rpc_info = call_->rpc_info();
auto* rpc_info = call_->client_rpc_info();
if (rpc_info->hijacked_ &&
(!reverse_ && curr_iteration_ == rpc_info->hijacked_interceptor_ + 1)) {
/* We now need to provide hijacked recv ops to this interceptor */
@ -898,7 +902,7 @@ class InterceptorBatchMethodsImpl
virtual void Hijack() override { /* fill this */
GPR_CODEGEN_ASSERT(!reverse_);
auto* rpc_info = call_->rpc_info();
auto* rpc_info = call_->client_rpc_info();
rpc_info->hijacked_ = true;
rpc_info->hijacked_interceptor_ = curr_iteration_;
ClearHookPoints();
@ -993,11 +997,11 @@ class InterceptorBatchMethodsImpl
/* This needs to be set before interceptors are run */
void SetCall(Call* call) { call_ = call; }
void SetCallOpSet(CallOpSetInterface* ops) { ops_ = ops; }
void SetCallOpSetInterface(CallOpSetInterface* ops) { ops_ = ops; }
/* Returns true if no interceptors are run */
bool RunInterceptors() {
auto* rpc_info = call_->rpc_info();
auto* rpc_info = call_->client_rpc_info();
if (rpc_info == nullptr || rpc_info->interceptors_.size() == 0) {
return true;
}
@ -1174,7 +1178,7 @@ class CallOpSet : public CallOpSetInterface,
this->Op4::SetInterceptionHookPoint(&interceptor_methods_);
this->Op5::SetInterceptionHookPoint(&interceptor_methods_);
this->Op6::SetInterceptionHookPoint(&interceptor_methods_);
interceptor_methods_.SetCallOpSet(this);
interceptor_methods_.SetCallOpSetInterface(this);
interceptor_methods_.SetCall(&call_);
// interceptor_methods_.SetFunctions(ContinueFillOpsAfterInterception,
// SetHijackingState, ContinueFinalizeResultAfterInterception);

@ -38,17 +38,10 @@ class CallNoOp;
namespace experimental {
class ClientRpcInfo;
class ClientInterceptor {
public:
virtual ~ClientInterceptor() {}
virtual void Intercept(InterceptorBatchMethods* methods) = 0;
};
class ClientInterceptorFactoryInterface {
public:
virtual ~ClientInterceptorFactoryInterface() {}
virtual ClientInterceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0;
virtual Interceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0;
};
class ClientRpcInfo {
@ -60,7 +53,7 @@ class ClientRpcInfo {
experimental::ClientInterceptorFactoryInterface>>& creators)
: ctx_(ctx), method_(method), channel_(channel) {
for (const auto& creator : creators) {
interceptors_.push_back(std::unique_ptr<experimental::ClientInterceptor>(
interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
creator->CreateClientInterceptor(this)));
}
}
@ -74,32 +67,7 @@ class ClientRpcInfo {
const char* method() { return method_; }
const Channel* channel() { return channel_; }
grpc::ClientContext* client_context() { return ctx_; }
// const grpc::InterceptedMessage& outgoing_message();
// grpc::InterceptedMessage *mutable_outgoing_message();
// const grpc::InterceptedMessage& received_message();
// grpc::InterceptedMessage *mutable_received_message();
// const std::multimap<grpc::string, grpc::string>* client_initial_metadata()
// { return &ctx_->send_initial_metadata_; } const
// std::multimap<grpc::string_ref, grpc::string_ref>*
// server_initial_metadata() { return &ctx_->GetServerInitialMetadata(); }
// const std::multimap<grpc::string_ref, grpc::string_ref>*
// server_trailing_metadata() { return &ctx_->GetServerTrailingMetadata(); }
// const Status *status();
// template <class M>
// void set_outgoing_message(M* msg); // edit outgoing message
// template <class M>
// void set_received_message(M* msg); // edit received message
// for hijacking (can be called multiple times for streaming)
// template <class M>
// void inject_received_message(M* msg);
// void set_client_initial_metadata(
// const std::multimap<grpc::string, grpc::string>& overwrite);
// void set_server_initial_metadata(const std::multimap<grpc::string,
// grpc::string>& overwrite); void set_server_trailing_metadata(const
// std::multimap<grpc::string, grpc::string>& overwrite); void
// set_status(Status status);
public:
/* Runs interceptor at pos \a pos. If \a reverse is set, the interceptor order
* is the reverse */
@ -110,20 +78,15 @@ class ClientRpcInfo {
interceptors_[pos]->Intercept(interceptor_methods);
}
private:
grpc::ClientContext* ctx_ = nullptr;
const char* method_ = nullptr;
const grpc::Channel* channel_ = nullptr;
public:
std::vector<std::unique_ptr<experimental::ClientInterceptor>> interceptors_;
std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
bool hijacked_ = false;
int hijacked_interceptor_ = false;
// template <class Op1 = internal::CallNoOp<1>, class Op2 =
// internal::CallNoOp<2>,
// class Op3 = internal::CallNoOp<3>, class Op4 =
// internal::CallNoOp<4>, class Op5 = internal::CallNoOp<5>, class Op6
// = internal::CallNoOp<6>>
// friend class internal::InterceptorBatchMethodsImpl;
};
} // namespace experimental

@ -119,6 +119,14 @@ class InterceptorBatchMethods {
virtual void SetRecvTrailingMetadata(internal::MetadataMap* map) = 0;
};
class Interceptor {
public:
virtual ~Interceptor() {}
virtual void Intercept(InterceptorBatchMethods* methods) = 0;
};
} // namespace experimental
} // namespace grpc

@ -0,0 +1,89 @@
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
#define GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
#include <vector>
#include <grpc/impl/codegen/log.h>
#include <grpcpp/impl/codegen/interceptor.h>
#include <grpcpp/impl/codegen/string_ref.h>
namespace grpc {
class ServerContext;
namespace internal {
template <int I>
class CallNoOp;
}
namespace experimental {
class ServerRpcInfo;
class ServerInterceptorFactoryInterface {
public:
virtual ~ServerInterceptorFactoryInterface() {}
virtual Interceptor* CreateServerInterceptor(ServerRpcInfo* info) = 0;
};
class ServerRpcInfo {
public:
ServerRpcInfo() {}
ServerRpcInfo(grpc::ServerContext* ctx, const char* method,
const std::vector<std::unique_ptr<
experimental::ServerInterceptorFactoryInterface>>& creators)
: ctx_(ctx), method_(method) {
for (const auto& creator : creators) {
interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
creator->CreateServerInterceptor(this)));
}
}
~ServerRpcInfo(){};
ServerRpcInfo(const ServerRpcInfo&) = delete;
ServerRpcInfo(ServerRpcInfo&&) = default;
ServerRpcInfo& operator=(ServerRpcInfo&&) = default;
// Getter methods
const char* method() { return method_; }
grpc::ServerContext* server_context() { return ctx_; }
public:
/* Runs interceptor at pos \a pos. If \a reverse is set, the interceptor order
* is the reverse */
void RunInterceptor(
experimental::InterceptorBatchMethods* interceptor_methods,
unsigned int pos) {
GPR_CODEGEN_ASSERT(pos < interceptors_.size());
interceptors_[pos]->Intercept(interceptor_methods);
}
private:
grpc::ServerContext* ctx_ = nullptr;
const char* method_ = nullptr;
public:
std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
};
} // namespace experimental
} // namespace grpc
#endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H

@ -45,7 +45,8 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface {
tag_(nullptr),
refs_(2),
finalized_(false),
cancelled_(0) {}
cancelled_(0),
done_intercepting_(false) {}
void FillOps(internal::Call* call) override;
bool FinalizeResult(void** tag, bool* status) override;
@ -89,6 +90,9 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface {
int refs_;
bool finalized_;
int cancelled_;
bool done_intercepting_;
internal::Call call_;
internal::InterceptorBatchMethodsImpl interceptor_methods_;
};
void ServerContext::CompletionOp::Unref() {
@ -105,6 +109,8 @@ void ServerContext::CompletionOp::FillOps(internal::Call* call) {
ops.data.recv_close_on_server.cancelled = &cancelled_;
ops.flags = 0;
ops.reserved = nullptr;
call_ = *call;
interceptor_methods_.SetCall(&call_);
GPR_ASSERT(GRPC_CALL_OK ==
grpc_call_start_batch(call->call(), &ops, 1, this, nullptr));
}

@ -60,7 +60,7 @@ class ClientInterceptorsEnd2endTest : public ::testing::Test {
std::unique_ptr<Server> server_;
};
class DummyInterceptor : public experimental::ClientInterceptor {
class DummyInterceptor : public experimental::Interceptor {
public:
DummyInterceptor(experimental::ClientRpcInfo* info) {}
@ -85,13 +85,13 @@ std::atomic<int> DummyInterceptor::num_times_run_;
class DummyInterceptorFactory
: public experimental::ClientInterceptorFactoryInterface {
public:
virtual experimental::ClientInterceptor* CreateClientInterceptor(
virtual experimental::Interceptor* CreateClientInterceptor(
experimental::ClientRpcInfo* info) override {
return new DummyInterceptor(info);
}
};
class HijackingInterceptor : public experimental::ClientInterceptor {
class HijackingInterceptor : public experimental::Interceptor {
public:
HijackingInterceptor(experimental::ClientRpcInfo* info) {
info_ = info;
@ -189,13 +189,13 @@ class HijackingInterceptor : public experimental::ClientInterceptor {
class HijackingInterceptorFactory
: public experimental::ClientInterceptorFactoryInterface {
public:
virtual experimental::ClientInterceptor* CreateClientInterceptor(
virtual experimental::Interceptor* CreateClientInterceptor(
experimental::ClientRpcInfo* info) override {
return new HijackingInterceptor(info);
}
};
class LoggingInterceptor : public experimental::ClientInterceptor {
class LoggingInterceptor : public experimental::Interceptor {
public:
LoggingInterceptor(experimental::ClientRpcInfo* info) {
info_ = info;
@ -262,7 +262,7 @@ class LoggingInterceptor : public experimental::ClientInterceptor {
class LoggingInterceptorFactory
: public experimental::ClientInterceptorFactoryInterface {
public:
virtual experimental::ClientInterceptor* CreateClientInterceptor(
virtual experimental::Interceptor* CreateClientInterceptor(
experimental::ClientRpcInfo* info) override {
return new LoggingInterceptor(info);
}

@ -970,6 +970,7 @@ include/grpcpp/impl/codegen/rpc_service_method.h \
include/grpcpp/impl/codegen/security/auth_context.h \
include/grpcpp/impl/codegen/serialization_traits.h \
include/grpcpp/impl/codegen/server_context.h \
include/grpcpp/impl/codegen/server_interceptor.h \
include/grpcpp/impl/codegen/server_interface.h \
include/grpcpp/impl/codegen/service_type.h \
include/grpcpp/impl/codegen/slice.h \

@ -972,6 +972,7 @@ include/grpcpp/impl/codegen/rpc_service_method.h \
include/grpcpp/impl/codegen/security/auth_context.h \
include/grpcpp/impl/codegen/serialization_traits.h \
include/grpcpp/impl/codegen/server_context.h \
include/grpcpp/impl/codegen/server_interceptor.h \
include/grpcpp/impl/codegen/server_interface.h \
include/grpcpp/impl/codegen/service_type.h \
include/grpcpp/impl/codegen/slice.h \

@ -11173,6 +11173,7 @@
"include/grpcpp/impl/codegen/security/auth_context.h",
"include/grpcpp/impl/codegen/serialization_traits.h",
"include/grpcpp/impl/codegen/server_context.h",
"include/grpcpp/impl/codegen/server_interceptor.h",
"include/grpcpp/impl/codegen/server_interface.h",
"include/grpcpp/impl/codegen/service_type.h",
"include/grpcpp/impl/codegen/slice.h",
@ -11243,6 +11244,7 @@
"include/grpcpp/impl/codegen/security/auth_context.h",
"include/grpcpp/impl/codegen/serialization_traits.h",
"include/grpcpp/impl/codegen/server_context.h",
"include/grpcpp/impl/codegen/server_interceptor.h",
"include/grpcpp/impl/codegen/server_interface.h",
"include/grpcpp/impl/codegen/service_type.h",
"include/grpcpp/impl/codegen/slice.h",

Loading…
Cancel
Save