Asynchronous model in grpc c++

后端 未结 2 772
感情败类
感情败类 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 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 
    class CallDataTemplated : CallData {
        static_assert(std::is_base_of::value, 
            "Request and reply must be protobuf messages");
        static_assert(std::is_base_of::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::treat_process() {
         ...
    }
    

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

提交回复
热议问题