微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用相同完成队列的gRPC c ++多个异步服务器

如何解决使用相同完成队列的gRPC c ++多个异步服务器

这是关于在异步gRPC中使用完成队列的一般问题。对于每个事务,我将其与完成队列绑定,然后将其传递给函数,该函数生成对特定远程节点的RPC调用,如下所示:

Status Sundial_Async_Client:: contactRemote(CompletionQueue* cq,TxnManager * txn,uint64_t node_id,SundialRequest& request,SundialResponse** response){

        // Call object to store rpc data
        AsyncclientCall* call = new AsyncclientCall;

        // stub_->PrepareAsyncSayHello() creates an RPC object,returning
        // an instance to store in "call" but does not actually start the RPC
        // Because we are using the asynchronous API,we need to hold on to
        // the "call" instance in order to get updates on the ongoing RPC.
        call->response_reader =
            stub_->PrepareAsynccontactRemote(&call->context,request,cq);

        // StartCall initiates the RPC call
        call->response_reader->StartCall();
        call->reply=*response;
        // Request that,upon completion of the RPC,"reply" be updated with the
        // server's response; "status" with the indication of whether the operation
        // was successful. Tag the request with the memory address of the call object.
        call->response_reader->Finish(call->reply,&call->status,(void*)call);
        glob_stats->_stats[GET_THD_ID]->_req_msg_count[ request.request_type() ] ++;
        glob_stats->_stats[GET_THD_ID]->_req_msg_size[ request.request_type() ] += 
        request.SpaceUsedLong();
    return Status::OK;
}

我还使用相同的完成队列检查响应

Status Sundial_Async_Client::contactRemoteDone(CompletionQueue* cq,SundialResponse* response,int count){
    if(count==0){
        return Status::OK;
    }
    void* got_tag;
    bool ok = false;
    int local_count=0;
    // Block until the next result is available in the completion queue "cq".
    while (cq->Next(&got_tag,&ok)) {
            local_count++;
            // The tag in this example is the memory location of the call object
            AsyncclientCall* call = static_cast<AsyncclientCall*>(got_tag);

            // Verify that the request was completed successfully. Note that "ok"
            // corresponds solely to the request for updates introduced by Finish().
            GPR_ASSERT(ok);


            // Once we're complete,deallocate the call object.
             //doing the cleaning
            glob_stats->_stats[GET_THD_ID]->_resp_msg_count[ call->reply->response_type() ] ++;
            glob_stats->_stats[GET_THD_ID]->_resp_msg_size[ call->reply->response_type() ] += call->reply->SpaceUsedLong();
            delete call;
            if(local_count==count){
            break;
        }
        }
        
     
      return Status::OK;
   
}

在我的2个节点设置下运行正常,但是我要切换到2个以上的节点,这意味着客户端可以向多个远程异步服务器发送异步请求。 然后,我想知道是否可以使用相同的完成队列来保留来自不同远程服务器的响应。基本上,在我的主线程中,我将传递与第一个函数中的参数相同的完成队列,并在一个循环中一起检查所有响应。我希望我的问题有道理。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。