brpc icon indicating copy to clipboard operation
brpc copied to clipboard

thrift协议支持多参数-讨论

Open lzfhust opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? (你需要的功能是否与某个问题有关?) 当前brpc对thrift协议,仅支持单个参数。thrift协议的多参数形式应用广泛,国内一些公司内部也在使用。希望brpc官方也能支持多参数

Describe the solution you'd like (描述你期望的解决方法) brpc同时支持单参数、多参数

Describe alternatives you've considered (描述你想到的折衷方案)

Additional context/screenshots (更多上下文/截图)

1). 通过 xxx_args来实现多参数: 1.1. client:

example::EchoService_Echo_args real_req;
        example::Context context;
        example::EchoRequest req2;
        example::EchoResponse res;
        req2.__set_data("hello");
        real_req.context = std::move(context);
        real_req.request2 = std::move(req2);
        LOG(INFO) << "request data:" << real_req.request2.data;
        stub.CallMethod("Echo", &cntl, &real_req, &res, NULL);

1.2. server:

       void ProcessThriftFramedRequest(brpc::Controller* cntl,
                                    brpc::ThriftFramedMessage* req,
                                    brpc::ThriftFramedMessage* res,
                                    google::protobuf::Closure* done) override {
        // Dispatch calls to different methods
        if (cntl->thrift_method_name() == "Echo") { 
            return Echo(cntl, req->Cast<example::EchoService_Echo_args>(),
                        res->Cast<example::EchoResponse>(), done);
        }

1.3. thrift_protocol.cpp 在 ReadThriftStruct中,直接读,即: xfer += raw_msg->Read(&iprot); 在 ThriftClosure::DoRun中,直接写回:xfer += _response.raw_instance()->Write(&oprot);

这样,可以支持多参数。 然而,对于单个参数,brpc thrift_protocol会对request、response消息进行writeStructBegin、writeFieldBegin、readStructBegin、readFieldBegin等逻辑处理; 对于多参数,xxx_args本身提供的read、write接口应该就已经有了readStructBegin、writeStructBegin等逻辑了。

2). 遇到的问题: 如何和单参数兼容?

lzfhust avatar Jun 09 '22 10:06 lzfhust

之前有个PR,可以看看 #495

wwbmmm avatar Jun 10 '22 02:06 wwbmmm

此外,在ReadThriftStruct的时候,是不是得try-catch一下?我们发现,client在ProcessThriftResponse的时候,有可能会出现ReadThriftStruct的exception,进而导致core。

lzfhust avatar Jun 10 '22 03:06 lzfhust