Asynchronous model in grpc c++

后端 未结 2 773
感情败类
感情败类 2021-02-02 01:52

My team is designing a scalable solution with micro-services architecture and planning to use gRPC as the transport communication between layers. And we\'ve decided to use async

相关标签:
2条回答
  • 2021-02-02 01:58

    This post is pretty old by now but I have not seen any answer or example regarding this so I will show how I solved it to any other readers. I have around 30 RPC calls and was looking for a way of reducing the footprint when adding and removing RPC calls. It took me some iterations to figure out a good way to solve it.

    So my interface for getting RPC requests from my (g)RPC library is a callback interface that the recepiant need to implement. The interface looks like this:

    class IRpcRequestHandler
    {
    public:
        virtual ~IRpcRequestHandler() = default;
        virtual void onZigbeeOpenNetworkRequest(const smarthome::ZigbeeOpenNetworkRequest& req,
                                                smarthome::Response& res) = 0;
        virtual void onZigbeeTouchlinkDeviceRequest(const smarthome::ZigbeeTouchlinkDeviceRequest& req,
                                                    smarthome::Response& res) = 0;
        ...
    };
    

    And some code for setting up/register each RPC method after the gRPC server is started:

    void ready() 
    {
        SETUP_SMARTHOME_CALL("ZigbeeOpenNetwork", // Alias that is used for debug messages
                             smarthome::Command::AsyncService::RequestZigbeeOpenNetwork,  // Generated gRPC service method for async.
                             smarthome::ZigbeeOpenNetworkRequest, // Generated gRPC service request message
                             smarthome::Response, // Generated gRPC service response message
                             IRpcRequestHandler::onZigbeeOpenNetworkRequest); // The callback method to call when request has arrived.
    
        SETUP_SMARTHOME_CALL("ZigbeeTouchlinkDevice",
                             smarthome::Command::AsyncService::RequestZigbeeTouchlinkDevice,
                             smarthome::ZigbeeTouchlinkDeviceRequest,
                             smarthome::Response,
                             IRpcRequestHandler::onZigbeeTouchlinkDeviceRequest);
        ...
    }
    

    This is all that you need to care about when adding and removing RPC methods.

    The SETUP_SMARTHOME_CALL is a home-cooked macro which looks like this:

    #define SETUP_SMARTHOME_CALL(ALIAS, SERVICE, REQ, RES, CALLBACK_FUNC) \
      new ServerCallData<REQ, RES>(                                       \
          ALIAS,                                                          \
          std::bind(&SERVICE,                                             \
                    &mCommandService,                                     \
                    std::placeholders::_1,                                \
                    std::placeholders::_2,                                \
                    std::placeholders::_3,                                \
                    std::placeholders::_4,                                \
                    std::placeholders::_5,                                \
                    std::placeholders::_6),                               \
          mCompletionQueue.get(),                                         \
          std::bind(&CALLBACK_FUNC, requestHandler, std::placeholders::_1, std::placeholders::_2))
    

    I think the ServerCallData class looks like the one from gRPCs examples with a few modifications. ServerCallData is derived from a non-templete class with an abstract function void proceed(bool ok) for the CompletionQueue::Next() handling. When ServerCallData is created, it will call the SERVICE method to register itself on the CompletionQueue and on every first proceed(ok) call, it will clone itself which will register another instance. I can post some sample code for that as well if someone is interested.

    EDIT: Added some more sample code below.

    GrpcServer

    class GrpcServer
    {
     public:
      explicit GrpcServer(std::vector<grpc::Service*> services);
      virtual ~GrpcServer();
    
      void run(const std::string& sslKey,
               const std::string& sslCert,
               const std::string& password,
               const std::string& listenAddr,
               uint32_t port,
               uint32_t threads = 1);
    
     private:
      virtual void ready();  // Called after gRPC server is created and before polling CQ.
      void handleRpcs();  // Function that polls from CQ, can be run by multiple threads. Casts object to CallData and calls CallData::proceed().
    
      std::unique_ptr<ServerCompletionQueue> mCompletionQueue;
      std::unique_ptr<Server> mServer;
      std::vector<grpc::Service*> mServices;
      std::list<std::shared_ptr<std::thread>> mThreads;
      ...
    }
    

    And the main part of the CallData object:

    template <typename TREQUEST, typename TREPLY>
    class ServerCallData : public ServerCallMethod
    {
     public:
      explicit ServerCallData(const std::string& methodName,
                              std::function<void(ServerContext*,
                                                 TREQUEST*,
                                                 ::grpc::ServerAsyncResponseWriter<TREPLY>*,
                                                 ::grpc::CompletionQueue*,
                                                 ::grpc::ServerCompletionQueue*,
                                                 void*)> serviceFunc,
                              grpc::ServerCompletionQueue* completionQueue,
                              std::function<void(const TREQUEST&, TREPLY&)> callback,
                              bool first = false)
          : ServerCallMethod(methodName),
            mResponder(&mContext),
            serviceFunc(serviceFunc),
            completionQueue(completionQueue),
            callback(callback)
      {
        requestNewCall();
      }
    
      void proceed(bool ok) override
      {
        if (!ok)
        {
          delete this;
          return;
        }
    
        if (callStatus() == ServerCallMethod::PROCESS)
        {    
          callStatus() = ServerCallMethod::FINISH;
          new ServerCallData<TREQUEST, TREPLY>(callMethodName(), serviceFunc, completionQueue, callback);
    
          try
          {
            callback(mRequest, mReply);
          }
          catch (const std::exception& e)
          {
            mResponder.Finish(mReply, Status::CANCELLED, this);
            return;
          }
    
          mResponder.Finish(mReply, Status::OK, this);
        }
        else
        {    
          delete this;
        }
      }
    
     private:
      void requestNewCall()
      {
        serviceFunc(
            &mContext, &mRequest, &mResponder, completionQueue, completionQueue, this);
      }
    
      ServerContext mContext;
      TREQUEST mRequest;
      TREPLY mReply;
      ServerAsyncResponseWriter<TREPLY> mResponder;
      std::function<void(ServerContext*,
                         TREQUEST*,
                         ::grpc::ServerAsyncResponseWriter<TREPLY>*,
                         ::grpc::CompletionQueue*,
                         ::grpc::ServerCompletionQueue*,
                         void*)>
          serviceFunc;
      std::function<void(const TREQUEST&, TREPLY&)> callback;
      grpc::ServerCompletionQueue* completionQueue;
    };
    
    0 讨论(0)
  • 2021-02-02 02:21

    Although the thread is old I wanted to share a solution I am currently implementing. It mainly consists templated classes inheriting CallData to be scalable. This way, each new rpc will only require specializing the templates of the required CallData methods.

    Calldata header:

    class CallData {
        protected:
            enum Status { CREATE, PROCESS, FINISH };
            Status status;
    
            virtual void treat_create() = 0;
            virtual void treat_process() = 0;
    
        public:
            void Proceed();
    };
    

    CallData Proceed implementation:

    void CallData::Proceed() {
        switch (status) {
            case CREATE:
                status = PROCESS;
                treat_create();
                break;
            case PROCESS:
                status = FINISH;
                treat_process();
                break;
            case FINISH:
                delete this;
        }
    }
    

    Inheriting from CallData header (simplified):

    template <typename Request, typename Reply>
    class CallDataTemplated : CallData {
        static_assert(std::is_base_of<google::protobuf::Message, Request>::value, 
            "Request and reply must be protobuf messages");
        static_assert(std::is_base_of<google::protobuf::Message, Reply>::value,
            "Request and reply must be protobuf messages");
    
        private:
            Service,Cq,Context,ResponseWriter,...
            Request request;
            Reply reply;
    
        protected:
            void treat_create() override;
            void treat_process() override;
    
        public:
            ...
    };
    

    Then, for specific rpc's in theory you should be able to do things like:

    template<>
    void CallDataTemplated<HelloRequest, HelloReply>::treat_process() {
         ...
    }
    

    It's a lot of templated methods but preferable to creating a class per rpc from my point of view.

    0 讨论(0)
提交回复
热议问题