@@ -430,11 +430,18 @@ static void HandleBackupRequest(void* arg) {
430430 bthread_id_error (correlation_id, EBACKUPREQUEST);
431431}
432432
433- void Channel::CallMethod (const google::protobuf::MethodDescriptor* method,
434- google::protobuf::RpcController* controller_base,
435- const google::protobuf::Message* request,
436- google::protobuf::Message* response,
437- google::protobuf::Closure* done) {
433+ template <bool is_pb>
434+ void Channel::CallMethodInternal (const typename std::conditional<is_pb,
435+ google::protobuf::MethodDescriptor,
436+ brpc::flatbuffers::MethodDescriptor>::type* method,
437+ google::protobuf::RpcController* controller_base,
438+ const typename std::conditional<is_pb,
439+ google::protobuf::Message,
440+ brpc::flatbuffers::Message>::type* request,
441+ typename std::conditional<is_pb,
442+ google::protobuf::Message,
443+ brpc::flatbuffers::Message>::type* response,
444+ google::protobuf::Closure* done) {
438445 const int64_t start_send_real_us = butil::gettimeofday_us ();
439446 Controller* cntl = static_cast <Controller*>(controller_base);
440447 cntl->OnRPCBegin (start_send_real_us);
@@ -492,22 +499,36 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
492499
493500 if (cntl->_sender == NULL && IsTraceable (Span::tls_parent ())) {
494501 const int64_t start_send_us = butil::cpuwide_time_us ();
495- std::string method_name;
502+ const std::string* method_name = NULL ;
496503 if (_get_method_name) {
497- method_name = butil::EnsureString (_get_method_name (method, cntl));
504+ if (is_pb) {
505+ auto pb_method = reinterpret_cast <const google::protobuf::MethodDescriptor*>(method);
506+ method_name = &_get_method_name (pb_method, cntl);
507+ } else {
508+ // FlatBuffers doesn't support _get_method_name yet
509+ method_name = NULL ;
510+ }
498511 } else if (method) {
499- method_name = butil::EnsureString (method->full_name ());
512+ if (is_pb) {
513+ auto pb_method = reinterpret_cast <const google::protobuf::MethodDescriptor*>(method);
514+ method_name = &pb_method->full_name ();
515+ } else {
516+ auto fb_method = reinterpret_cast <const brpc::flatbuffers::MethodDescriptor*>(method);
517+ method_name = &fb_method->full_name ();
518+ }
500519 } else {
501520 const static std::string NULL_METHOD_STR = " null-method" ;
502- method_name = NULL_METHOD_STR;
521+ method_name = &NULL_METHOD_STR;
522+ }
523+ if (method_name) {
524+ Span* span = Span::CreateClientSpan (
525+ *method_name, start_send_real_us - start_send_us);
526+ span->set_log_id (cntl->log_id ());
527+ span->set_base_cid (correlation_id);
528+ span->set_protocol (_options.protocol );
529+ span->set_start_send_us (start_send_us);
530+ cntl->_span = span;
503531 }
504- Span* span = Span::CreateClientSpan (
505- method_name, start_send_real_us - start_send_us);
506- span->set_log_id (cntl->log_id ());
507- span->set_base_cid (correlation_id);
508- span->set_protocol (_options.protocol );
509- span->set_start_send_us (start_send_us);
510- cntl->_span = span;
511532 }
512533 // Override some options if they haven't been set by Controller
513534 if (cntl->timeout_ms () == UNSET_MAGIC_NUM) {
@@ -525,11 +546,20 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
525546 if (cntl->connection_type () == CONNECTION_TYPE_UNKNOWN) {
526547 cntl->set_connection_type (_options.connection_type );
527548 }
528- cntl-> _response = response;
549+
529550 cntl->_done = done;
530551 cntl->_pack_request = _pack_request;
531- cntl->_method = method;
532552 cntl->_auth = _options.auth ;
553+ // Use reinterpret_cast to avoid template instantiation errors
554+ // The actual type is guaranteed by the is_pb parameter
555+ if (is_pb) {
556+ cntl->_method = reinterpret_cast <const google::protobuf::MethodDescriptor*>(method);
557+ cntl->_response = reinterpret_cast <google::protobuf::Message*>(response);
558+ } else {
559+ cntl->_fb_method = reinterpret_cast <const brpc::flatbuffers::MethodDescriptor*>(method);
560+ cntl->_fb_response = reinterpret_cast <brpc::flatbuffers::Message*>(response);
561+ cntl->set_use_flatbuffer ();
562+ }
533563
534564 if (SingleServer ()) {
535565 cntl->_single_server_id = _server_id;
@@ -615,6 +645,22 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
615645 }
616646}
617647
648+ void Channel::CallMethod (const google::protobuf::MethodDescriptor* method,
649+ google::protobuf::RpcController* controller_base,
650+ const google::protobuf::Message* request,
651+ google::protobuf::Message* response,
652+ google::protobuf::Closure* done) {
653+ CallMethodInternal<true >(method, controller_base, request, response, done);
654+ }
655+
656+ void Channel::FBCallMethod (const brpc::flatbuffers::MethodDescriptor* method,
657+ google::protobuf::RpcController* controller_base,
658+ const brpc::flatbuffers::Message* request,
659+ brpc::flatbuffers::Message* response,
660+ google::protobuf::Closure* done) {
661+ CallMethodInternal<false >(method, controller_base, request, response, done);
662+ }
663+
618664void Channel::Describe (std::ostream& os, const DescribeOptions& opt) const {
619665 os << " Channel[" ;
620666 if (SingleServer ()) {
@@ -644,4 +690,24 @@ int Channel::CheckHealth() {
644690 }
645691}
646692
693+ // CallMethodInternal instance for pb and fb
694+ template
695+ void Channel::CallMethodInternal<true >(
696+ const google::protobuf::MethodDescriptor* method,
697+ google::protobuf::RpcController* controller_base,
698+ const google::protobuf::Message* request,
699+ google::protobuf::Message* response,
700+ google::protobuf::Closure* done
701+ );
702+
703+ // CallMethodInternal instance for pb and fb
704+ template
705+ void Channel::CallMethodInternal<false >(
706+ const brpc::flatbuffers::MethodDescriptor* method,
707+ google::protobuf::RpcController* controller_base,
708+ const brpc::flatbuffers::Message* request,
709+ brpc::flatbuffers::Message* response,
710+ google::protobuf::Closure* done
711+ );
712+
647713} // namespace brpc
0 commit comments